import { ProgressReportSemester } from '@/generated/prisma/client';
import { ApiPropertyOptional } from '@nestjs/swagger';
import { Transform, Type, type TransformFnParams } from 'class-transformer';
import {
  IsEnum,
  IsInt,
  IsNotEmpty,
  IsOptional,
  IsString,
  Max,
  MaxLength,
  Min,
} from 'class-validator';

import { IsEditorContent } from './is-editor-content.validator';

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

// Only editable draft content lives here. "status" is missing on purpose so
// the send endpoint owns that transition, and "firstDeadline" is missing so
// deadlines are only changed through the deadline endpoints. The global
// ValidationPipe uses forbidNonWhitelisted, so sending them fails.
export class UpdateProgressReportDto {
  @ApiPropertyOptional({ example: 'Semester 1 progress report' })
  @Transform(trimString)
  @IsOptional()
  @IsString()
  @IsNotEmpty()
  @MaxLength(255)
  title?: string;

  @ApiPropertyOptional({ enum: ProgressReportSemester })
  @IsOptional()
  @IsEnum(ProgressReportSemester)
  semester?: ProgressReportSemester;

  @ApiPropertyOptional({ example: 'A short summary of this semester report.' })
  @Transform(trimString)
  @IsOptional()
  @IsEditorContent()
  description?: string | Record<string, unknown>;

  @ApiPropertyOptional({ example: 2026 })
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(2000)
  @Max(2100)
  year?: number;

  @ApiPropertyOptional({
    type: 'string',
    format: 'binary',
    description: 'Replacement PDF attachment, uploaded as multipart form data.',
  })
  @IsOptional()
  @Transform(trimString)
  @IsString()
  @MaxLength(500)
  attachment?: string;

  @ApiPropertyOptional({ type: 'string', format: 'binary' })
  @IsOptional()
  @Transform(trimString)
  @IsString()
  @MaxLength(500)
  draftSemesterReport?: string;

  @ApiPropertyOptional({ type: 'string', format: 'binary' })
  @IsOptional()
  @Transform(trimString)
  @IsString()
  @MaxLength(500)
  finalSemesterReport?: string;
}
