import { ApiPropertyOptional } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import {
  IsDateString,
  IsNotEmpty,
  IsOptional,
  IsString,
  Matches,
  MaxLength,
} from 'class-validator';

import { MEETING_TIME_PATTERN } from './create-progress-report-meeting.dto';

function trimText(value: unknown) {
  return typeof value === 'string' ? value.trim() : value;
}

// Only these fields can be edited. "slot", "progressReportId", "userId", and
// "status" are left out on purpose: the slot is fixed when the meeting is
// created, and the status only changes through the send endpoint. The global
// ValidationPipe uses forbidNonWhitelisted, so sending any of them fails.
export class UpdateProgressReportMeetingDto {
  @ApiPropertyOptional({ example: 'Semester 2 | 2025' })
  @Transform(({ value }) => trimText(value))
  @IsOptional()
  @IsString()
  @IsNotEmpty()
  @MaxLength(100)
  title?: string;

  @ApiPropertyOptional({ example: '<p>Meeting description</p>' })
  @Transform(({ value }) => trimText(value))
  @IsOptional()
  @IsString()
  @IsNotEmpty()
  description?: string;

  @ApiPropertyOptional({
    example: '2025-06-07',
    description: 'Meeting date in YYYY-MM-DD format.',
  })
  @IsOptional()
  @IsString()
  @Matches(/^\d{4}-\d{2}-\d{2}$/)
  @IsDateString()
  meetingDate?: string;

  @ApiPropertyOptional({
    example: '09:00:00',
    description: 'Start time in 24-hour HH:mm or HH:mm:ss format.',
  })
  @IsOptional()
  @IsString()
  @Matches(MEETING_TIME_PATTERN)
  startTime?: string;

  @ApiPropertyOptional({
    example: '11:00:00',
    description: 'End time in 24-hour HH:mm or HH:mm:ss format.',
  })
  @IsOptional()
  @IsString()
  @Matches(MEETING_TIME_PATTERN)
  endTime?: string;

  @ApiPropertyOptional({ example: 'CDC-GPSF Office' })
  @Transform(({ value }) => trimText(value))
  @IsOptional()
  @IsString()
  @IsNotEmpty()
  @MaxLength(255)
  location?: string;
}
