import { type Food } from './food';
import { type MenuCategory } from './types';

function mapTagToCategory(tag: string): string {
  switch (tag) {
    case 'Appetizer':
      return 'Appetizers';
    case 'Breakfast':
    case 'Lunch':
    case 'Dinner':
      return 'Mains';
    case 'Dessert':
      return 'Desserts';
    case 'Drink':
      return 'Drinks';
    default:
      return 'Mains';
  }
}

function mapTagToDietary(tag: string): string | null {
  switch (tag) {
    case 'Vegetarian':
      return 'vegetarian';
    case 'Spicy':
      return 'spicy';
    case 'Alcoholic':
      return 'alcoholic';
    default:
      return null;
  }
}

export function convertFoodToMenuData(foods: Food[]): MenuCategory[] {
  const categoryMap = new Map<string, MenuCategory>();

  for (const food of foods) {
    const category = food.tags.find(tag => 
      ['Appetizer', 'Breakfast', 'Lunch', 'Dinner', 'Dessert', 'Drink'].includes(tag)
    ) || 'Dinner';
    
    const menuCategory = mapTagToCategory(category);
    
    const dietary = food.tags
      .map((tag: string) => mapTagToDietary(tag))
      .filter((tag): tag is string => tag !== null);

    if (!categoryMap.has(menuCategory)) {
      categoryMap.set(menuCategory, {
        name: menuCategory,
        items: []
      });
    }

    const menuItem = {
      id: `food-${food.id}`,
      name: food.name,
      description: food.description,
      price: food.price,
      category: menuCategory,
      image: food.image,
      dietary: dietary,
      allergens: []
    };

    categoryMap.get(menuCategory)!.items.push(menuItem);
  }

  return Array.from(categoryMap.values());
}
