/**
 * Recipe Templates
 * Common bakery recipes for quick setup
 */

import { MeasurementUnit } from '../../../../api/types/recipes';

export interface RecipeIngredientTemplate {
  ingredientName: string; // Name to match against user's ingredients
  quantity: number;
  unit: MeasurementUnit;
  alternatives?: string[]; // Alternative ingredient names
}

export interface RecipeTemplate {
  id: string;
  name: string;
  category: string;
  description: string;
  difficulty: 1 | 2 | 3 | 4 | 5;
  yieldQuantity: number;
  yieldUnit: MeasurementUnit;
  prepTime?: number;
  cookTime?: number;
  totalTime?: number;
  ingredients: RecipeIngredientTemplate[];
  instructions?: string;
  instructionsKey?: string; // Translation key for instructions
  tips?: string[];
  tipsKeys?: string[]; // Translation keys for tips
}

/**
 * Essential bread recipes
 */
export const BREAD_RECIPES: RecipeTemplate[] = [
  {
    id: 'baguette',
    name: 'Baguette Francesa',
    category: 'Panes',
    description: 'Classic French baguette with crispy crust',
    difficulty: 3,
    yieldQuantity: 4,
    yieldUnit: MeasurementUnit.PIECES,
    prepTime: 30,
    cookTime: 25,
    totalTime: 240, // Including proofing time
    ingredients: [
      { ingredientName: 'Harina 000', quantity: 500, unit: MeasurementUnit.GRAMS, alternatives: ['Harina', 'Flour'] },
      { ingredientName: 'Agua', quantity: 350, unit: MeasurementUnit.MILLILITERS, alternatives: ['Water'] },
      { ingredientName: 'Levadura Fresca', quantity: 10, unit: MeasurementUnit.GRAMS, alternatives: ['Levadura', 'Yeast'] },
      { ingredientName: 'Sal Fina', quantity: 10, unit: MeasurementUnit.GRAMS, alternatives: ['Sal', 'Salt'] },
    ],
    instructions: '1. Mix flour, water, yeast, and salt\n2. Knead for 10 minutes\n3. First rise: 1 hour\n4. Shape into baguettes\n5. Second rise: 45 minutes\n6. Score and bake at 230°C for 25 minutes',
    tips: [
      'Use steam in the oven for a crispier crust',
      'Score the dough just before baking',
      'Let cool completely before cutting',
    ],
  },
  {
    id: 'pan-sandwich',
    name: 'Pan de Molde (Sandwich)',
    category: 'Panes',
    description: 'Soft sandwich bread for daily use',
    difficulty: 2,
    yieldQuantity: 1,
    yieldUnit: MeasurementUnit.PIECES,
    prepTime: 20,
    cookTime: 35,
    totalTime: 180,
    ingredients: [
      { ingredientName: 'Harina 000', quantity: 500, unit: MeasurementUnit.GRAMS },
      { ingredientName: 'Leche Entera', quantity: 250, unit: MeasurementUnit.MILLILITERS, alternatives: ['Leche', 'Milk'] },
      { ingredientName: 'Manteca', quantity: 50, unit: MeasurementUnit.GRAMS, alternatives: ['Butter'] },
      { ingredientName: 'Azúcar', quantity: 30, unit: MeasurementUnit.GRAMS, alternatives: ['Sugar'] },
      { ingredientName: 'Levadura Fresca', quantity: 15, unit: MeasurementUnit.GRAMS },
      { ingredientName: 'Sal Fina', quantity: 8, unit: MeasurementUnit.GRAMS },
    ],
    instructions: '1. Warm milk and dissolve yeast\n2. Mix all ingredients\n3. Knead until smooth\n4. First rise: 1 hour\n5. Shape and place in loaf pan\n6. Second rise: 45 minutes\n7. Bake at 180°C for 35 minutes',
    tips: [
      'Brush with butter after baking for softer crust',
      'Let cool in pan for 10 minutes before removing',
    ],
  },
];

