import mongoose, { Document, Schema, Types } from 'mongoose';

export interface IClinicalRecord extends Document {
    patientId: Types.ObjectId;
    doctorId: Types.ObjectId;
    date: Date;
    title?: string;
    // Backward compatibility
    reasonForConsultation?: string;

    // New Fields
    chiefComplaint?: string;
    historyOfPresentIllness?: string;

    // Merged/Expanded Medical History
    pastMedicalHistory: {
        // Original fields (kept optional)
        underMedicalTreatment?: boolean;
        takingMedication?: boolean;
        medicationDetails?: string;
        previousDentalTreatmentProblems?: boolean;
        problemsDetails?: string;
        immunizationsUpToDate?: boolean;
        womenOnly?: {
            pregnant: boolean;
            breastfeeding: boolean;
            oralContraceptives: boolean;
        };

        // New fields matching the form
        pathological?: string;
        medications?: string; // Overlaps with takingMedication/medicationDetails logically but kept separate for form
        allergies?: string;
        surgeries?: string;
        trauma?: string;
        toxicHabits?: string;
        occupation?: string;
        immunizations?: string;
        familyHistory?: string;
        gynecological?: string;
    };

    reviewOfSystems?: string;

    physicalExamination?: {
        skinAndMucosa?: string;
        headAndNeck?: string;
        chest?: string;
        heart?: string;
        lungs?: string;
        abdomenAndPelvis?: string;
        upperExtremities?: string;
        lowerExtremities?: string;
        neurological?: string;
    };

    vitalSigns?: {
        bp?: string;
        hr?: string;
        rr?: string;
        temp?: string;
        spo2?: string;
    };

    complementaryStudies?: string;
    files?: string[];
    diagnoses?: string[];
    plan?: string;

    // Original fields kept for backward compatibility or if needed
    habits?: {
        smoker: boolean;
        nailBiting: boolean;
        lipBiting: boolean;
        thumbSucking: boolean;
        mouthBreather: boolean;
        atypicalSwallowing: boolean;
        bruxism: boolean;
        other?: string;
    };
    patientAttitude?: {
        cooperative: boolean;
        apprehensive: boolean;
        fearful: boolean;
        other: string;
    };
    comments?: string;

    createdAt: Date;
    updatedAt: Date;
}

const clinicalRecordSchema = new Schema<IClinicalRecord>({
    patientId: { type: Schema.Types.ObjectId, ref: 'Patient', required: true },
    doctorId: { type: Schema.Types.ObjectId, ref: 'Doctor' },
    date: { type: Date, default: Date.now },
    title: String,

    reasonForConsultation: String,
    chiefComplaint: String,
    historyOfPresentIllness: String,

    pastMedicalHistory: {
        underMedicalTreatment: { type: Boolean, default: false },
        takingMedication: { type: Boolean, default: false },
        medicationDetails: String,
        previousDentalTreatmentProblems: { type: Boolean, default: false },
        problemsDetails: String,
        immunizationsUpToDate: { type: Boolean, default: false },
        womenOnly: {
            pregnant: { type: Boolean, default: false },
            breastfeeding: { type: Boolean, default: false },
            oralContraceptives: { type: Boolean, default: false }
        },
        pathological: String,
        medications: String,
        allergies: String,
        surgeries: String,
        trauma: String,
        toxicHabits: String,
        occupation: String,
        immunizations: String,
        familyHistory: String,
        gynecological: String
    },

    reviewOfSystems: String,

    physicalExamination: {
        skinAndMucosa: String,
        headAndNeck: String,
        chest: String,
        heart: String,
        lungs: String,
        abdomenAndPelvis: String,
        upperExtremities: String,
        lowerExtremities: String,
        neurological: String
    },

    vitalSigns: {
        bp: String,
        hr: String,
        rr: String,
        temp: String,
        spo2: String
    },

    complementaryStudies: String,
    files: [String],
    diagnoses: [String],
    plan: String,

    habits: {
        smoker: { type: Boolean, default: false },
        nailBiting: { type: Boolean, default: false },
        lipBiting: { type: Boolean, default: false },
        thumbSucking: { type: Boolean, default: false },
        mouthBreather: { type: Boolean, default: false },
        atypicalSwallowing: { type: Boolean, default: false },
        bruxism: { type: Boolean, default: false },
        other: String
    },
    patientAttitude: {
        cooperative: { type: Boolean, default: false },
        apprehensive: { type: Boolean, default: false },
        fearful: { type: Boolean, default: false },
        other: String
    },
    comments: String
}, {
    timestamps: true
});

export default mongoose.model<IClinicalRecord>('ClinicalRecord', clinicalRecordSchema);
