/**
 * Ingredient Starter Templates
 * Common bakery ingredients for quick setup
 */

import { UnitOfMeasure, IngredientCategory } from '../../../../api/types/inventory';
import type { IngredientCreate } from '../../../../api/types/inventory';

export interface IngredientTemplate {
  id: string;
  name: string;
  category: IngredientCategory;
  unit_of_measure: UnitOfMeasure;
  description?: string;
  estimatedCost?: number;
  typical_suppliers?: string[];
}

/**
 * Essential ingredients that every bakery needs
 */
export const ESSENTIAL_INGREDIENTS: IngredientTemplate[] = [
  // Flours
  {
    id: 'flour-000',
    name: 'Harina 000',
    category: IngredientCategory.FLOUR,
    unit_of_measure: UnitOfMeasure.KILOGRAMS,
    description: 'All-purpose flour for general baking',
    estimatedCost: 1.50,
  },
  {
    id: 'flour-0000',
    name: 'Harina 0000',
    category: IngredientCategory.FLOUR,
    unit_of_measure: UnitOfMeasure.KILOGRAMS,
    description: 'Soft flour for pastries and cakes',
    estimatedCost: 1.80,
  },
  {
    id: 'flour-integral',
    name: 'Harina Integral',
    category: IngredientCategory.FLOUR,
    unit_of_measure: UnitOfMeasure.KILOGRAMS,
    description: 'Whole wheat flour',
    estimatedCost: 2.20,
  },

  // Leavening agents
  {
    id: 'yeast-fresh',
    name: 'Levadura Fresca',
    category: IngredientCategory.YEAST,
    unit_of_measure: UnitOfMeasure.GRAMS,
    description: 'Fresh baker\'s yeast',
    estimatedCost: 0.50,
  },
  {
    id: 'yeast-dry',
    name: 'Levadura Seca',
    category: IngredientCategory.YEAST,
    unit_of_measure: UnitOfMeasure.GRAMS,
    description: 'Dry active yeast',
    estimatedCost: 0.80,
  },

  // Dairy
  {
    id: 'milk',
    name: 'Leche Entera',
    category: IngredientCategory.DAIRY,
    unit_of_measure: UnitOfMeasure.LITERS,
    description: 'Whole milk',
    estimatedCost: 1.20,
  },
  {
    id: 'butter',
    name: 'Manteca',
    category: IngredientCategory.FATS,
    unit_of_measure: UnitOfMeasure.KILOGRAMS,
    description: 'Butter for baking',
    estimatedCost: 8.50,
  },

  // Eggs
  {
    id: 'eggs',
    name: 'Huevos',
    category: IngredientCategory.EGGS,
    unit_of_measure: UnitOfMeasure.UNITS,
    description: 'Fresh eggs',
    estimatedCost: 0.30,
  },

  // Sugar and sweeteners
  {
    id: 'sugar',
    name: 'Azúcar',
    category: IngredientCategory.SUGAR,
    unit_of_measure: UnitOfMeasure.KILOGRAMS,
    description: 'White sugar',
    estimatedCost: 1.50,
  },
  {
    id: 'sugar-impalpable',
    name: 'Azúcar Impalpable',
    category: IngredientCategory.SUGAR,
    unit_of_measure: UnitOfMeasure.KILOGRAMS,
    description: 'Powdered sugar',
    estimatedCost: 2.00,
  },

  // Salt
  {
    id: 'salt',
    name: 'Sal Fina',
    category: IngredientCategory.SALT,
    unit_of_measure: UnitOfMeasure.KILOGRAMS,
    description: 'Fine salt for baking',
    estimatedCost: 0.80,
  },

  // Oils and fats
  {
    id: 'oil',
    name: 'Aceite',
    category: IngredientCategory.FATS,
    unit_of_measure: UnitOfMeasure.LITERS,
    description: 'Vegetable oil',
    estimatedCost: 3.50,
  },
];

/**
 * Optional but commonly used ingredients
 */
