<?php

namespace Database\Seeders;

use App\Models\Product;
use App\Models\ProductImage;
use App\Models\Category;
use App\Models\Subcategory;
use App\Models\Brand;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\File;

class ProductSeeder extends Seeder
{
    private array $colors = [
        '#10B981', '#3B82F6', '#8B5CF6', '#F59E0B', '#EF4444',
        '#EC4899', '#06B6D4', '#84CC16', '#F97316', '#6366F1',
        '#14B8A6', '#E11D48', '#A855F7', '#0EA5E9', '#D946EF',
    ];

    private int $colorIndex = 0;

    private function nextColor(): string
    {
        $color = $this->colors[$this->colorIndex % count($this->colors)];
        $this->colorIndex++;
        return $color;
    }

    private function generateSvg(string $name, string $category): string
    {
        $initials = '';
        foreach (explode(' ', $name) as $word) {
            $initials .= mb_strtoupper(mb_substr($word, 0, 1));
            if (mb_strlen($initials) >= 2) break;
        }
        $bg = $this->nextColor();
        return <<<SVG
<svg xmlns="http://www.w3.org/2000/svg" width="400" height="400" viewBox="0 0 400 400">
  <rect width="400" height="400" fill="{$bg}" rx="12"/>
  <text x="200" y="185" text-anchor="middle" dominant-baseline="middle" font-family="Arial" font-size="28" font-weight="700" fill="white" opacity="0.95">{$initials}</text>
  <text x="200" y="230" text-anchor="middle" dominant-baseline="middle" font-family="Arial" font-size="11" fill="white" opacity="0.7">{$category}</text>
</svg>
SVG;
    }

    private function createImage(Product $product, string $category): void
    {
        $filename = Str::slug($product->name) . '.svg';
        $dir = public_path('storage/products');
        if (!File::isDirectory($dir)) {
            File::makeDirectory($dir, 0755, true);
        }
        file_put_contents($dir . '/' . $filename, $this->generateSvg($product->name, $category));
        ProductImage::create(['product_id' => $product->id, 'path' => 'products/' . $filename]);
    }

