import {
  IsArray,
  IsOptional,
  IsString,
  IsInt,
  IsBoolean,
  IsDateString,
  IsIn,
  ValidateNested,
  IsNotEmpty,
  ArrayNotEmpty,
  Matches,
  Max,
  Min,
} from 'class-validator';
import { Type } from 'class-transformer';


export class AttendancePeriodConfigItemDto {
  @IsString()
  @IsNotEmpty()
  periodKey: string;

  @IsString()
  @IsNotEmpty()
  label: string;

  @IsString()
  @Matches(/^([01]\d|2[0-3]):[0-5]\d$/)
  startTime: string;

  @IsString()
  @Matches(/^([01]\d|2[0-3]):[0-5]\d$/)
  endTime: string;

  @IsOptional()
  @IsInt()
  sortOrder?: number;

  @IsOptional()
  @IsBoolean()
  enabled?: boolean;
}

export class SaveAttendancePeriodConfigsDto {
  @IsArray()
  @ArrayNotEmpty()
  @ValidateNested({ each: true })
  @Type(() => AttendancePeriodConfigItemDto)
  periods: AttendancePeriodConfigItemDto[];
}

export class AttendanceRecordItem {
  @IsInt()
  @IsNotEmpty()
  studentId: number;

  @IsOptional()
  @IsInt()
  classId?: number;

  @IsDateString()
  @IsNotEmpty()
  attendanceDate: string;

  @IsString()
  @IsIn(['morning_reading', 'morning', 'afternoon', 'evening_study', 'night_check'])
  @IsNotEmpty()
  session: string;

  @IsString()
  @IsIn(['present', 'late', 'absent', 'leave'])
  @IsNotEmpty()
  status: string;

  @IsOptional()
  @IsString()
  remark?: string;

  @IsOptional()
  @IsString()
  source?: string;
}

export class BatchCreateAttendanceDto {
  @IsArray()
  @ArrayNotEmpty()
  @ValidateNested({ each: true })
  @Type(() => AttendanceRecordItem)
  records: AttendanceRecordItem[];
}

export class AttendanceSummaryQueryDto {
  @IsOptional()
  @IsInt()
  @Type(() => Number)
  classId?: number;

  @IsOptional()
  @IsInt()
  @Type(() => Number)
  scheduleId?: number;

  @IsOptional()
  @IsDateString()
  dateFrom?: string;

  @IsOptional()
  @IsDateString()
  dateTo?: string;

  @IsOptional()
  @IsString()
  session?: string;
}


export class RefreshDingTalkAttendanceDto {
  @IsDateString()
  @IsNotEmpty()
  date: string;

  @IsOptional()
  @IsInt()
  @Type(() => Number)
  classId?: number;

  @IsOptional()
  @IsString()
  session?: string;
}

export class AttendanceCalendarQueryDto {
  @IsInt()
  @Type(() => Number)
  @IsNotEmpty()
  classId: number;

  @IsOptional()
  @IsDateString()
  weekStart?: string;
}

export class QueryDingRawDto {
  @IsOptional()
  @IsString()
  @IsIn(['unmatched', 'pending', 'matched'])
  matchStatus?: string;

  @IsOptional()
  @IsInt()
  @Type(() => Number)
  classId?: number;

  @IsOptional()
  @IsDateString()
  dateFrom?: string;

  @IsOptional()
  @IsDateString()
  dateTo?: string;

  @IsOptional()
  @IsInt()
  @Min(1)
  @Type(() => Number)
  page?: number;

  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(200)
  @Type(() => Number)
  pageSize?: number;
}

export class AttendanceScheduleOptionsQueryDto {
  @IsInt()
  @Type(() => Number)
  @IsNotEmpty()
  classId: number;

  @IsDateString()
  @IsNotEmpty()
  date: string;
}

export class QueryAttendanceRecordsDto {
  @IsOptional()
  @IsInt()
  @Type(() => Number)
  classId?: number;

  @IsOptional()
  @IsInt()
  @Type(() => Number)
  scheduleId?: number;

  @IsOptional()
  @IsDateString()
  dateFrom?: string;

  @IsOptional()
  @IsDateString()
  dateTo?: string;

  @IsOptional()
  @IsString()
  session?: string;

  @IsOptional()
  @IsString()
  @IsIn(['present', 'late', 'absent', 'leave', 'pending'])
  status?: string;

  @IsOptional()
  @IsString()
  source?: string;

  @IsOptional()
  @IsInt()
  @Min(1)
  @Type(() => Number)
  page?: number;

  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(200)
  @Type(() => Number)
  pageSize?: number;
}

export class MatchDingRecordDto {
  @IsInt()
  @IsNotEmpty()
  studentId: number;
}

export class UpdateAttendanceRecordDto {
  @IsOptional()
  @IsString()
  @IsIn(['present', 'late', 'absent', 'leave'])
  status?: string;

  @IsOptional()
  @IsString()
  remark?: string;
}


export class AttendanceAlertsQueryDto {
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  @Max(365)
  days?: number;

  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  threshold?: number;
}

export class AttendanceReportQueryDto {
  @IsOptional()
  @IsInt()
  @Type(() => Number)
  classId?: number;

  @IsOptional()
  @IsDateString()
  dateFrom?: string;

  @IsOptional()
  @IsDateString()
  dateTo?: string;
}

export class GenerateAttendanceFromSchedulesDto {
  @IsInt()
  @Type(() => Number)
  @IsNotEmpty()
  classId: number;

  @IsDateString()
  @IsNotEmpty()
  dateFrom: string;

  @IsDateString()
  @IsNotEmpty()
  dateTo: string;
}

export class GenerateFromSchedulesDto {
  @IsInt()
  @Type(() => Number)
  @IsNotEmpty()
  classId: number;

  @IsOptional()
  @IsDateString()
  startDate?: string;

  @IsOptional()
  @IsDateString()
  endDate?: string;
}

export class LessonAttendanceQueryDto {
  @IsDateString()
  @IsNotEmpty()
  date: string;
}

export class StartLessonAttendanceDto {
  @IsDateString()
  @IsNotEmpty()
  date: string;
}