export const COMMON_INGREDIENTS: IngredientTemplate[] = [
  // Spices and flavorings
  {
    id: 'vanilla',
    name: 'Esencia de Vainilla',
    category: IngredientCategory.SPICES,
    unit_of_measure: UnitOfMeasure.MILLILITERS,
    description: 'Vanilla extract',
    estimatedCost: 5.00,
  },
  {
    id: 'cinnamon',
    name: 'Canela Molida',
    category: IngredientCategory.SPICES,
    unit_of_measure: UnitOfMeasure.GRAMS,
    description: 'Ground cinnamon',
    estimatedCost: 3.50,
  },

  // Additional dairy
  {
    id: 'cream',
    name: 'Crema de Leche',
    category: IngredientCategory.DAIRY,
    unit_of_measure: UnitOfMeasure.LITERS,
    description: 'Heavy cream',
    estimatedCost: 4.50,
  },
  {
    id: 'cheese-cream',
    name: 'Queso Crema',
    category: IngredientCategory.DAIRY,
    unit_of_measure: UnitOfMeasure.KILOGRAMS,
    description: 'Cream cheese',
    estimatedCost: 6.00,
  },

  // Chocolate and cocoa
  {
    id: 'cocoa',
    name: 'Cacao en Polvo',
    category: IngredientCategory.ADDITIVES,
    unit_of_measure: UnitOfMeasure.KILOGRAMS,
    description: 'Cocoa powder',
    estimatedCost: 8.00,
  },
  {
    id: 'chocolate',
    name: 'Chocolate Cobertura',
    category: IngredientCategory.ADDITIVES,
    unit_of_measure: UnitOfMeasure.KILOGRAMS,
    description: 'Baking chocolate',
    estimatedCost: 12.00,
  },

  // Nuts and fruits
  {
    id: 'almonds',
    name: 'Almendras',
    category: IngredientCategory.ADDITIVES,
    unit_of_measure: UnitOfMeasure.KILOGRAMS,
    description: 'Almonds',
    estimatedCost: 15.00,
  },
  {
    id: 'raisins',
    name: 'Pasas de Uva',
    category: IngredientCategory.ADDITIVES,
    unit_of_measure: UnitOfMeasure.KILOGRAMS,
    description: 'Raisins',
    estimatedCost: 7.00,
  },

  // Baking powder
  {
    id: 'baking-powder',
    name: 'Polvo de Hornear',
    category: IngredientCategory.YEAST,
    unit_of_measure: UnitOfMeasure.GRAMS,
    description: 'Baking powder',
    estimatedCost: 2.50,
  },
];

/**
 * Packaging materials
 */
export const PACKAGING_ITEMS: IngredientTemplate[] = [
  {
    id: 'paper-bags',
    name: 'Bolsas de Papel',
    category: IngredientCategory.PACKAGING,
    unit_of_measure: UnitOfMeasure.UNITS,
    description: 'Paper bags for bread',
    estimatedCost: 0.15,
  },
  {
    id: 'plastic-bags',
    name: 'Bolsas Plásticas',
    category: IngredientCategory.PACKAGING,
    unit_of_measure: UnitOfMeasure.UNITS,
    description: 'Plastic bags',
    estimatedCost: 0.10,
  },
  {
    id: 'boxes',
    name: 'Cajas de Cartón',
    category: IngredientCategory.PACKAGING,
    unit_of_measure: UnitOfMeasure.UNITS,
    description: 'Cardboard boxes for cakes',
    estimatedCost: 1.50,
  },
];

/**
 * Get all templates grouped by category
 */
export const getAllTemplates = (): Record<string, IngredientTemplate[]> => {
  return {
    essential: ESSENTIAL_INGREDIENTS,
    common: COMMON_INGREDIENTS,
    packaging: PACKAGING_ITEMS,
  };
};

/**
 * Get templates for a specific bakery type
 */
export const getTemplatesForBakeryType = (bakeryType: string): IngredientTemplate[] => {
  const baseTemplates = ESSENTIAL_INGREDIENTS;

  switch (bakeryType.toLowerCase()) {
    case 'artisan':
    case 'artisanal':
      return [...baseTemplates, ...COMMON_INGREDIENTS.slice(0, 4)];

    case 'pastry':
    case 'pasteleria':
      return [
        ...baseTemplates,
        ...COMMON_INGREDIENTS.filter(i =>
          ['vanilla', 'cream', 'cheese-cream', 'cocoa', 'chocolate'].includes(i.id)
        ),
      ];

    case 'traditional':
    case 'tradicional':
      return [...baseTemplates];

    default:
      return [...baseTemplates, ...COMMON_INGREDIENTS.slice(0, 3)];
  }
};

/**
 * Convert template to IngredientCreate format
 */
export const templateToIngredientCreate = (
  template: IngredientTemplate,
  customName?: string
): Omit<IngredientCreate, 'tenant_id'> => {
  // Calculate realistic stock levels based on unit type
  const lowStock = 10;
  const reorderPoint = 20;
  const reorderQty = 50;
  const maxStock = reorderQty * 2; // 100 - always > 0

  return {
    name: customName || template.name,
    category: template.category,
    unit_of_measure: template.unit_of_measure,
    description: template.description,
    standard_cost: template.estimatedCost,
    low_stock_threshold: lowStock,
    max_stock_level: maxStock,  // Added: prevents validation error
    reorder_point: reorderPoint,
    reorder_quantity: reorderQty,
    is_perishable: [IngredientCategory.DAIRY, IngredientCategory.EGGS].includes(template.category),
  };
};
