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

export interface IVitalSign {
  date: Date;
  height?: number; // cm
  weight?: number; // kg
  bloodPressure?: string;
  heartRate?: number;
  temperature?: number;
  respiratoryRate?: number;
  oxygenSaturation?: number;
  notes?: string;
}

export interface ITestResult {
  date: Date;
  type: string; // Lab, Imaging, etc.
  name: string;
  result: string;
  fileUrl?: string;
  notes?: string;
}

export interface IPhysicalExam {
  date: Date;
  findings: string;
  notes?: string;
}

export interface IDiagnosis {
  date: Date;
  code: string;
  description: string;
  type: 'ICD-10' | 'CDT' | 'Custom';
  status: 'Active' | 'Resolved' | 'Chronic';
}

export interface ITreatmentPlan {
  date: Date;
  description: string;
  status: 'Proposed' | 'Accepted' | 'Completed' | 'Declined';
  costEstimate?: number;
}

export interface IDocument {
  date: Date;
  name: string;
  type: string; // JPG, PDF
  url: string;
  category?: string;
}

export interface IPatient extends Document {
  userId: Types.ObjectId; // Reference to User collection
  doctorId: Types.ObjectId; // Reference to Doctor collection - REQUIRED
  dateOfBirth?: Date;
  gender?: 'Male' | 'Female' | 'Other';
  address?: string;
  medicalHistory: string; // General history notes
  vitalSigns: IVitalSign[];
  testResults: ITestResult[];
  physicalExams: IPhysicalExam[];
  diagnoses: IDiagnosis[];
  treatmentPlans: ITreatmentPlan[];
  documents: IDocument[]; // Attached exams/files
  clinicalRecords: Types.ObjectId[]; // References to ClinicalRecord
  dentalRecords: Types.ObjectId[]; // References to DentalRecord
  occupation?: string;
  educationLevel?: string;
  maritalStatus?: string;
  emergencyContact?: {
    name: string;
    phone: string;
    relation: string;
  };
  insurance?: {
    provider: string;
    policyNumber: string;
    expirationDate?: Date;
  };
  nationalId?: string;
  dentalChart?: Map<string, any>; // JSON structure for teeth status
  createdAt: Date;
  updatedAt: Date;
}

const vitalSignSchema = new Schema({
  date: { type: Date, default: Date.now },
  height: Number,
  weight: Number,
  bloodPressure: String,
  heartRate: Number,
  temperature: Number,
  respiratoryRate: Number,
  oxygenSaturation: Number,
  notes: String
});

const testResultSchema = new Schema({
  date: { type: Date, default: Date.now },
  type: String,
  name: String,
  result: String,
  fileUrl: String,
  notes: String
});

const physicalExamSchema = new Schema({
  date: { type: Date, default: Date.now },
  findings: String,
  notes: String
});

const diagnosisSchema = new Schema({
  date: { type: Date, default: Date.now },
  code: String,
  description: String,
  type: { type: String, enum: ['ICD-10', 'CDT', 'Custom'], default: 'Custom' },
  status: { type: String, enum: ['Active', 'Resolved', 'Chronic'], default: 'Active' }
});

const treatmentPlanSchema = new Schema({
  date: { type: Date, default: Date.now },
  description: String,
  status: { type: String, enum: ['Proposed', 'Accepted', 'Completed', 'Declined'], default: 'Proposed' },
  costEstimate: Number
});

const documentSchema = new Schema({
  date: { type: Date, default: Date.now },
  name: String,
  type: String,
  url: String,
  category: String
});

const patientSchema = new Schema<IPatient>({
  userId: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true,
    unique: true
  },
  doctorId: {
    type: Schema.Types.ObjectId,
    ref: 'Doctor',
    required: true
  },
  dateOfBirth: Date,
  gender: { type: String, enum: ['Male', 'Female', 'Other'] },
  address: String,
  medicalHistory: String,
  vitalSigns: [vitalSignSchema],
  testResults: [testResultSchema],
  physicalExams: [physicalExamSchema],
  diagnoses: [diagnosisSchema],
  treatmentPlans: [treatmentPlanSchema],
  documents: [documentSchema],
  clinicalRecords: [{ type: Schema.Types.ObjectId, ref: 'ClinicalRecord' }],
  dentalRecords: [{ type: Schema.Types.ObjectId, ref: 'DentalRecord' }],
  occupation: String,
  educationLevel: String,
  maritalStatus: String,
  emergencyContact: {
    name: String,
    phone: String,
    relation: String
  },
  insurance: {
    provider: String,
    policyNumber: String,
    expirationDate: Date
  },
  nationalId: String,
  dentalChart: { type: Map, of: Schema.Types.Mixed, default: {} },
  createdAt: { type: Date, default: Date.now },
  updatedAt: { type: Date, default: Date.now }
}, {
  timestamps: true // This manages createdAt and updatedAt automatically if configured, explicitly kept here to match existing style but adding timestamps option is good practice
});

export default mongoose.model<IPatient>('Patient', patientSchema);
