import mongoose from 'mongoose';

const priceRecordSchema = new mongoose.Schema(
  {
    householdId: { type: String, required: true },
    productId: { type: String, required: true },
    productName: { type: String, required: true },
    storeId: { type: String, required: true },
    storeName: { type: String, required: true },
    price: { type: Number, required: true },
    currency: { type: String, required: true },
    quantity: { type: Number, required: true },
    unit: { type: String, required: true },
    pricePerUnit: { type: Number, required: true },
    date: { type: Date, required: true },
    receiptImageUrl: { type: String },
    notes: { type: String },
    createdBy: { type: String, required: true },
  },
  { timestamps: { createdAt: true, updatedAt: false } },
);

// Performance Indexes for Lookup Speed and Aggregations
priceRecordSchema.index({ householdId: 1, productId: 1, storeId: 1, date: -1 });
priceRecordSchema.index({ householdId: 1, productId: 1, date: -1 });
priceRecordSchema.index({ householdId: 1, storeId: 1, date: -1 });

export const PriceRecordModel = mongoose.model('PriceRecord', priceRecordSchema);
export type PriceRecordDocument = mongoose.InferSchemaType<typeof priceRecordSchema> & {
  _id: mongoose.Types.ObjectId;
};
