<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Branch;
use App\Models\Category;
use App\Models\Product;
use App\Models\Ingredient;
use App\Models\OptionSet;
use App\Models\OptionSetItem;
use App\Models\InnerOptionSet;
use App\Models\InnerOptionItem;
use App\Models\User;
use Illuminate\Support\Facades\Hash;

class SampleDataSeeder extends Seeder
{
    public function run(): void
    {
        $this->command->info('🏢 Creating branches...');
        $this->createBranches();

        $this->command->info('🗂️ Creating categories...');
        $this->createCategories();

        $this->command->info('🍦 Creating products...');
        $this->createProducts();

        $this->command->info('🥄 Creating option sets (sizes)...');
        $this->createOptionSets();

        $this->command->info('🍫 Creating inner option sets (toppings, cones)...');
        $this->createInnerOptionSets();

        $this->command->info('🧪 Creating ingredients...');
        $this->createIngredients();

        $this->command->info('👥 Creating users...');
        $this->createUsers();

        $this->command->success('✅ Sample data seeded successfully!');
    }

    private function createBranches(): void
    {
        $branches = [
            [
                'name' => 'Creamy Utopia - Downtown',
                'code' => 'DT001',
                'address' => '123 Main Street, Downtown',
                'phone' => '555-0101',
                'email' => 'downtown@creamyutopia.com',
                'is_active' => true,
            ],
            [
                'name' => 'Creamy Utopia - Mall',
                'code' => 'ML001',
                'address' => '456 Shopping Center, Mall Road',
                'phone' => '555-0102',
                'email' => 'mall@creamyutopia.com',
                'is_active' => true,
            ],
            [
                'name' => 'Creamy Utopia - Beach',
                'code' => 'BH001',
                'address' => '789 Beach Boulevard',
                'phone' => '555-0103',
                'email' => 'beach@creamyutopia.com',
                'is_active' => true,
            ],
        ];

        $created = 0;
        foreach ($branches as $branchData) {
            if (!Branch::where('code', $branchData['code'])->exists()) {
                Branch::create($branchData);
                $created++;
            }
        }

        $this->command->info("   ✓ Created $created branches (skipped " . (count($branches) - $created) . " existing)");
    }

    private function createCategories(): void
    {
        $categories = [
            [
                'name' => 'Ice Cream',
                'slug' => 'ice-cream',
                'description' => 'Delicious ice cream flavors',
                'is_active' => true,
                'sort_order' => 1,
            ],
            [
                'name' => 'Sundaes',
                'slug' => 'sundaes',
                'description' => 'Ice cream sundaes with toppings',
                'is_active' => true,
                'sort_order' => 2,
            ],
            [
                'name' => 'Shakes',
                'slug' => 'shakes',
                'description' => 'Creamy milkshakes',
                'is_active' => true,
                'sort_order' => 3,
            ],
            [
                'name' => 'Frozen Yogurt',
                'slug' => 'frozen-yogurt',
                'description' => 'Healthy frozen yogurt',
                'is_active' => true,
                'sort_order' => 4,
            ],
        ];

        foreach ($categories as $category) {
            Category::create($category);
        }

        $this->command->info('   ✓ Created ' . count($categories) . ' categories');
    }

