import mongoose from 'mongoose';

const nutritionTargetSchema = new mongoose.Schema(
  {
    userId: { type: String, required: true },
    householdId: { type: String, required: true },
    dailyCalories: { type: Number, required: true, min: 0 },
    proteinG: { type: Number, required: true, min: 0 },
    carbsG: { type: Number, required: true, min: 0 },
    fatG: { type: Number, required: true, min: 0 },
    fiberG: { type: Number, min: 0 },
    sodiumMg: { type: Number, min: 0 },
    sugarG: { type: Number, min: 0 },
    isActive: { type: Boolean, required: true, default: true },
  },
  {
    timestamps: true,
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  },
);

// One active target per user per household
nutritionTargetSchema.index({ userId: 1, householdId: 1, isActive: 1 });

export const NutritionTargetModel = mongoose.model('NutritionTarget', nutritionTargetSchema);
export type NutritionTargetDocument = mongoose.InferSchemaType<typeof nutritionTargetSchema> & {
  _id: mongoose.Types.ObjectId;
};
