import {
  IsOptional,
  IsString,
  IsNotEmpty,
  IsInt,
  IsArray,
  IsDateString,
  IsEnum,
  ArrayNotEmpty,
  ValidateNested,
  Min,
} from 'class-validator';
import { Type, Transform } from 'class-transformer';
import { ClassType, ClassStatus, TeacherRoleType } from '../../entities';

export class ClassTeacherItemDto {
  @IsInt()
  userId: number;

  @IsEnum(TeacherRoleType)
  roleType: string;

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

export class CreateClassDto {
  @IsString()
  @IsNotEmpty()
  name: string;

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

  @IsEnum(ClassType)
  @IsString()
  @IsNotEmpty()
  classType: string;

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

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

  @IsEnum(ClassStatus)
  @IsOptional()
  @IsString()
  status?: string;

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

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

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

  @IsOptional()
  @IsInt()
  @Min(0)
  maxStudents?: number;

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

  @IsOptional()
  @IsArray()
  @IsInt({ each: true })
  studentIds?: number[];

  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ImportUserItem)
  users?: ImportUserItem[];

  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ClassTeacherItemDto)
  teachers?: ClassTeacherItemDto[];
}

export class UpdateClassDto {
  @IsOptional()
  @IsString()
  name?: string;

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

  @IsEnum(ClassType)
  @IsOptional()
  @IsString()
  classType?: string;

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

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

  @IsEnum(ClassStatus)
  @IsOptional()
  @IsString()
  status?: string;

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

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

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

  @IsOptional()
  @IsInt()
  @Min(0)
  maxStudents?: number;

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

export class QueryClassDto {
  @IsOptional()
  @IsString()
  status?: string;

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

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

  @IsOptional()
  @Transform(({ value }) => {
    if (typeof value === 'boolean') return value;
    if (value === 'true' || value === '1') return true;
    if (value === 'false' || value === '0') return false;
    return value;
  })
  isArchived?: boolean;
}

export class AddStudentsDto {
  @IsArray()
  @IsInt({ each: true })
  studentIds: number[];
}

export class AddTeacherDto {
  @IsInt()
  userId: number;

  @IsEnum(TeacherRoleType)
  roleType: string;

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

export class QueryClassScheduleDto {
  @IsOptional()
  @IsDateString()
  startDate?: string;

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

export class QueryClassAttendanceSummaryDto {
  @IsOptional()
  @IsDateString()
  startDate?: string;

  @IsOptional()
  @IsDateString()
  endDate?: string;
}
export class BatchImportStudentsDto {
  @IsArray()
  @ArrayNotEmpty()
  @ValidateNested({ each: true })
  @Type(() => ImportUserItem)
  users: ImportUserItem[];
}

export class ImportUserItem {
  @IsString()
  @IsNotEmpty()
  dingUserId: string;

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

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