    private function createProducts(): void
    {
        $iceCategory = Category::where('name', 'Ice Cream')->first();
        $sundaeCategory = Category::where('name', 'Sundaes')->first();
        $shakeCategory = Category::where('name', 'Shakes')->first();

        $products = [
            // Ice Cream Flavors
            [
                'category_id' => $iceCategory->id,
                'name' => 'Vanilla Dream',
                'slug' => 'vanilla-dream',
                'description' => 'Classic Madagascar vanilla',
                'base_price' => 3.99,
                'max_scoops' => 3,
                'is_active' => true,
            ],
            [
                'category_id' => $iceCategory->id,
                'name' => 'Chocolate Fudge',
                'slug' => 'chocolate-fudge',
                'description' => 'Rich dark chocolate',
                'base_price' => 4.49,
                'max_scoops' => 3,
                'is_active' => true,
            ],
            [
                'category_id' => $iceCategory->id,
                'name' => 'Strawberry Bliss',
                'slug' => 'strawberry-bliss',
                'description' => 'Fresh strawberry cream',
                'base_price' => 4.29,
                'max_scoops' => 3,
                'is_active' => true,
            ],
            [
                'category_id' => $iceCategory->id,
                'name' => 'Mint Chocolate Chip',
                'slug' => 'mint-chocolate-chip',
                'description' => 'Cool mint with chocolate chips',
                'base_price' => 4.49,
                'max_scoops' => 3,
                'is_active' => true,
            ],
            [
                'category_id' => $iceCategory->id,
                'name' => 'Cookies & Cream',
                'slug' => 'cookies-and-cream',
                'description' => 'Oreo cookie pieces',
                'base_price' => 4.99,
                'max_scoops' => 3,
                'is_active' => true,
            ],
            [
                'category_id' => $iceCategory->id,
                'name' => 'Mango Tango',
                'slug' => 'mango-tango',
                'description' => 'Tropical mango delight',
                'base_price' => 4.79,
                'max_scoops' => 3,
                'is_active' => true,
            ],
            // Sundaes
            [
                'category_id' => $sundaeCategory->id,
                'name' => 'Hot Fudge Sundae',
                'slug' => 'hot-fudge-sundae',
                'description' => 'Vanilla ice cream with hot fudge',
                'base_price' => 6.99,
                'max_scoops' => 0,
                'is_active' => true,
            ],
            [
                'category_id' => $sundaeCategory->id,
                'name' => 'Banana Split',
                'slug' => 'banana-split',
                'description' => 'Three scoops with banana and toppings',
                'base_price' => 8.99,
                'max_scoops' => 0,
                'is_active' => true,
            ],
            // Shakes
            [
                'category_id' => $shakeCategory->id,
                'name' => 'Chocolate Shake',
                'slug' => 'chocolate-shake',
                'description' => 'Thick and creamy chocolate shake',
                'base_price' => 5.49,
                'max_scoops' => 0,
                'is_active' => true,
            ],
            [
                'category_id' => $shakeCategory->id,
                'name' => 'Strawberry Shake',
                'slug' => 'strawberry-shake',
                'description' => 'Fresh strawberry shake',
                'base_price' => 5.49,
                'max_scoops' => 0,
                'is_active' => true,
            ],
        ];

        foreach ($products as $product) {
            Product::create($product);
        }

        $this->command->info('   ✓ Created ' . count($products) . ' products');
    }

    private function createOptionSets(): void
    {
        // Size option set
        $sizeOptionSet = OptionSet::create([
            'name' => 'size',
            'display_name' => 'Size',
            'is_required' => true,
            'is_multiple' => false,
            'max_selections' => 1,
        ]);

        $sizes = [
            ['name' => 'Small', 'price_modifier' => 0.00, 'is_default' => true],
            ['name' => 'Medium', 'price_modifier' => 1.50, 'is_default' => false],
            ['name' => 'Large', 'price_modifier' => 2.50, 'is_default' => false],
        ];

        foreach ($sizes as $size) {
            OptionSetItem::create([
                'option_set_id' => $sizeOptionSet->id,
                'name' => $size['name'],
                'price_modifier' => $size['price_modifier'],
                'is_default' => $size['is_default'],
            ]);
        }

        $this->command->info('   ✓ Created size option set with 3 items');
    }

    private function createInnerOptionSets(): void
    {
        // Get the size option set
        $sizeOptionSet = OptionSet::where('name', 'size')->first();

        // Cone type inner option set
        $coneOptionSet = InnerOptionSet::create([
            'name' => 'cone_type',
            'display_name' => 'Cone Type',
            'option_set_id' => $sizeOptionSet->id,
        ]);

        $cones = [
            ['name' => 'Regular Cone', 'price_modifier' => 0.00],
            ['name' => 'Waffle Cone', 'price_modifier' => 0.50],
            ['name' => 'Sugar Cone', 'price_modifier' => 0.30],
            ['name' => 'Cup', 'price_modifier' => 0.00],
        ];

        foreach ($cones as $cone) {
            InnerOptionItem::create([
                'inner_option_set_id' => $coneOptionSet->id,
                'name' => $cone['name'],
                'price_modifier' => $cone['price_modifier'],
            ]);
        }

        // Toppings inner option set
        $toppingsOptionSet = InnerOptionSet::create([
            'name' => 'toppings',
            'display_name' => 'Toppings',
            'option_set_id' => $sizeOptionSet->id,
        ]);

        $toppings = [
            ['name' => 'Chocolate Chips', 'price_modifier' => 0.50],
            ['name' => 'Sprinkles', 'price_modifier' => 0.30],
            ['name' => 'Caramel Sauce', 'price_modifier' => 0.50],
            ['name' => 'Hot Fudge', 'price_modifier' => 0.60],
            ['name' => 'Whipped Cream', 'price_modifier' => 0.40],
            ['name' => 'Cherry', 'price_modifier' => 0.25],
            ['name' => 'Nuts', 'price_modifier' => 0.45],
            ['name' => 'Oreo Crumbs', 'price_modifier' => 0.55],
        ];

        foreach ($toppings as $topping) {
            InnerOptionItem::create([
                'inner_option_set_id' => $toppingsOptionSet->id,
                'name' => $topping['name'],
                'price_modifier' => $topping['price_modifier'],
            ]);
        }

        $this->command->info('   ✓ Created 2 inner option sets (Cone Types: 4, Toppings: 8)');
    }