/**
 * Pastry recipes
 */
export const PASTRY_RECIPES: RecipeTemplate[] = [
  {
    id: 'medialunas',
    name: 'Medialunas de Manteca',
    category: 'Facturas',
    description: 'Traditional Argentine croissants',
    difficulty: 4,
    yieldQuantity: 12,
    yieldUnit: MeasurementUnit.PIECES,
    prepTime: 45,
    cookTime: 15,
    totalTime: 360,
    ingredients: [
      { ingredientName: 'Harina 0000', quantity: 500, unit: MeasurementUnit.GRAMS },
      { ingredientName: 'Leche Entera', quantity: 200, unit: MeasurementUnit.MILLILITERS },
      { ingredientName: 'Manteca', quantity: 150, unit: MeasurementUnit.GRAMS },
      { ingredientName: 'Azúcar', quantity: 80, unit: MeasurementUnit.GRAMS },
      { ingredientName: 'Huevos', quantity: 2, unit: MeasurementUnit.UNITS },
      { ingredientName: 'Levadura Fresca', quantity: 25, unit: MeasurementUnit.GRAMS },
      { ingredientName: 'Sal Fina', quantity: 8, unit: MeasurementUnit.GRAMS },
    ],
    instructions: '1. Make dough with flour, milk, yeast, eggs, and salt\n2. Laminate with butter (3 folds)\n3. Rest in refrigerator between folds\n4. Roll and cut into triangles\n5. Shape into crescents\n6. Proof for 1 hour\n7. Brush with egg wash\n8. Bake at 200°C for 15 minutes',
    tips: [
      'Keep butter cold during lamination',
      'Brush with sugar syrup while hot for glossy finish',
    ],
  },
  {
    id: 'facturas-simple',
    name: 'Facturas Simples',
    category: 'Facturas',
    description: 'Simple sweet pastries',
    difficulty: 2,
    yieldQuantity: 15,
    yieldUnit: MeasurementUnit.PIECES,
    prepTime: 25,
    cookTime: 12,
    totalTime: 150,
    ingredients: [
      { ingredientName: 'Harina 0000', quantity: 500, unit: MeasurementUnit.GRAMS },
      { ingredientName: 'Leche Entera', quantity: 250, unit: MeasurementUnit.MILLILITERS },
      { ingredientName: 'Manteca', quantity: 100, unit: MeasurementUnit.GRAMS },
      { ingredientName: 'Azúcar', quantity: 100, unit: MeasurementUnit.GRAMS },
      { ingredientName: 'Huevos', quantity: 2, unit: MeasurementUnit.UNITS },
      { ingredientName: 'Levadura Fresca', quantity: 20, unit: MeasurementUnit.GRAMS },
    ],
    instructions: '1. Mix all ingredients to form soft dough\n2. Knead for 8 minutes\n3. Rest for 15 minutes\n4. Roll and cut shapes\n5. Proof for 1 hour\n6. Brush with egg wash\n7. Bake at 190°C for 12 minutes',
  },
];

/**
 * Cake recipes
 */
