import { generateObject } from "ai";
import { z } from "zod";
import { resolveModel, type AiConfig } from "../factory";

const NutritionSchema = z.object({
  perServing: z.object({
    calories: z.number().describe("Total calories per serving (kcal)"),
    proteinG: z.number().describe("Protein in grams per serving"),
    carbsG: z.number().describe("Total carbohydrates in grams per serving"),
    fatG: z.number().describe("Total fat in grams per serving"),
    fiberG: z.number().describe("Dietary fiber in grams per serving"),
    sodiumMg: z.number().describe("Sodium in milligrams per serving"),
  }),
});

export type NutritionEstimate = z.infer<typeof NutritionSchema>;

export async function estimateNutrition(
  recipe: {
    title: string;
    baseServings: number;
    ingredients: Array<{
      rawName: string;
      quantity?: string | number | null;
      unit?: string | null;
    }>;
  },
  config?: AiConfig
): Promise<NutritionEstimate> {
  const model = resolveModel(config);

  const ingredientList = recipe.ingredients
    .map((i) => {
      const parts: string[] = [];
      if (i.quantity) parts.push(String(i.quantity));
      if (i.unit) parts.push(i.unit);
      parts.push(i.rawName);
      return `- ${parts.join(" ")}`;
    })
    .join("\n");

  const { object } = await generateObject({
    model,
    schema: NutritionSchema,
    system:
      "You are an expert nutritionist with deep knowledge of food composition and nutritional values. Estimate nutrition data accurately based on ingredients, quantities, and typical cooking methods. When quantities are not specified, use typical serving amounts. Provide realistic estimates based on standard nutritional databases.",
    prompt: `Estimate the nutritional content per serving for the following recipe.

Recipe: ${recipe.title}
Servings: ${recipe.baseServings}

Ingredients:
${ingredientList}

Calculate nutrition values per single serving (total divided by ${recipe.baseServings} servings).`,
  });

  return object;
}