    private function createIngredients(): void
    {
        $ingredients = [
            // Dairy
            ['name' => 'Whole Milk', 'unit' => 'L', 'cost_per_unit' => 1.50, 'minimum_stock' => 50],
            ['name' => 'Heavy Cream', 'unit' => 'L', 'cost_per_unit' => 3.00, 'minimum_stock' => 30],
            ['name' => 'Condensed Milk', 'unit' => 'kg', 'cost_per_unit' => 4.00, 'minimum_stock' => 20],

            // Flavors
            ['name' => 'Vanilla Extract', 'unit' => 'ml', 'cost_per_unit' => 0.05, 'minimum_stock' => 500],
            ['name' => 'Cocoa Powder', 'unit' => 'kg', 'cost_per_unit' => 8.00, 'minimum_stock' => 10],
            ['name' => 'Strawberry Puree', 'unit' => 'kg', 'cost_per_unit' => 6.00, 'minimum_stock' => 15],
            ['name' => 'Mango Puree', 'unit' => 'kg', 'cost_per_unit' => 7.00, 'minimum_stock' => 15],

            // Sweeteners
            ['name' => 'Sugar', 'unit' => 'kg', 'cost_per_unit' => 1.00, 'minimum_stock' => 50],
            ['name' => 'Honey', 'unit' => 'kg', 'cost_per_unit' => 10.00, 'minimum_stock' => 10],

            // Mix-ins
            ['name' => 'Chocolate Chips', 'unit' => 'kg', 'cost_per_unit' => 5.00, 'minimum_stock' => 20],
            ['name' => 'Oreo Cookies', 'unit' => 'kg', 'cost_per_unit' => 4.00, 'minimum_stock' => 15],
            ['name' => 'Nuts (Mixed)', 'unit' => 'kg', 'cost_per_unit' => 12.00, 'minimum_stock' => 10],

            // Toppings
            ['name' => 'Sprinkles', 'unit' => 'kg', 'cost_per_unit' => 3.00, 'minimum_stock' => 10],
            ['name' => 'Caramel Sauce', 'unit' => 'L', 'cost_per_unit' => 8.00, 'minimum_stock' => 5],
            ['name' => 'Hot Fudge', 'unit' => 'L', 'cost_per_unit' => 9.00, 'minimum_stock' => 5],
            ['name' => 'Whipped Cream', 'unit' => 'can', 'cost_per_unit' => 5.00, 'minimum_stock' => 20],

            // Cones
            ['name' => 'Regular Cones', 'unit' => 'pcs', 'cost_per_unit' => 0.10, 'minimum_stock' => 500],
            ['name' => 'Waffle Cones', 'unit' => 'pcs', 'cost_per_unit' => 0.25, 'minimum_stock' => 300],
            ['name' => 'Sugar Cones', 'unit' => 'pcs', 'cost_per_unit' => 0.15, 'minimum_stock' => 400],
            ['name' => 'Paper Cups', 'unit' => 'pcs', 'cost_per_unit' => 0.05, 'minimum_stock' => 1000],
        ];

        foreach ($ingredients as $ingredient) {
            Ingredient::create($ingredient);
        }

        $this->command->info('   ✓ Created ' . count($ingredients) . ' ingredients');
    }

    private function createUsers(): void
    {
        $downtown = Branch::where('code', 'DT001')->first();
        $mall = Branch::where('code', 'ML001')->first();

        $users = [
            [
                'name' => 'John Manager',
                'email' => 'manager@test.com',
                'password' => Hash::make('password'),
                'branch_id' => $downtown->id,
                'role' => 'manager',
            ],
            [
                'name' => 'Sarah Cashier',
                'email' => 'cashier@test.com',
                'password' => Hash::make('password'),
                'branch_id' => $downtown->id,
                'role' => 'pos-operator',
            ],
            [
                'name' => 'Mike Kitchen',
                'email' => 'kitchen@test.com',
                'password' => Hash::make('password'),
                'branch_id' => $mall->id,
                'role' => 'kitchen-staff',
            ],
        ];

        foreach ($users as $userData) {
            $role = $userData['role'];
            unset($userData['role']);

            $user = User::create($userData);
            $user->assignRole($role);
        }

        $this->command->info('   ✓ Created ' . count($users) . ' users (password: password)');
    }
}