export const CAKE_RECIPES: RecipeTemplate[] = [
  {
    id: 'bizcochuelo',
    name: 'Bizcochuelo Clásico',
    category: 'Tortas',
    description: 'Classic sponge cake',
    difficulty: 2,
    yieldQuantity: 1,
    yieldUnit: MeasurementUnit.PIECES,
    prepTime: 20,
    cookTime: 35,
    totalTime: 55,
    ingredients: [
      { ingredientName: 'Harina 0000', quantity: 200, unit: MeasurementUnit.GRAMS },
      { ingredientName: 'Azúcar', quantity: 200, unit: MeasurementUnit.GRAMS },
      { ingredientName: 'Huevos', quantity: 4, unit: MeasurementUnit.UNITS },
      { ingredientName: 'Esencia de Vainilla', quantity: 5, unit: MeasurementUnit.MILLILITERS, alternatives: ['Vanilla'] },
      { ingredientName: 'Polvo de Hornear', quantity: 10, unit: MeasurementUnit.GRAMS, alternatives: ['Baking Powder'] },
    ],
    instructions: '1. Beat eggs with sugar until fluffy\n2. Add vanilla\n3. Gently fold in flour and baking powder\n4. Pour into greased pan\n5. Bake at 180°C for 35 minutes',
    instructionsKey: 'recipe_templates:bizcochuelo.instructions',
    tips: [
      'Do not overmix after adding flour',
      'Test doneness with toothpick',
    ],
    tipsKeys: [
      'recipe_templates:bizcochuelo.tip1',
      'recipe_templates:bizcochuelo.tip2',
    ],
  },
];

/**
 * Cookie recipes
 */
export const COOKIE_RECIPES: RecipeTemplate[] = [
  {
    id: 'galletas-manteca',
    name: 'Galletas de Manteca',
    category: 'Galletitas',
    description: 'Butter cookies',
    difficulty: 1,
    yieldQuantity: 30,
    yieldUnit: MeasurementUnit.PIECES,
    prepTime: 15,
    cookTime: 12,
    totalTime: 27,
    ingredients: [
      { ingredientName: 'Harina 0000', quantity: 300, unit: MeasurementUnit.GRAMS },
      { ingredientName: 'Manteca', quantity: 150, unit: MeasurementUnit.GRAMS },
      { ingredientName: 'Azúcar', quantity: 100, unit: MeasurementUnit.GRAMS },
      { ingredientName: 'Huevos', quantity: 1, unit: MeasurementUnit.UNITS },
      { ingredientName: 'Esencia de Vainilla', quantity: 5, unit: MeasurementUnit.MILLILITERS },
    ],
    instructions: '1. Cream butter and sugar\n2. Add egg and vanilla\n3. Mix in flour\n4. Roll and cut shapes\n5. Bake at 180°C for 12 minutes',
  },
];

/**
 * Get all templates grouped by category
 */
export const getAllRecipeTemplates = (): Record<string, RecipeTemplate[]> => {
  return {
    breads: BREAD_RECIPES,
    pastries: PASTRY_RECIPES,
    cakes: CAKE_RECIPES,
    cookies: COOKIE_RECIPES,
  };
};

/**
 * Get templates for a specific bakery type
 */
export const getRecipeTemplatesForBakeryType = (bakeryType: string): RecipeTemplate[] => {
  switch (bakeryType.toLowerCase()) {
    case 'artisan':
    case 'artisanal':
      return [...BREAD_RECIPES];

    case 'pastry':
    case 'pasteleria':
      return [...PASTRY_RECIPES, ...CAKE_RECIPES];

    case 'traditional':
    case 'tradicional':
      return [...BREAD_RECIPES, ...PASTRY_RECIPES.slice(0, 1)];

    default:
      return [...BREAD_RECIPES.slice(0, 1), ...PASTRY_RECIPES.slice(0, 1)];
  }
};

/**
 * Match recipe ingredient template to actual ingredient ID
 */
export const matchIngredientToTemplate = (
  templateIngredient: RecipeIngredientTemplate,
  availableIngredients: Array<{ id: string; name: string }>
): string | null => {
  const searchNames = [
    templateIngredient.ingredientName.toLowerCase(),
    ...(templateIngredient.alternatives?.map(a => a.toLowerCase()) || []),
  ];

  for (const ingredient of availableIngredients) {
    const ingredientNameLower = ingredient.name.toLowerCase();
    for (const searchName of searchNames) {
      if (
        ingredientNameLower.includes(searchName) ||
        searchName.includes(ingredientNameLower)
      ) {
        return ingredient.id;
      }
    }
  }

  return null;
};
