import {
    IsEmail,
    IsNotEmpty,
    IsOptional,
    IsString,
    MinLength,
    IsDateString,
    IsArray,
    IsBoolean,
    ValidateNested,
} from 'class-validator';
import { Type } from 'class-transformer';
import { CustomDocumentDto } from '../../common/dto/custom-document.dto';
import { CustomImageDto } from '../../common/dto/custom-image.dto';
import { EducationEntryDto } from '../../common/dto/education-entry.dto';

export class CreateStudentDto {
    // === Mandatory Fields ===
    @IsNotEmpty() @IsString() name: string;
    @IsNotEmpty() @IsString() classId: string;
    @IsNotEmpty() @IsString() academicYearId: string;
    @IsDateString() dateOfBirth: string;

    // === Optional Account Fields ===
    @IsOptional() @IsEmail() email?: string;
    @IsOptional() @IsString() @MinLength(8) password?: string;

    // Enrolment
    // Stored as string for historical compatibility (legacy rolls may contain
    // non-digit characters). Frontend inputs force digits-only for new/edited
    // values so new data stays numeric — but the backend stays permissive to
    // avoid breaking edits on pre-existing non-numeric rolls.
    @IsOptional() @IsString() rollNumber?: string;
    @IsOptional() @IsString() sectionId?: string;
    @IsOptional() @IsString() gender?: string;

    // Admission
    @IsOptional() @IsDateString() admissionDate?: string;

    // Student info (optional)
    @IsOptional() @IsString() motherTongue?: string;
    @IsOptional() @IsString() religion?: string;
    @IsOptional() @IsString() caste?: string;
    @IsOptional() @IsString() mediumOfInstruction?: string;
    @IsOptional() @IsString() nationality?: string;
    @IsOptional() @IsString() previousSchoolName?: string;

    // Transfer Certificate
    @IsOptional() @IsBoolean() tcIssued?: boolean;
    @IsOptional() @IsBoolean() clearedAllDues?: boolean;
    @IsOptional() @IsString() tcNumber?: string;

    // Physical
    @IsOptional() @IsString() bloodGroup?: string;
    @IsOptional() @IsString() heightCm?: string;
    @IsOptional() @IsString() weightKg?: string;

    // Medical
    @IsOptional() @IsString() medicalConditions?: string;
    @IsOptional() @IsBoolean() hasAllergies?: boolean;
    @IsOptional() @IsString() allergyDetails?: string;
    @IsOptional() @IsString() emergencyMedicalInstructions?: string;

    // Guardian & Contact
    @IsOptional() @IsString() guardianName?: string;
    @IsOptional() @IsString() guardianRelation?: string;
    @IsOptional() @IsString() phone?: string;
    @IsOptional() @IsString() whatsapp?: string;
    @IsOptional() @IsString() address?: string;

    // Father
    @IsOptional() @IsString() fatherName?: string;
    @IsOptional() @IsString() fatherEmail?: string;
    @IsOptional() @IsString() fatherOccupation?: string;
    @IsOptional() @IsString() fatherMonthlyIncome?: string;
    @IsOptional() @IsString() fatherPhone?: string;
    @IsOptional() @IsString() fatherWhatsapp?: string;

    // Mother
    @IsOptional() @IsString() motherName?: string;
    @IsOptional() @IsString() motherEmail?: string;
    @IsOptional() @IsString() motherOccupation?: string;
    @IsOptional() @IsString() motherMonthlyIncome?: string;
    @IsOptional() @IsString() motherPhone?: string;
    @IsOptional() @IsString() motherWhatsapp?: string;

    // Transport
    @IsOptional() @IsBoolean() transportRequired?: boolean;
    @IsOptional() @IsString() routePickupPoint?: string;

    // Status
    @IsOptional() @IsBoolean() isActive?: boolean;

    // Exit Details
    @IsOptional() @IsDateString() leavingDate?: string;
    @IsOptional() @IsString() leavingReason?: string;

    // Document URLs (from Cloudinary)
    @IsOptional() @IsString() photoUrl?: string;
    @IsOptional() @IsString() fatherPhotoUrl?: string;
    @IsOptional() @IsString() motherPhotoUrl?: string;
    @IsOptional() @IsString() birthCertificateUrl?: string;
    @IsOptional() @IsString() scstCertificateUrl?: string;
    @IsOptional() @IsString() leavingCertificateUrl?: string;
    @IsOptional() @IsString() vaccineCardUrl?: string;

    // Custom Documents
    @IsOptional()
    @IsArray()
    @ValidateNested({ each: true })
    @Type(() => CustomDocumentDto)
    customDocuments?: CustomDocumentDto[];

    // Custom Images
    @IsOptional()
    @IsArray()
    @ValidateNested({ each: true })
    @Type(() => CustomImageDto)
    customImages?: CustomImageDto[];

    // Education History
    @IsOptional()
    @IsArray()
    @ValidateNested({ each: true })
    @Type(() => EducationEntryDto)
    education?: EducationEntryDto[];
}
