<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class ProductCatalogSeeder extends Seeder
{
    public function run(): void
    {
        $categories = [
            ['name' => 'Ice Cream',   'description' => 'Scoop-based ice cream flavours', 'sort_order' => 1],
            ['name' => 'Baked Goods', 'description' => 'Made-to-order baked desserts',   'sort_order' => 2],
            ['name' => 'Waffles',     'description' => 'Freshly baked waffles',           'sort_order' => 3],
            ['name' => 'Cones',       'description' => 'Ice cream cones and cups',        'sort_order' => 4],
            ['name' => 'Toppings',    'description' => 'Toppings to add on ice cream',    'sort_order' => 5],
            ['name' => 'Sauces',      'description' => 'Sauce add-ons',                   'sort_order' => 6],
        ];

        $categoryMap = [];
        foreach ($categories as $cat) {
            $category = \App\Models\Category::firstOrCreate(
                ['slug' => \Illuminate\Support\Str::slug($cat['name'])],
                array_merge($cat, ['is_active' => true])
            );
            $categoryMap[$cat['name']] = $category->id;
        }

        $products = [
            ['category' => 'Ice Cream',   'name' => 'Vanilla Ice Cream',   'description' => 'Classic creamy vanilla ice cream',           'base_price' => 0,    'scoop_price' => 2.50, 'max_scoops' => 6],
            ['category' => 'Ice Cream',   'name' => 'Chocolate Ice Cream', 'description' => 'Rich chocolate ice cream',                   'base_price' => 0,    'scoop_price' => 2.50, 'max_scoops' => 6],
            ['category' => 'Baked Goods', 'name' => 'Cookies',             'description' => 'Freshly baked cookies, made to order',       'base_price' => 3.50, 'scoop_price' => null,  'max_scoops' => 0],
            ['category' => 'Baked Goods', 'name' => 'Brownie',             'description' => 'Warm fudgy brownie, made to order',          'base_price' => 4.00, 'scoop_price' => null,  'max_scoops' => 0],
            ['category' => 'Baked Goods', 'name' => 'Molten Lava',         'description' => 'Molten lava cake, mixed and baked to order', 'base_price' => 5.00, 'scoop_price' => null,  'max_scoops' => 0],
            ['category' => 'Waffles',     'name' => 'Classic Waffle',      'description' => 'Plain waffle, baked to order',               'base_price' => 5.00, 'scoop_price' => null,  'max_scoops' => 0],
            ['category' => 'Waffles',     'name' => 'Bubble Waffle',       'description' => 'Hong Kong-style bubble waffle, baked to order','base_price'=> 6.00, 'scoop_price' => null,  'max_scoops' => 0],
            ['category' => 'Waffles',     'name' => 'Belgian Waffle',      'description' => 'Thick Belgian waffle, baked to order',        'base_price' => 6.50, 'scoop_price' => null,  'max_scoops' => 0],
            ['category' => 'Cones',       'name' => 'Ice Cream Cone',      'description' => 'Crispy waffle cone',                          'base_price' => 0.50, 'scoop_price' => null,  'max_scoops' => 0],
            ['category' => 'Toppings',    'name' => 'Sprinkles',           'description' => 'Colourful sprinkles topping',                 'base_price' => 0.50, 'scoop_price' => null,  'max_scoops' => 0],
            ['category' => 'Toppings',    'name' => 'Oreo Crumble',        'description' => 'Crushed Oreo cookie topping',                 'base_price' => 0.75, 'scoop_price' => null,  'max_scoops' => 0],
            ['category' => 'Toppings',    'name' => 'Fresh Fruits',        'description' => 'Mixed fresh fruit topping',                   'base_price' => 1.00, 'scoop_price' => null,  'max_scoops' => 0],
            ['category' => 'Sauces',      'name' => 'Lotus Sauce',         'description' => 'Caramelised biscuit spread sauce',            'base_price' => 1.00, 'scoop_price' => null,  'max_scoops' => 0],
            ['category' => 'Sauces',      'name' => 'Nutella Sauce',       'description' => 'Hazelnut chocolate spread sauce',             'base_price' => 1.00, 'scoop_price' => null,  'max_scoops' => 0],
            ['category' => 'Sauces',      'name' => 'Pista Sauce',         'description' => 'Pistachio sauce',                             'base_price' => 1.00, 'scoop_price' => null,  'max_scoops' => 0],
        ];

        $branches = \App\Models\Branch::where('is_active', true)->get();

        foreach ($products as $productData) {
            $categoryId = $categoryMap[$productData['category']];
            unset($productData['category']);
            $productData['slug'] = \Illuminate\Support\Str::slug($productData['name']);
            $productData['category_id'] = $categoryId;
            $productData['is_active'] = true;

            $product = \App\Models\Product::firstOrCreate(
                ['slug' => $productData['slug']],
                $productData
            );

            foreach ($branches as $branch) {
                \App\Models\BranchProduct::firstOrCreate(
                    ['branch_id' => $branch->id, 'product_id' => $product->id],
                    ['is_available' => true]
                );
            }
        }

        $this->command->info('Seeded: ' . count($categories) . ' categories, ' . count($products) . ' products.');
    }
}
