import mongoose from 'mongoose';
import { ConcentrationUnit, DosageUnit, MedicineProductSource } from '@meshitrack/shared';

const medicineProductSchema = new mongoose.Schema(
  {
    householdId: { type: String, required: true },
    medicineId: { type: String, required: true },
    medicineName: { type: String, required: true },
    brand: { type: String, required: true },
    manufacturer: { type: String },
    packageSize: { type: Number, required: true },
    packageUnit: { type: String, enum: Object.values(DosageUnit), required: true },
    concentration: { type: Number },
    concentrationUnit: { type: String, enum: Object.values(ConcentrationUnit) },
    imageUrl: { type: String },
    notes: { type: String },
    source: {
      type: String,
      enum: Object.values(MedicineProductSource),
      default: MedicineProductSource.MANUAL,
    },
    createdBy: { type: String, required: true },
    isDeleted: { type: Boolean, default: false },
  },
  {
    timestamps: true,
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  },
);

medicineProductSchema.index({ householdId: 1, medicineId: 1 });
medicineProductSchema.index({ householdId: 1, brand: 'text' });

export const MedicineProductModel = mongoose.model('MedicineProduct', medicineProductSchema);
export type MedicineProductDocument = mongoose.InferSchemaType<typeof medicineProductSchema> & {
  _id: mongoose.Types.ObjectId;
};
