<?php

namespace Database\Seeders;

use App\Models\Category;
use App\Models\Product;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class SampleProductsSeeder extends Seeder
{
    public function run(): void
    {
        // Create Categories
        $categories = [
            [
                'name' => 'Beverages',
                'slug' => 'beverages',
                'description' => 'Soft drinks, juices, water, and other beverages',
                'is_active' => true,
            ],
            [
                'name' => 'Groceries',
                'slug' => 'groceries',
                'description' => 'Daily essentials like rice, beans, flour, oil, etc.',
                'is_active' => true,
            ],
            [
                'name' => 'Snacks',
                'slug' => 'snacks',
                'description' => 'Chips, biscuits, sweets, and other snacks',
                'is_active' => true,
            ],
            [
                'name' => 'Personal Care',
                'slug' => 'personal-care',
                'description' => 'Soap, toothpaste, body cream, and toiletries',
                'is_active' => true,
            ],
            [
                'name' => 'Household Items',
                'slug' => 'household-items',
                'description' => 'Detergents, cleaning supplies, and household goods',
                'is_active' => true,
            ],
            [
                'name' => 'Dairy & Eggs',
                'slug' => 'dairy-eggs',
                'description' => 'Milk, eggs, butter, cheese, and dairy products',
                'is_active' => true,
            ],
        ];

        $createdCategories = [];
        foreach ($categories as $categoryData) {
            $createdCategories[$categoryData['slug']] = Category::firstOrCreate(
                ['slug' => $categoryData['slug']],
                $categoryData
            );
        }

        // Create Products for each category
        $products = [
            // Beverages
            [
                'category_id' => $createdCategories['beverages']->id,
                'name' => 'Coca-Cola 50cl',
                'slug' => 'coca-cola-50cl',
                'sku' => 'BEV-COLA-50',
                'barcode' => '8851234567890',
                'description' => 'Coca-Cola soft drink 50cl bottle',
                'unit' => 'bottle',
                'cost_price' => 180.00,
                'selling_price' => 250.00,
                'stock_quantity' => 120,
                'low_stock_threshold' => 20,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['beverages']->id,
                'name' => 'Fanta Orange 50cl',
                'slug' => 'fanta-orange-50cl',
                'sku' => 'BEV-FANT-50',
                'barcode' => '8851234567891',
                'description' => 'Fanta Orange soft drink 50cl bottle',
                'unit' => 'bottle',
                'cost_price' => 180.00,
                'selling_price' => 250.00,
                'stock_quantity' => 100,
                'low_stock_threshold' => 20,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['beverages']->id,
                'name' => 'Eva Water 75cl',
                'slug' => 'eva-water-75cl',
                'sku' => 'BEV-EVA-75',
                'barcode' => '8851234567892',
                'description' => 'Eva bottled water 75cl',
                'unit' => 'bottle',
                'cost_price' => 120.00,
                'selling_price' => 200.00,
                'stock_quantity' => 200,
                'low_stock_threshold' => 30,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['beverages']->id,
                'name' => 'Chi Exotic Juice 1L',
                'slug' => 'chi-exotic-juice-1l',
                'sku' => 'BEV-CHI-1L',
                'barcode' => '8851234567893',
                'description' => 'Chi Exotic fruit juice 1 liter',
                'unit' => 'pack',
                'cost_price' => 450.00,
                'selling_price' => 650.00,
                'stock_quantity' => 80,
                'low_stock_threshold' => 15,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['beverages']->id,
                'name' => 'Maltina 33cl',
                'slug' => 'maltina-33cl',
                'sku' => 'BEV-MALT-33',
                'barcode' => '8851234567894',
                'description' => 'Maltina malt drink 33cl',
                'unit' => 'bottle',
                'cost_price' => 200.00,
                'selling_price' => 300.00,
                'stock_quantity' => 90,
                'low_stock_threshold' => 20,
                'is_active' => true,
            ],

            // Groceries
            [
                'category_id' => $createdCategories['groceries']->id,
                'name' => 'Golden Penny Rice 5kg',
                'slug' => 'golden-penny-rice-5kg',
                'sku' => 'GRO-RICE-5KG',
                'barcode' => '8851234567895',
                'description' => 'Golden Penny parboiled rice 5kg',
                'unit' => 'bag',
                'cost_price' => 3500.00,
                'selling_price' => 4500.00,
                'stock_quantity' => 50,
                'low_stock_threshold' => 10,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['groceries']->id,
                'name' => 'Honeywell Flour 2kg',
                'slug' => 'honeywell-flour-2kg',
                'sku' => 'GRO-FLOUR-2KG',
                'barcode' => '8851234567896',
                'description' => 'Honeywell wheat flour 2kg',
                'unit' => 'pack',
                'cost_price' => 800.00,
                'selling_price' => 1200.00,
                'stock_quantity' => 60,
                'low_stock_threshold' => 15,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['groceries']->id,
                'name' => 'Power Oil 1L',
                'slug' => 'power-oil-1l',
                'sku' => 'GRO-OIL-1L',
                'barcode' => '8851234567897',
                'description' => 'Power vegetable oil 1 liter',
                'unit' => 'bottle',
                'cost_price' => 980.00,
                'selling_price' => 1400.00,
                'stock_quantity' => 70,
                'low_stock_threshold' => 15,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['groceries']->id,
                'name' => 'Golden Penny Sugar 1kg',
                'slug' => 'golden-penny-sugar-1kg',
                'sku' => 'GRO-SUGAR-1KG',
                'barcode' => '8851234567898',
                'description' => 'Golden Penny granulated sugar 1kg',
                'unit' => 'pack',
                'cost_price' => 500.00,
                'selling_price' => 750.00,
                'stock_quantity' => 80,
                'low_stock_threshold' => 20,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['groceries']->id,
                'name' => 'Dangote Salt 1kg',
                'slug' => 'dangote-salt-1kg',
                'sku' => 'GRO-SALT-1KG',
                'barcode' => '8851234567899',
                'description' => 'Dangote iodized salt 1kg',
                'unit' => 'pack',
                'cost_price' => 200.00,
                'selling_price' => 350.00,
                'stock_quantity' => 100,
                'low_stock_threshold' => 20,
                'is_active' => true,
            ],

            // Snacks
            [
                'category_id' => $createdCategories['snacks']->id,
                'name' => 'Pringles Original 40g',
                'slug' => 'pringles-original-40g',
                'sku' => 'SNK-PRING-40',
                'barcode' => '8851234567900',
                'description' => 'Pringles original potato chips 40g',
                'unit' => 'pack',
                'cost_price' => 350.00,
                'selling_price' => 550.00,
                'stock_quantity' => 45,
                'low_stock_threshold' => 10,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['snacks']->id,
                'name' => 'Gala Sausage Roll',
                'slug' => 'gala-sausage-roll',
                'sku' => 'SNK-GALA-SR',
                'barcode' => '8851234567901',
                'description' => 'Gala sausage roll',
                'unit' => 'piece',
                'cost_price' => 120.00,
                'selling_price' => 200.00,
                'stock_quantity' => 150,
                'low_stock_threshold' => 30,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['snacks']->id,
                'name' => 'Digestive Biscuit',
                'slug' => 'digestive-biscuit',
                'sku' => 'SNK-DIGE-BIS',
                'barcode' => '8851234567902',
                'description' => 'McVities Digestive biscuit pack',
                'unit' => 'pack',
                'cost_price' => 450.00,
                'selling_price' => 700.00,
                'stock_quantity' => 60,
                'low_stock_threshold' => 15,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['snacks']->id,
                'name' => 'Tom Tom Menthol Sweet',
                'slug' => 'tom-tom-menthol-sweet',
                'sku' => 'SNK-TOM-TOM',
                'barcode' => '8851234567903',
                'description' => 'Tom Tom menthol sweet candy',
                'unit' => 'pack',
                'cost_price' => 50.00,
                'selling_price' => 100.00,
                'stock_quantity' => 200,
                'low_stock_threshold' => 40,
                'is_active' => true,
            ],

            // Personal Care
            [
                'category_id' => $createdCategories['personal-care']->id,
                'name' => 'Imperial Leather Soap',
                'slug' => 'imperial-leather-soap',
                'sku' => 'PC-SOAP-IL',
                'barcode' => '8851234567904',
                'description' => 'Imperial Leather soap bar',
                'unit' => 'piece',
                'cost_price' => 280.00,
                'selling_price' => 450.00,
                'stock_quantity' => 80,
                'low_stock_threshold' => 20,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['personal-care']->id,
                'name' => 'Oral-B Toothpaste 75ml',
                'slug' => 'oral-b-toothpaste-75ml',
                'sku' => 'PC-TOOTH-OB',
                'barcode' => '8851234567905',
                'description' => 'Oral-B Complete toothpaste 75ml',
                'unit' => 'tube',
                'cost_price' => 650.00,
                'selling_price' => 950.00,
                'stock_quantity' => 50,
                'low_stock_threshold' => 15,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['personal-care']->id,
                'name' => 'Nivea Body Cream 250ml',
                'slug' => 'nivea-body-cream-250ml',
                'sku' => 'PC-CREAM-NIV',
                'barcode' => '8851234567906',
                'description' => 'Nivea body lotion 250ml',
                'unit' => 'bottle',
                'cost_price' => 1200.00,
                'selling_price' => 1800.00,
                'stock_quantity' => 40,
                'low_stock_threshold' => 10,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['personal-care']->id,
                'name' => 'Always Sanitary Pads',
                'slug' => 'always-sanitary-pads',
                'sku' => 'PC-PAD-ALWY',
                'barcode' => '8851234567907',
                'description' => 'Always ultra sanitary pads pack',
                'unit' => 'pack',
                'cost_price' => 450.00,
                'selling_price' => 750.00,
                'stock_quantity' => 55,
                'low_stock_threshold' => 15,
                'is_active' => true,
            ],

            // Household Items
            [
                'category_id' => $createdCategories['household-items']->id,
                'name' => 'Omo Detergent 500g',
                'slug' => 'omo-detergent-500g',
                'sku' => 'HH-OMO-500G',
                'barcode' => '8851234567908',
                'description' => 'Omo washing powder 500g',
                'unit' => 'pack',
                'cost_price' => 380.00,
                'selling_price' => 600.00,
                'stock_quantity' => 70,
                'low_stock_threshold' => 15,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['household-items']->id,
                'name' => 'Harpic Toilet Cleaner 500ml',
                'slug' => 'harpic-toilet-cleaner-500ml',
                'sku' => 'HH-HARPIC-500',
                'barcode' => '8851234567909',
                'description' => 'Harpic toilet cleaner 500ml',
                'unit' => 'bottle',
                'cost_price' => 550.00,
                'selling_price' => 850.00,
                'stock_quantity' => 45,
                'low_stock_threshold' => 10,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['household-items']->id,
                'name' => 'Hypo Bleach 1L',
                'slug' => 'hypo-bleach-1l',
                'sku' => 'HH-HYPO-1L',
                'barcode' => '8851234567910',
                'description' => 'Hypo bleach 1 liter',
                'unit' => 'bottle',
                'cost_price' => 200.00,
                'selling_price' => 350.00,
                'stock_quantity' => 60,
                'low_stock_threshold' => 15,
                'is_active' => true,
            ],

            // Dairy & Eggs
            [
                'category_id' => $createdCategories['dairy-eggs']->id,
                'name' => 'Peak Milk 400g',
                'slug' => 'peak-milk-400g',
                'sku' => 'DE-PEAK-400G',
                'barcode' => '8851234567911',
                'description' => 'Peak powdered milk 400g',
                'unit' => 'tin',
                'cost_price' => 1100.00,
                'selling_price' => 1600.00,
                'stock_quantity' => 50,
                'low_stock_threshold' => 10,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['dairy-eggs']->id,
                'name' => 'Fresh Eggs (Crate)',
                'slug' => 'fresh-eggs-crate',
                'sku' => 'DE-EGGS-CRATE',
                'barcode' => '8851234567912',
                'description' => 'Fresh eggs 1 crate (30 pieces)',
                'unit' => 'crate',
                'cost_price' => 1800.00,
                'selling_price' => 2500.00,
                'stock_quantity' => 25,
                'low_stock_threshold' => 5,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['dairy-eggs']->id,
                'name' => 'Dano Milk Sachet',
                'slug' => 'dano-milk-sachet',
                'sku' => 'DE-DANO-SACH',
                'barcode' => '8851234567913',
                'description' => 'Dano instant milk sachet',
                'unit' => 'sachet',
                'cost_price' => 50.00,
                'selling_price' => 100.00,
                'stock_quantity' => 300,
                'low_stock_threshold' => 50,
                'is_active' => true,
            ],
            [
                'category_id' => $createdCategories['dairy-eggs']->id,
                'name' => 'Butter 250g',
                'slug' => 'butter-250g',
                'sku' => 'DE-BUTTER-250',
                'barcode' => '8851234567914',
                'description' => 'Butter pack 250g',
                'unit' => 'pack',
                'cost_price' => 850.00,
                'selling_price' => 1300.00,
                'stock_quantity' => 35,
                'low_stock_threshold' => 10,
                'is_active' => true,
            ],
        ];

        foreach ($products as $productData) {
            Product::firstOrCreate(
                ['sku' => $productData['sku']],
                $productData
            );
        }

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