import {
  IsArray,
  IsBoolean,
  IsIn,
  IsInt,
  IsNotEmpty,
  IsOptional,
  IsString,
  ArrayUnique,
} from 'class-validator'
import { Type } from 'class-transformer'
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import {
  SESSION_ATTENDANCE_REPORT_PURPOSES,
  SessionAttendanceReportPurpose,
} from '../constants/session-attendance-report.constants'

export class SessionAttendanceReportFilterDto {
  @ApiProperty()
  @IsNotEmpty()
  @IsInt()
  @Type(() => Number)
  programId: number

  @ApiPropertyOptional()
  @IsOptional()
  @IsInt()
  @Type(() => Number)
  sessionId?: number

  @ApiPropertyOptional({ type: [String] })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  registrationStatus?: string[]

  @ApiPropertyOptional()
  @IsOptional()
  @IsBoolean()
  attended?: boolean
}

export class GenerateSessionAttendanceReportDto {
  @ApiPropertyOptional({
    type: [String],
    description: 'List of field keys to include in the report. Omit and use `purpose` instead for a hardcoded, program-type-specific field set.',
  })
  @IsOptional()
  @IsArray()
  @ArrayUnique()
  @IsString({ each: true })
  fields?: string[]

  @ApiPropertyOptional({
    enum: SESSION_ATTENDANCE_REPORT_PURPOSES,
    description: 'Shortcut for a hardcoded field set, so callers don\'t need to pass `fields`. Add a new value here (and its field list in reports.service.ts) per report type instead of a new endpoint.',
  })
  @IsOptional()
  @IsIn(SESSION_ATTENDANCE_REPORT_PURPOSES)
  purpose?: SessionAttendanceReportPurpose

  @ApiProperty({ type: SessionAttendanceReportFilterDto, description: 'Filters — programId is required' })
  @IsNotEmpty()
  filters: SessionAttendanceReportFilterDto

  @ApiPropertyOptional({ enum: ['json', 'excel'] })
  @IsOptional()
  @IsIn(['json', 'excel'])
  format?: 'json' | 'excel'

  @ApiPropertyOptional({ description: 'Custom file name for excel download (without extension)' })
  @IsOptional()
  @IsString()
  fileName?: string
}
