import mongoose from 'mongoose';
import { NutritionWarning } from '@meshitrack/shared';

const nutritionInfoSchema = new mongoose.Schema(
  {
    calories: { type: Number, required: true, min: 0 },
    protein: { type: Number, required: true, min: 0 },
    carbs: { type: Number, required: true, min: 0 },
    fat: { type: Number, required: true, min: 0 },
    fiber: { type: Number, min: 0 },
    sugar: { type: Number, min: 0 },
    sodium: { type: Number, min: 0 },
    saturatedFat: { type: Number, min: 0 },
    cholesterol: { type: Number, min: 0 },
  },
  { _id: false },
);

const recipeIngredientSchema = new mongoose.Schema(
  {
    productId: { type: String, required: true },
    productName: { type: String, required: true },
    quantity: { type: Number, required: true, min: 0 },
    unit: { type: String, enum: ['g', 'ml', 'piece', 'slice'], required: true },
    originalQuantity: { type: Number },
    originalUnit: { type: String },
    preparation: { type: String },
    isOptional: { type: Boolean, default: false },
    nutritionContribution: { type: nutritionInfoSchema, required: true },
  },
  { _id: false },
);

const recipeStepSchema = new mongoose.Schema(
  {
    order: { type: Number, required: true },
    instruction: { type: String, required: true },
    duration: { type: Number },
    tip: { type: String },
  },
  { _id: false },
);

const recipeSourceSchema = new mongoose.Schema(
  {
    type: { type: String, enum: ['manual', 'url', 'llm_import', 'text_import'], required: true },
    url: { type: String },
    importedAt: { type: Date },
  },
  { _id: false },
);

const recipeSchema = new mongoose.Schema(
  {
    householdId: { type: String, required: true },
    name: { type: String, required: true },
    description: { type: String },
    servings: { type: Number, required: true, min: 1 },
    prepTime: { type: Number },
    cookTime: { type: Number },
    totalTime: { type: Number },
    ingredients: { type: [recipeIngredientSchema], default: [] },
    steps: { type: [recipeStepSchema], default: [] },
    tags: { type: [String], default: [] },
    cuisine: { type: String },
    imageUrl: { type: String },
    source: { type: recipeSourceSchema },
    totalNutrition: { type: nutritionInfoSchema, required: true },
    perServingNutrition: { type: nutritionInfoSchema, required: true },
    warnings: { type: [String], enum: Object.values(NutritionWarning), default: [] },
    isFavorite: { type: Boolean, default: false },
    createdBy: { type: String, required: true },
    deletedAt: { type: Date },
  },
  {
    timestamps: true,
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  },
);

recipeSchema.index(
  { householdId: 1, name: 'text', tags: 'text', cuisine: 'text' },
  { name: 'recipe_text_search' },
);
recipeSchema.index({ householdId: 1, 'ingredients.productId': 1 });
recipeSchema.index({ householdId: 1, tags: 1 });
recipeSchema.index({ householdId: 1, isFavorite: 1 });
recipeSchema.index({ householdId: 1, deletedAt: 1 });

export const RecipeModel = mongoose.model('Recipe', recipeSchema);
export type RecipeDocument = mongoose.InferSchemaType<typeof recipeSchema> & {
  _id: mongoose.Types.ObjectId;
};
