import mongoose from 'mongoose';
import { StorageLocation, FreshnessRuleSource, ProductCategory } from '@meshitrack/shared';

const freshnessRuleSchema = new mongoose.Schema(
  {
    householdId: { type: String },
    category: { type: String, enum: Object.values(ProductCategory), required: true },
    storageLocation: { type: String, enum: Object.values(StorageLocation), required: true },
    shelfLifeDays: { type: Number, required: true, min: 1 },
    openedLifeDays: { type: Number, required: true, min: 1 },
    freezerLifeDays: { type: Number, min: 1 },
    spoilageSignsToCheck: { type: [String], default: [] },
    tips: { type: String },
    source: {
      type: String,
      enum: Object.values(FreshnessRuleSource),
      default: FreshnessRuleSource.HOUSEHOLD,
      required: true,
    },
  },
  {
    timestamps: true,
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  },
);

freshnessRuleSchema.index({ category: 1, storageLocation: 1, householdId: 1 }, { unique: true });
freshnessRuleSchema.index({ householdId: 1 });

export const FreshnessRuleModel = mongoose.model('FreshnessRule', freshnessRuleSchema);
export type FreshnessRuleDocument = mongoose.InferSchemaType<typeof freshnessRuleSchema> & {
  _id: mongoose.Types.ObjectId;
};