    public function run(): void
    {
        $brands = Brand::pluck('id')->toArray();
        if (empty($brands)) return;

        $allProducts = [
            'home' => [
                ['name' => 'Modern Velvet Sofa - 3 Seater', 'price' => 899.99, 'sale' => 749.99, 'desc' => 'Premium velvet upholstery with solid wood frame. Plush cushioning and elegant tapered legs.', 'sub' => 'living-room'],
                ['name' => 'Industrial Coffee Table', 'price' => 299.99, 'desc' => 'Rustic design with spacious tabletop and lower storage shelf. Reclaimed wood and steel.', 'sub' => 'living-room'],
                ['name' => 'Mid Century Accent Chair', 'price' => 449.99, 'sale' => 379.99, 'desc' => 'Mid-century modern accent chair with solid walnut legs and linen upholstery.', 'sub' => 'living-room'],
                ['name' => 'Abstract Area Rug 5x7', 'price' => 189.99, 'desc' => 'Hand-tufted wool blend rug with contemporary abstract pattern. Stain-resistant.', 'sub' => 'living-room'],
                ['name' => 'Queen Upholstered Bed Frame', 'price' => 699.99, 'sale' => 599.99, 'desc' => 'Button-tufted headboard with durable linen fabric. Sturdy slat support system.', 'sub' => 'bedroom'],
                ['name' => 'Memory Foam Mattress Topper - Queen', 'price' => 159.99, 'sale' => 119.99, 'desc' => '3-inch gel memory foam topper with cooling technology. CertiPUR-US certified.', 'sub' => 'bedroom'],
                ['name' => 'Bedside Table with Charging Station', 'price' => 179.99, 'desc' => 'Compact nightstand with built-in wireless charger, USB ports, and hidden storage.', 'sub' => 'bedroom'],
                ['name' => 'Luxury Bamboo Sheet Set - Queen', 'price' => 89.99, 'sale' => 69.99, 'desc' => 'Ultra-soft bamboo viscose sheets with deep pockets. Includes fitted and flat sheet.', 'sub' => 'bedroom'],
                ['name' => 'Professional Chef Knife Set - 5 Piece', 'price' => 199.99, 'sale' => 149.99, 'desc' => 'High-carbon German stainless steel knives with ergonomic handles.', 'sub' => 'kitchen-dining'],
                ['name' => 'Cast Iron Dutch Oven - 6 Quart', 'price' => 129.99, 'desc' => 'Enameled cast iron dutch oven with tight-fitting lid. Oven safe to 500°F.', 'sub' => 'kitchen-dining'],
                ['name' => 'Farmhouse Dining Table - Seats 6', 'price' => 799.99, 'sale' => 679.99, 'desc' => 'Solid pine farmhouse table with trestle base. Distressed finish.', 'sub' => 'kitchen-dining'],
                ['name' => 'Ceramic Dinnerware Set - 16 Piece', 'price' => 89.99, 'sale' => 74.99, 'desc' => 'Stoneware dinner set service for 4. Microwave and dishwasher safe.', 'sub' => 'kitchen-dining'],
                ['name' => 'Luxury Turkish Cotton Towel Set', 'price' => 79.99, 'sale' => 59.99, 'desc' => '700 GSM zero-twist Turkish cotton towels. Includes bath, hand, and face towels.', 'sub' => 'bathroom'],
                ['name' => 'Rainfall Shower Head with Handheld', 'price' => 69.99, 'sale' => 54.99, 'desc' => '10-inch rainfall shower head with detachable handheld sprayer.', 'sub' => 'bathroom'],
                ['name' => 'Macrame Wall Hanging - Large', 'price' => 49.99, 'desc' => 'Handcrafted cotton macrame wall hanging with wooden dowel. 36 inches wide.', 'sub' => 'home-decor'],
                ['name' => 'Scented Soy Candle Collection - 3 Pack', 'price' => 39.99, 'sale' => 29.99, 'desc' => 'Hand-poured soy wax candles in artisan glass jars. 40+ hour burn time.', 'sub' => 'home-decor'],
                ['name' => 'Gallery Wall Art Set - 4 Frames', 'price' => 129.99, 'desc' => 'Curated set of 4 botanical prints with black frames. Ready to hang.', 'sub' => 'home-decor'],
                ['name' => 'Modern Pendant Light - Gold', 'price' => 159.99, 'desc' => 'Brass-finished pendant light with opal glass shade. Adjustable cord length.', 'sub' => 'lighting'],
                ['name' => 'Smart LED Desk Lamp', 'price' => 79.99, 'sale' => 59.99, 'desc' => 'Touch-controlled LED desk lamp with adjustable color temperature.', 'sub' => 'lighting'],
                ['name' => 'Crystal Chandelier - Small', 'price' => 249.99, 'desc' => 'Elegant K9 crystal chandelier with chrome finish. Features 6 lights.', 'sub' => 'lighting'],
                ['name' => 'Robotic Vacuum Cleaner', 'price' => 449.99, 'sale' => 379.99, 'desc' => 'Smart robot vacuum with LiDAR navigation and mopping function.', 'sub' => 'cleaning-supplies'],
                ['name' => 'Cordless Stick Vacuum Cleaner', 'price' => 299.99, 'desc' => 'Powerful cordless stick vacuum with 40-minute runtime. HEPA filtration.', 'sub' => 'cleaning-supplies'],
                ['name' => 'All-Purpose Cleaning Spray', 'price' => 12.99, 'sale' => 9.99, 'desc' => 'Plant-based all-purpose cleaner with essential oils. 32 oz spray bottle.', 'sub' => 'cleaning-supplies'],
                ['name' => 'Modular Cube Storage System', 'price' => 129.99, 'sale' => 99.99, 'desc' => 'Versatile cube storage unit with 9 compartments. Includes 3 fabric bins.', 'sub' => 'storage-organization'],
                ['name' => 'Under-Bed Storage Bags - 2 Pack', 'price' => 34.99, 'desc' => 'Heavy-duty zippered storage bags with clear view window. Waterproof.', 'sub' => 'storage-organization'],
            ],

            'baby-food' => [
                ['name' => 'Organic Baby Cereal - Rice', 'price' => 8.99, 'desc' => 'Single grain organic rice cereal. Iron-fortified, non-GMO.', 'sub' => null],
                ['name' => 'Baby Food Pouch - Apple & Banana', 'price' => 3.49, 'desc' => 'Organic fruit puree pouch. No added sugar or preservatives.', 'sub' => null],
                ['name' => 'Baby Food Pouch - Sweet Potato', 'price' => 3.49, 'desc' => 'Organic sweet potato puree. Rich in vitamins A and C.', 'sub' => null],
                ['name' => 'Toddler Snack Bars - Berry', 'price' => 4.99, 'sale' => 3.99, 'desc' => 'Whole grain snack bars with real berries. Safe for toddlers.', 'sub' => null],
                ['name' => 'Organic Baby Oatmeal', 'price' => 7.99, 'desc' => 'Steel-cut organic oatmeal fortified with iron and zinc.', 'sub' => null],
            ],

            'beauty-personal-care' => [
                ['name' => 'Hydrating Face Moisturizer', 'price' => 24.99, 'sale' => 19.99, 'desc' => 'Lightweight daily moisturizer with hyaluronic acid and vitamin E.', 'sub' => 'skincare'],
                ['name' => 'Vitamin C Serum - 30ml', 'price' => 34.99, 'desc' => 'Brightening serum with 20% vitamin C and ferulic acid.', 'sub' => 'skincare'],
                ['name' => 'Sunscreen SPF 50 - Daily', 'price' => 18.99, 'desc' => 'Broad spectrum SPF 50 mineral sunscreen. Non-greasy formula.', 'sub' => 'skincare'],
                ['name' => 'Clay Face Mask - Green', 'price' => 22.99, 'desc' => 'Purifying green clay mask with tea tree oil. Deep cleans pores.', 'sub' => 'skincare'],
                ['name' => 'Lip Balm - Shea Butter', 'price' => 6.99, 'desc' => 'Moisturizing lip balm with shea butter and coconut oil.', 'sub' => 'skincare'],
                ['name' => 'Rose Facial Toner', 'price' => 15.99, 'sale' => 12.99, 'desc' => 'Alcohol-free rose water toner. Balances pH and tightens pores.', 'sub' => 'skincare'],
                ['name' => 'Eye Cream - Anti-Aging', 'price' => 29.99, 'desc' => 'Peptide-infused eye cream. Reduces fine lines and dark circles.', 'sub' => 'skincare'],
            ],

            'frozen-foods' => [
                ['name' => 'Frozen Chicken Breast - 2kg', 'price' => 18.99, 'desc' => 'Boneless, skinless chicken breasts. Individually flash-frozen.', 'sub' => 'frozen-meat'],
                ['name' => 'Frozen Mixed Vegetables', 'price' => 5.99, 'sale' => 4.49, 'desc' => 'Blend of peas, carrots, corn, and green beans.', 'sub' => 'frozen-vegetables'],
                ['name' => 'Frozen French Fries - Crispy', 'price' => 4.99, 'desc' => 'Golden crispy steak-cut fries. Oven or air fryer ready.', 'sub' => 'frozen-vegetables'],
                ['name' => 'Frozen Pizza - Margherita', 'price' => 8.99, 'desc' => 'Hand-stretched crust with mozzarella and basil. 12-inch.', 'sub' => null],
                ['name' => 'Frozen Fish Fillets - Tilapia', 'price' => 12.99, 'sale' => 10.99, 'desc' => 'Boneless tilapia fillets. Perfect for baking or frying.', 'sub' => 'fish-seafood'],
                ['name' => 'Frozen Mango Chunks', 'price' => 6.99, 'desc' => 'Premium ripe mango chunks. Perfect for smoothies.', 'sub' => null],
                ['name' => 'Frozen Dumplings - Pork', 'price' => 7.99, 'desc' => 'Handmade pork and vegetable dumplings. Steam or pan-fry.', 'sub' => null],
            ],

            'food-groceries' => [
                ['name' => 'Basmati Rice - 5kg', 'price' => 14.99, 'desc' => 'Premium aged basmati rice. Long grain and aromatic.', 'sub' => 'rice-grains'],
                ['name' => 'Extra Virgin Olive Oil - 1L', 'price' => 12.99, 'sale' => 10.99, 'desc' => 'Cold-pressed extra virgin olive oil from Mediterranean olives.', 'sub' => 'cooking-oil'],
                ['name' => 'Whole Wheat Pasta - 500g', 'price' => 3.49, 'desc' => '100% whole wheat penne pasta. High fiber.', 'sub' => 'pasta-noodles'],
                ['name' => 'Canned Tomatoes - 400g', 'price' => 2.49, 'desc' => 'San Marzano whole peeled tomatoes. BPA-free can.', 'sub' => 'canned-goods'],
                ['name' => 'Honey - Raw & Unfiltered', 'price' => 9.99, 'desc' => 'Raw unfiltered honey from local apiaries. Pure.', 'sub' => null],
                ['name' => 'Almonds - Roasted & Salted', 'price' => 8.99, 'sale' => 7.49, 'desc' => 'Premium roasted almonds with sea salt. 500g bag.', 'sub' => null],
                ['name' => 'Oat Milk - Original', 'price' => 4.99, 'desc' => 'Creamy oat milk, fortified with calcium and vitamin D.', 'sub' => null],
            ],

            'health-wellness' => [
                ['name' => 'Multivitamin - Daily Formula', 'price' => 19.99, 'desc' => 'Complete daily multivitamin with 25 essential nutrients.', 'sub' => null],
                ['name' => 'Fish Oil Omega-3 - 120 Softgels', 'price' => 22.99, 'sale' => 18.99, 'desc' => 'Triple-strength omega-3 fish oil. Heart and brain health.', 'sub' => null],
                ['name' => 'Probiotic Complex - 30 Capsules', 'price' => 29.99, 'desc' => '10 billion CFU probiotic blend. Digestive health.', 'sub' => null],
                ['name' => 'Vitamin D3 + K2 Drops', 'price' => 16.99, 'desc' => 'Liquid vitamin D3 with K2 for better absorption.', 'sub' => null],
                ['name' => 'Collagen Peptides Powder', 'price' => 27.99, 'sale' => 22.99, 'desc' => 'Grass-fed collagen peptides. Skin, hair, and joints.', 'sub' => null],
            ],

            'body-bathing-accessories' => [
                ['name' => 'Loofah Bath Sponge - 3 Pack', 'price' => 8.99, 'desc' => 'Natural loofah sponges for exfoliation. 3 sizes.', 'sub' => null],
                ['name' => 'Bath Caddy Tray - Bamboo', 'price' => 34.99, 'sale' => 27.99, 'desc' => 'Adjustable bamboo bath caddy with book holder.', 'sub' => null],
                ['name' => 'Silicone Body Brush', 'price' => 12.99, 'desc' => 'Soft silicone body brush for gentle exfoliation.', 'sub' => null],
                ['name' => 'Bath Towel Set - 6 Piece', 'price' => 49.99, 'desc' => 'Premium Egyptian cotton towels. 6 pieces.', 'sub' => null],
                ['name' => 'Bath Pillow - Memory Foam', 'price' => 22.99, 'sale' => 17.99, 'desc' => 'Contoured memory foam bath pillow with suction cups.', 'sub' => null],
            ],

            'skin-care' => [
                ['name' => 'Retinol Night Cream', 'price' => 32.99, 'desc' => 'Anti-aging retinol night cream with vitamin E.', 'sub' => null],
                ['name' => 'Hyaluronic Acid Serum', 'price' => 26.99, 'sale' => 21.99, 'desc' => 'Pure hyaluronic acid serum. Deep hydration.', 'sub' => null],
                ['name' => 'Vitamin C Cleanser', 'price' => 16.99, 'desc' => 'Gentle foaming cleanser with vitamin C.', 'sub' => null],
                ['name' => 'Charcoal Face Mask', 'price' => 14.99, 'desc' => 'Activated charcoal peel-off mask. Unclogs pores.', 'sub' => null],
                ['name' => 'Aloe Vera Gel - Pure', 'price' => 9.99, 'desc' => '100% pure aloe vera gel. Soothes and heals skin.', 'sub' => null],
            ],

            'oral-care' => [
                ['name' => 'Electric Toothbrush - Sonic', 'price' => 49.99, 'sale' => 39.99, 'desc' => 'Sonic electric toothbrush with 5 modes. 4 brush heads.', 'sub' => null],
                ['name' => 'Whitening Toothpaste - Charcoal', 'price' => 8.99, 'desc' => 'Activated charcoal whitening toothpaste.', 'sub' => null],
                ['name' => 'Dental Floss - Mint', 'price' => 4.99, 'desc' => 'Expanding floss with natural mint flavor.', 'sub' => null],
                ['name' => 'Mouthwash - Alcohol Free', 'price' => 7.99, 'desc' => 'Alcohol-free antibacterial mouthwash.', 'sub' => null],
                ['name' => 'Water Flosser - Portable', 'price' => 39.99, 'desc' => 'Cordless water flosser with 3 pressure settings.', 'sub' => null],
            ],

            'hair-care' => [
                ['name' => 'Keratin Shampoo - 500ml', 'price' => 14.99, 'desc' => 'Smoothing keratin shampoo for frizzy hair.', 'sub' => null],
                ['name' => 'Hair Oil - Argan', 'price' => 18.99, 'sale' => 14.99, 'desc' => 'Pure argan oil for hair. Tames frizz, adds shine.', 'sub' => null],
                ['name' => 'Hair Mask - Deep Conditioning', 'price' => 16.99, 'desc' => 'Intensive hair mask with keratin and coconut oil.', 'sub' => null],
                ['name' => 'Leave-In Conditioner Spray', 'price' => 12.99, 'desc' => 'Lightweight leave-in conditioner. Heat protection.', 'sub' => null],
                ['name' => 'Hair Growth Serum', 'price' => 24.99, 'sale' => 19.99, 'desc' => 'Biotin and caffeine hair growth serum.', 'sub' => null],
            ],

            'frozen-meat' => [
                ['name' => 'Premium Beef Steak - Ribeye 500g', 'price' => 24.99, 'desc' => 'Grass-fed ribeye steaks. Vacuum-sealed.', 'sub' => null],
                ['name' => 'Chicken Thighs - Boneless 1kg', 'price' => 12.99, 'sale' => 10.99, 'desc' => 'Boneless, skinless chicken thighs. Tender.', 'sub' => null],
                ['name' => 'Lamb Chops - 6 Pack', 'price' => 29.99, 'desc' => 'New Zealand lamb loin chops. Perfect for grilling.', 'sub' => null],
                ['name' => 'Ground Turkey - Lean 500g', 'price' => 9.99, 'desc' => 'Lean ground turkey, 93% fat-free.', 'sub' => null],
                ['name' => 'Pork Tenderloin - 400g', 'price' => 11.99, 'desc' => 'Lean pork tenderloin. Roasting or grilling.', 'sub' => null],
            ],

            'fish-seafood' => [
                ['name' => 'Salmon Fillets - Atlantic 500g', 'price' => 22.99, 'desc' => 'Wild-caught Atlantic salmon fillets. Omega-3 rich.', 'sub' => null],
                ['name' => 'Tiger Shrimp - Peeled 400g', 'price' => 18.99, 'sale' => 15.99, 'desc' => 'Jumbo tiger shrimp, peeled and deveined.', 'sub' => null],
                ['name' => 'Cod Fillets - 4 Pack', 'price' => 16.99, 'desc' => 'Boneless cod fillets. Mild flavor.', 'sub' => null],
                ['name' => 'Mussels - Fresh 500g', 'price' => 14.99, 'desc' => 'Fresh mussels, cleaned and devearded.', 'sub' => null],
                ['name' => 'Calamari Rings - 300g', 'price' => 11.99, 'desc' => 'Tender calamari rings. Lightly breaded.', 'sub' => null],
            ],

            'frozen-vegetables' => [
                ['name' => 'Broccoli Florets - 500g', 'price' => 4.99, 'desc' => 'Fresh-frozen broccoli florets. Flash-frozen.', 'sub' => null],
                ['name' => 'Spinach - Leaf 400g', 'price' => 3.99, 'sale' => 2.99, 'desc' => 'Chopped leaf spinach. Smoothies and sauteing.', 'sub' => null],
                ['name' => 'Corn Kernels - Sweet 500g', 'price' => 4.49, 'desc' => 'Sweet corn kernels. Ready to heat and serve.', 'sub' => null],
                ['name' => 'Mixed Stir-Fry Vegetables', 'price' => 5.49, 'desc' => 'Bell peppers, snap peas, carrots, and water chestnuts.', 'sub' => null],
                ['name' => 'Green Peas - 500g', 'price' => 3.49, 'desc' => 'Garden green peas. Sweet and tender.', 'sub' => null],
            ],

            'fruits' => [
                ['name' => 'Organic Bananas - 1kg', 'price' => 3.99, 'desc' => 'Fresh organic bananas. Rich in potassium.', 'sub' => null],
                ['name' => 'Strawberries - Premium 500g', 'price' => 6.99, 'sale' => 5.49, 'desc' => 'Sweet, juicy strawberries. Hand-picked.', 'sub' => null],
                ['name' => 'Blueberries - Organic 250g', 'price' => 7.99, 'desc' => 'Organic blueberries. Antioxidant rich.', 'sub' => null],
                ['name' => 'Avocados - 4 Pack', 'price' => 8.99, 'desc' => 'Hass avocados, perfectly ripe.', 'sub' => null],
                ['name' => 'Mixed Fruit Box - Seasonal', 'price' => 12.99, 'sale' => 10.99, 'desc' => 'Curated seasonal fruit selection.', 'sub' => null],
            ],

            'produce-vegetables' => [
                ['name' => 'Organic Spinach - 200g', 'price' => 3.49, 'desc' => 'Fresh organic baby spinach. Pre-washed.', 'sub' => null],
                ['name' => 'Bell Peppers - 3 Pack', 'price' => 4.99, 'desc' => 'Red, yellow, and green bell peppers.', 'sub' => null],
                ['name' => 'Broccoli - Fresh Head', 'price' => 2.99, 'sale' => 2.49, 'desc' => 'Fresh broccoli crowns. Vitamins C and K.', 'sub' => null],
                ['name' => 'Sweet Potatoes - 1kg', 'price' => 4.49, 'desc' => 'Organic sweet potatoes. Nutrient-dense.', 'sub' => null],
                ['name' => 'Cherry Tomatoes - 250g', 'price' => 3.99, 'desc' => 'Vine-ripened cherry tomatoes.', 'sub' => null],
            ],

            'daily-milk' => [
                ['name' => 'Whole Milk - 1 Gallon', 'price' => 4.99, 'desc' => 'Fresh whole milk. Calcium and vitamin D.', 'sub' => null],
                ['name' => 'Greek Yogurt - Plain 500g', 'price' => 5.99, 'sale' => 4.49, 'desc' => 'Thick and creamy Greek yogurt. High protein.', 'sub' => null],
                ['name' => 'Butter - Unsalted 250g', 'price' => 6.99, 'desc' => 'Premium unsalted butter. Baking and cooking.', 'sub' => null],
                ['name' => 'Cream Cheese - 200g', 'price' => 4.99, 'desc' => 'Smooth and spreadable cream cheese.', 'sub' => null],
                ['name' => 'Cheese Slices - Cheddar 200g', 'price' => 5.99, 'sale' => 4.99, 'desc' => 'Sliced cheddar cheese. Sandwiches and burgers.', 'sub' => null],
            ],

            'breakfast-cereals' => [
                ['name' => 'Granola - Honey & Almond 500g', 'price' => 8.99, 'desc' => 'Crunchy granola with honey and almonds.', 'sub' => null],
                ['name' => 'Rolled Oats - Organic 1kg', 'price' => 6.99, 'sale' => 5.49, 'desc' => 'Thick rolled oats for creamy porridge.', 'sub' => null],
                ['name' => 'Corn Flakes - Classic 375g', 'price' => 4.99, 'desc' => 'Classic crispy corn flakes. Fortified.', 'sub' => null],
                ['name' => 'Waffles - Frozen 6 Pack', 'price' => 5.99, 'desc' => 'Belgian-style frozen waffles.', 'sub' => null],
                ['name' => 'Pancake Mix - Buttermilk 450g', 'price' => 4.49, 'sale' => 3.49, 'desc' => 'Buttermilk pancake mix. Fluffy every time.', 'sub' => null],
            ],

            'drinks-beverages' => [
                ['name' => 'Green Tea - Organic 20 Bags', 'price' => 5.99, 'desc' => 'Organic green tea bags. Antioxidant rich.', 'sub' => null],
                ['name' => 'Cold Brew Coffee - Ready to Drink', 'price' => 4.99, 'sale' => 3.99, 'desc' => 'Smooth cold brew coffee. Low acidity.', 'sub' => null],
                ['name' => 'Orange Juice - Fresh Squeezed 1L', 'price' => 6.99, 'desc' => 'Freshly squeezed orange juice. No added sugar.', 'sub' => null],
                ['name' => 'Coconut Water - Natural 330ml', 'price' => 3.49, 'desc' => 'Natural coconut water. Electrolyte-rich.', 'sub' => null],
                ['name' => 'Protein Shake - Chocolate 400ml', 'price' => 5.99, 'sale' => 4.99, 'desc' => 'Ready-to-drink chocolate protein shake.', 'sub' => null],
            ],

            'snacks' => [
                ['name' => 'Mixed Nuts - Premium 400g', 'price' => 12.99, 'desc' => 'Premium blend of almonds, cashews, and walnuts.', 'sub' => null],
                ['name' => 'Dark Chocolate Bar - 70%', 'price' => 4.99, 'sale' => 3.99, 'desc' => 'Single-origin dark chocolate. 70% cocoa.', 'sub' => null],
                ['name' => 'Potato Chips - Sea Salt', 'price' => 3.99, 'desc' => 'Kettle-cooked potato chips. Sea salt.', 'sub' => null],
                ['name' => 'Hummus - Classic 200g', 'price' => 5.99, 'desc' => 'Creamy classic hummus with tahini.', 'sub' => null],
                ['name' => 'Trail Mix - Energy 300g', 'price' => 9.99, 'sale' => 7.99, 'desc' => 'Energy trail mix with nuts and dried fruits.', 'sub' => null],
            ],

            'pantry' => [
                ['name' => 'Tomato Sauce - Italian 500ml', 'price' => 4.99, 'desc' => 'Authentic Italian tomato sauce.', 'sub' => null],
                ['name' => 'Chicken Broth - Low Sodium 1L', 'price' => 3.99, 'sale' => 2.99, 'desc' => 'Rich chicken broth with reduced sodium.', 'sub' => null],
                ['name' => 'Peanut Butter - Natural 400g', 'price' => 7.99, 'desc' => 'Natural peanut butter. No added sugar.', 'sub' => null],
                ['name' => 'Maple Syrup - Pure 250ml', 'price' => 12.99, 'desc' => 'Pure Grade A maple syrup.', 'sub' => null],
                ['name' => 'Balsamic Vinegar - Aged 250ml', 'price' => 9.99, 'sale' => 7.99, 'desc' => 'Aged balsamic vinegar from Modena.', 'sub' => null],
            ],

            'vitamins-dietary-supplements' => [
                ['name' => 'Whey Protein - Vanilla 1kg', 'price' => 39.99, 'sale' => 32.99, 'desc' => 'Grass-fed whey protein isolate. 24g protein.', 'sub' => null],
                ['name' => 'BCAA Powder - Tropical', 'price' => 29.99, 'desc' => 'Branched-chain amino acids for recovery.', 'sub' => null],
                ['name' => 'Creatine Monohydrate - 500g', 'price' => 19.99, 'desc' => 'Pure creatine monohydrate. Strength support.', 'sub' => null],
                ['name' => 'Green Superfood Powder', 'price' => 34.99, 'sale' => 28.99, 'desc' => 'Blend of 40+ organic greens and fruits.', 'sub' => null],
                ['name' => 'Ashwagandha Root Extract', 'price' => 22.99, 'sale' => 18.99, 'desc' => 'Standardized ashwagandha extract. Stress relief.', 'sub' => null],
            ],
        ];

        $categories = Category::whereIn('slug', array_keys($allProducts))->get()->keyBy('slug');

        $allSubcats = Subcategory::with('category')->get()->keyBy(function ($s) {
            return $s->category->slug . '.' . $s->slug;
        });

        foreach ($allProducts as $catSlug => $products) {
            $category = $categories->get($catSlug);
            if (!$category) continue;

            foreach ($products as $data) {
                $hasSale = isset($data['sale']);
                $subId = null;
                if (!empty($data['sub'])) {
                    $key = $catSlug . '.' . $data['sub'];
                    $subcat = $allSubcats->get($key);
                    if ($subcat) $subId = $subcat->id;
                }

                $product = Product::create([
                    'name' => $data['name'],
                    'slug' => Str::slug($data['name']),
                    'sku' => strtoupper(Str::random(3) . '-' . rand(1000, 9999)),
                    'description' => $data['desc'],
                    'short_description' => Str::limit($data['desc'], 80),
                    'price' => $data['price'],
                    'sale_price' => $data['sale'] ?? null,
                    'sale_price_start_date' => $hasSale ? now()->subDays(5) : null,
                    'sale_price_end_date' => $hasSale ? now()->addMonth() : null,
                    'stock_quantity' => rand(10, 200),
                    'stock_status' => 'in_stock',
                    'category_id' => $category->id,
                    'subcategory_id' => $subId,
                    'brand_id' => $brands[array_rand($brands)],
                    'is_featured' => (bool) rand(0, 3),
                    'is_active' => true,
                ]);

                $this->createImage($product, $category->name);
            }
        }
    }
}
