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

export interface IMedication {
    name: string;
    dosage: string;
    frequency: string;
    duration: string;
    instructions?: string;
}

export interface IPrescription extends Document {
    patientId: mongoose.Types.ObjectId;
    doctorId: mongoose.Types.ObjectId;
    date: Date;
    documentType: 'Prescription' | 'Referral' | 'Certification' | 'Medical Leave' | 'Laboratory' | 'Imaging';
    diagnosis: string;
    diagnosisType: 'ICD-10' | 'CDT' | 'Custom';
    medications: IMedication[];
    freeText?: string;
    labTests?: string;
    images?: string;
    notes?: string;
    pdfUrl?: string; // URL to the generated PDF
    status: 'Draft' | 'Finalized' | 'Cancelled';
    createdAt: Date;
}

const medicationSchema = new Schema({
    name: { type: String, required: true },
    dosage: String,
    frequency: String,
    duration: String,
    instructions: String
});

const prescriptionSchema = new Schema<IPrescription>({
    patientId: { type: Schema.Types.ObjectId, ref: 'Patient', required: true },
    doctorId: { type: Schema.Types.ObjectId, ref: 'Doctor', required: true },
    date: { type: Date, default: Date.now },
    documentType: { type: String, enum: ['Prescription', 'Referral', 'Certification', 'Medical Leave', 'Laboratory', 'Imaging'], default: 'Prescription' },
    diagnosis: String,
    diagnosisType: { type: String, enum: ['ICD-10', 'CDT', 'Custom'], default: 'Custom' },
    medications: [medicationSchema],
    freeText: String,
    labTests: String,
    images: String,
    notes: String,
    pdfUrl: String,
    status: { type: String, enum: ['Draft', 'Finalized', 'Cancelled'], default: 'Draft' },
    createdAt: { type: Date, default: Date.now }
});

export default mongoose.model<IPrescription>('Prescription', prescriptionSchema);
