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

export interface IDentalRecord extends Document {
    patientId: Types.ObjectId;
    doctorId: Types.ObjectId;
    date: Date;
    dentalHistory: {
        lossOfTeeth: boolean;
        causeOfLoss: string;
        previousDentalTreatment: boolean;
        treatmentExplanation: string;
        brushingFrequency: string;
        bleedingGums: boolean;
        badBreath: boolean;
        foodImpaction: boolean;
        mouthRinse: boolean;
        dentalFloss: boolean;
        anesthesiaProblems: boolean;
        dentalTrauma: string;
        extraoralExamNote: string;
    };
    extraoralExamination: {
        head: string;
        face: string;
        neck: string;
        eyes: string;
        ears: string;
        nose: string;
        tmj: string;
        skin: string;
        lips: string;
        other: string;
    };
    intraoralExamination: {
        hardPalate: string;
        softPalate: string;
        cheeks: string;
        tongue: string;
        floorOfMouth: string;
        oralMucosa: string;
        gums: string;
        frenula: string;
        oropharynx: string;
        salivaryGlands: string;
        residualRidge: string;
        occlusion: string;
    };
    procedures: {
        procedure: string;
        date?: Date;
        totalPaid: string;
    }[];
    odontogram: Map<string, { status: string; surfaces: { top: string; bottom: string; left: string; right: string; center: string }; notes: string; condition?: string; treatment?: string; }>; // tooth number as key (18, 17, etc.)
    odontogramNotes: string;
    radiographicAnalysis: string;
    treatmentPlan: string;
    createdAt: Date;
    updatedAt: Date;
}

const dentalRecordSchema = new Schema<IDentalRecord>({
    patientId: { type: Schema.Types.ObjectId, ref: 'Patient', required: true },
    doctorId: { type: Schema.Types.ObjectId, ref: 'Doctor' },
    date: { type: Date, default: Date.now },
    dentalHistory: {
        lossOfTeeth: { type: Boolean, default: false },
        causeOfLoss: String,
        previousDentalTreatment: { type: Boolean, default: false },
        treatmentExplanation: String,
        brushingFrequency: String,
        bleedingGums: { type: Boolean, default: false },
        badBreath: { type: Boolean, default: false },
        foodImpaction: { type: Boolean, default: false },
        mouthRinse: { type: Boolean, default: false },
        dentalFloss: { type: Boolean, default: false },
        anesthesiaProblems: { type: Boolean, default: false },
        dentalTrauma: String,
        extraoralExamNote: String
    },
    extraoralExamination: {
        head: String,
        face: String,
        neck: String,
        eyes: String,
        ears: String,
        nose: String,
        tmj: String,
        skin: String,
        lips: String,
        other: String
    },
    intraoralExamination: {
        hardPalate: String,
        softPalate: String,
        cheeks: String,
        tongue: String,
        floorOfMouth: String,
        oralMucosa: String,
        gums: String,
        frenula: String,
        oropharynx: String,
        salivaryGlands: String,
        residualRidge: String,
        occlusion: String
    },
    procedures: [{
        procedure: String,
        date: Date,
        totalPaid: String
    }],
    odontogram: {
        type: Map,
        of: {
            status: String,
            surfaces: {
                top: String,
                bottom: String,
                left: String,
                right: String,
                center: String,
            },
            notes: String,
            condition: String,
            treatment: String,
        },
        default: {}
    },
    odontogramNotes: String,
    radiographicAnalysis: String,
    treatmentPlan: String
}, {
    timestamps: true
});

export default mongoose.model<IDentalRecord>('DentalRecord', dentalRecordSchema);
