<?php

namespace Database\Seeders;

use App\Models\Plan;
use App\Models\Meal;
use App\Models\Menu;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;
use Carbon\Carbon;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // --- Plans ---
        $plans = [
            [
                'name' => 'Daily Lite',
                'slug' => 'daily-lite',
                'description' => 'One fresh meal delivered to your door every day. Perfect for busy professionals.',
                'frequency' => 'daily',
                'meals_per_delivery' => 1,
                'deliveries_per_week' => 7,
                'price' => 89.99,
                'price_per_meal' => 12.86,
                'is_featured' => false,
                'features' => ['1 meal per day', 'Free delivery', 'Skip anytime', 'Cancel anytime'],
            ],
            [
                'name' => 'Daily Standard',
                'slug' => 'daily-standard',
                'description' => 'Two meals delivered daily — lunch and dinner covered. Our most popular plan.',
                'frequency' => 'daily',
                'meals_per_delivery' => 2,
                'deliveries_per_week' => 7,
                'price' => 149.99,
                'price_per_meal' => 10.71,
                'is_featured' => true,
                'features' => ['2 meals per day', 'Free delivery', 'Skip anytime', 'Cancel anytime', 'Priority support'],
            ],
            [
                'name' => 'Weekly Family',
                'slug' => 'weekly-family',
                'description' => 'Three weekly deliveries with 4 meals each — feed the whole family.',
                'frequency' => 'weekly',
                'meals_per_delivery' => 4,
                'deliveries_per_week' => 3,
                'price' => 119.99,
                'price_per_meal' => 10.00,
                'is_featured' => false,
                'features' => ['4 meals per delivery', '3 deliveries/week', 'Family portions', 'Free delivery', 'Cancel anytime'],
            ],
            [
                'name' => 'Weekly Essentials',
                'slug' => 'weekly-essentials',
                'description' => 'Two weekly deliveries with 3 meals each — balanced and affordable.',
                'frequency' => 'weekly',
                'meals_per_delivery' => 3,
                'deliveries_per_week' => 2,
                'price' => 69.99,
                'price_per_meal' => 11.67,
                'is_featured' => false,
                'features' => ['3 meals per delivery', '2 deliveries/week', 'Free delivery', 'Cancel anytime'],
            ],
        ];

        foreach ($plans as $plan) {
            Plan::create($plan);
        }

        // --- Meals ---
        $meals = [
            ['name' => 'Grilled Chicken Bowl', 'category' => 'lunch', 'calories' => 520, 'protein' => 42, 'carbs' => 45, 'fat' => 16, 'dietary_info' => ['high-protein', 'gluten-free'], 'description' => 'Tender grilled chicken breast over brown rice with roasted vegetables and chimichurri sauce.'],
            ['name' => 'Salmon & Quinoa', 'category' => 'dinner', 'calories' => 580, 'protein' => 38, 'carbs' => 42, 'fat' => 24, 'dietary_info' => ['high-protein', 'gluten-free'], 'description' => 'Pan-seared Atlantic salmon with lemon-herb quinoa and steamed broccoli.'],
            ['name' => 'Veggie Stir Fry', 'category' => 'lunch', 'calories' => 380, 'protein' => 14, 'carbs' => 52, 'fat' => 12, 'dietary_info' => ['vegetarian', 'vegan'], 'description' => 'Colorful vegetable stir fry with tofu, sesame ginger sauce over jasmine rice.'],
            ['name' => 'Turkey Meatballs', 'category' => 'dinner', 'calories' => 490, 'protein' => 36, 'carbs' => 48, 'fat' => 18, 'dietary_info' => ['high-protein'], 'description' => 'Lean turkey meatballs in marinara sauce with whole wheat penne and parmesan.'],
            ['name' => 'Overnight Oats', 'category' => 'breakfast', 'calories' => 340, 'protein' => 14, 'carbs' => 52, 'fat' => 10, 'dietary_info' => ['vegetarian'], 'description' => 'Creamy overnight oats with mixed berries, chia seeds, and honey.'],
            ['name' => 'Avocado Toast Plate', 'category' => 'breakfast', 'calories' => 420, 'protein' => 16, 'carbs' => 38, 'fat' => 24, 'dietary_info' => ['vegetarian'], 'description' => 'Sourdough toast with smashed avocado, poached eggs, cherry tomatoes, and microgreens.'],
            ['name' => 'Mediterranean Bowl', 'category' => 'lunch', 'calories' => 460, 'protein' => 22, 'carbs' => 48, 'fat' => 20, 'dietary_info' => ['vegetarian', 'gluten-free'], 'description' => 'Falafel, hummus, tabbouleh, pickled veggies, and tahini over mixed greens.'],
            ['name' => 'BBQ Pulled Pork', 'category' => 'dinner', 'calories' => 550, 'protein' => 34, 'carbs' => 52, 'fat' => 22, 'dietary_info' => ['high-protein'], 'description' => 'Slow-cooked BBQ pulled pork with coleslaw and sweet potato mash.'],
            ['name' => 'Acai Bowl', 'category' => 'breakfast', 'calories' => 310, 'protein' => 8, 'carbs' => 58, 'fat' => 8, 'dietary_info' => ['vegetarian', 'vegan', 'gluten-free'], 'description' => 'Thick acai blend topped with granola, fresh fruit, coconut flakes, and honey drizzle.'],
            ['name' => 'Teriyaki Beef Bowl', 'category' => 'dinner', 'calories' => 620, 'protein' => 40, 'carbs' => 58, 'fat' => 22, 'dietary_info' => ['high-protein'], 'description' => 'Tender teriyaki glazed beef strips with steamed rice, edamame, and pickled ginger.'],
            ['name' => 'Greek Yogurt Parfait', 'category' => 'snack', 'calories' => 220, 'protein' => 18, 'carbs' => 28, 'fat' => 6, 'dietary_info' => ['vegetarian', 'gluten-free'], 'description' => 'Thick Greek yogurt layered with granola, mixed berries, and a drizzle of honey.'],
            ['name' => 'Protein Energy Bites', 'category' => 'snack', 'calories' => 180, 'protein' => 10, 'carbs' => 22, 'fat' => 8, 'dietary_info' => ['vegetarian', 'gluten-free'], 'description' => 'No-bake energy bites with oats, peanut butter, dark chocolate chips, and whey protein.'],
        ];

        foreach ($meals as $meal) {
            Meal::create(array_merge($meal, [
                'slug' => Str::slug($meal['name']),
                'is_available' => true,
                'tags' => $meal['dietary_info'],
                'allergens' => [],
                'prep_time_minutes' => rand(15, 45),
            ]));
        }

        // --- Menus for upcoming week ---
        $mealModels = Meal::all();
        $today = Carbon::today();

        for ($i = 0; $i < 7; $i++) {
            $date = $today->copy()->addDays($i);

            foreach (['breakfast', 'lunch', 'dinner'] as $time) {
                $menu = Menu::create([
                    'name' => $date->format('l') . ' ' . ucfirst($time),
                    'available_date' => $date->toDateString(),
                    'meal_time' => $time,
                    'is_published' => true,
                ]);

                // Attach 3-4 relevant meals
                $relevantMeals = $mealModels->filter(function ($m) use ($time) {
                    if ($time === 'breakfast') return $m->category === 'breakfast';
                    if ($time === 'lunch') return in_array($m->category, ['lunch', 'snack']);
                    return in_array($m->category, ['dinner', 'snack']);
                })->shuffle()->take(3);

                $menu->meals()->attach(
                    $relevantMeals->mapWithKeys(fn ($m, $i) => [$m->id => ['sort_order' => $i]])
                );
            }
        }
    }
}
