import mongoose from 'mongoose';
import {
  CabinetItemStatus,
  ConcentrationUnit,
  DosageUnit,
  MedicineForm,
  StrengthUnit,
} from '@meshitrack/shared';

const cabinetItemSchema = new mongoose.Schema(
  {
    householdId: { type: String, required: true },
    medicineId: { type: String, required: true },
    medicineName: { type: String, required: true },
    medicineStrength: { type: Number, required: true },
    medicineStrengthUnit: { type: String, enum: Object.values(StrengthUnit), required: true },
    medicineForm: { type: String, enum: Object.values(MedicineForm), required: true },
    medicineProductId: { type: String },
    medicineProductBrand: { type: String },
    concentration: { type: Number },
    concentrationUnit: { type: String, enum: Object.values(ConcentrationUnit) },
    quantity: { type: Number, required: true, min: 0 },
    unit: { type: String, enum: Object.values(DosageUnit), required: true },
    expirationDate: { type: Date },
    purchaseDate: { type: Date },
    unitPrice: { type: Number },
    totalPrice: { type: Number },
    currency: { type: String },
    storeId: { type: String },
    storeName: { type: String },
    status: {
      type: String,
      enum: Object.values(CabinetItemStatus),
      default: CabinetItemStatus.ACTIVE,
    },
    notes: { type: String },
    createdBy: { type: String, required: true },
    isDeleted: { type: Boolean, default: false },
  },
  {
    timestamps: true,
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  },
);

cabinetItemSchema.index({ householdId: 1, medicineId: 1, status: 1 });
cabinetItemSchema.index({ householdId: 1, status: 1 });
cabinetItemSchema.index({ householdId: 1, expirationDate: 1 });

export const CabinetItemModel = mongoose.model('CabinetItem', cabinetItemSchema);
export type CabinetItemDocument = mongoose.InferSchemaType<typeof cabinetItemSchema> & {
  _id: mongoose.Types.ObjectId;
};
