<?php

namespace Database\Seeders;

use App\Models\User;
use App\Models\Collection;
use App\Models\Product;
use App\Models\ProductVariant;
use App\Models\ProductImage;
use App\Models\HeroSlide;
use App\Models\SiteSetting;
use App\Models\Coupon;
use App\Models\Order;
use App\Models\OrderItem;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Carbon\Carbon;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // 1. Seed Users
        $admin = User::create([
            'name' => 'Plottwear Admin',
            'email' => 'admin@plottwear.com',
            'password' => Hash::make('password'),
            'phone' => '254700000000',
            'role' => 'admin',
            'is_active' => true,
        ]);

        $customer = User::create([
            'name' => 'John Doe',
            'email' => 'customer@plottwear.com',
            'password' => Hash::make('password'),
            'phone' => '254711111111',
            'role' => 'customer',
            'is_active' => true,
        ]);

        // 2. Seed Collections
        $newWave = Collection::create([
            'name' => 'New Wave',
            'slug' => 'new-wave',
            'description' => 'Fresh drop energy. Clean lines, bold statement cuts, and modern streetwear aesthetics.',
            'sort_order' => 1,
            'hero_image_path' => '/storage/collections/new_wave_hero.jpg',
            'accent_color' => '#E8A0B4', // Pink accent
        ]);

        $goldDesigns = Collection::create([
            'name' => 'Gold Designs',
            'slug' => 'gold-designs',
            'description' => 'Archival streetwear items with a nostalgic touch. Warm parchment feel, gold accents, and vintage vibes.',
            'sort_order' => 2,
            'hero_image_path' => '/storage/collections/gold_designs_hero.jpg',
            'accent_color' => '#C9A84C', // Gold accent
        ]);

        $diamondDesigns = Collection::create([
            'name' => 'Diamond Designs',
            'slug' => 'diamond-designs',
            'description' => 'Gallery-level heritage garments. Generous whitespace layouts, minimal outlines, and chrome details.',
            'sort_order' => 3,
            'hero_image_path' => '/storage/collections/diamond_designs_hero.jpg',
            'accent_color' => '#9E9E9E', // Silver accent
        ]);

        // 3. Seed Site Settings
        SiteSetting::setValue('whatsapp_number', '+254700123456');
        SiteSetting::setValue('phone_number', '+254700123456');
        SiteSetting::setValue('instagram_url', 'https://instagram.com/plottwear');
        SiteSetting::setValue('tiktok_url', 'https://tiktok.com/@plottwear');
        SiteSetting::setValue('facebook_url', 'https://facebook.com/plottwear');
        SiteSetting::setValue('twitter_url', 'https://x.com/plottwear');
        SiteSetting::setValue('mpesa_till_number', '174379'); // Sandbox M-Pesa Lipa Na Mpesa Till Number
        SiteSetting::setValue('delivery_fee', '350'); // Default delivery fee in KES
        SiteSetting::setValue('delivery_rates', json_encode([
            ['id' => 1, 'location' => 'Nairobi CBD', 'fee' => 200],
            ['id' => 2, 'location' => 'Nairobi Suburbs (Westlands, Kilimani, Karen)', 'fee' => 350],
            ['id' => 3, 'location' => 'Kiambu / Thika / Ruiru', 'fee' => 400],
            ['id' => 4, 'location' => 'Upcountry Major Towns (Nakuru, Kisumu, Mombasa)', 'fee' => 500],
            ['id' => 5, 'location' => 'Rest of Kenya (Remote Outposts)', 'fee' => 650]
        ]));
        SiteSetting::setValue('payment_methods', json_encode([
            'cod' => true,
            'mpesa_till' => true,
            'stk_push' => true
        ]));
        SiteSetting::setValue('terms_and_conditions', "### Plottwear Terms & Conditions\n\nWelcome to Plottwear. By purchasing our products, you agree to the following terms:\n\n1. **Deliveries**: Deliveries are processed within 24-48 hours via ENA Coach (to town offices) or Wells Fargo (doorstep delivery).\n2. **Refunds & Exchanges**: Returns are accepted within 7 days in original unworn condition.\n3. **Payments**: All payments are made via M-Pesa STK Push checkout. Do not send cash to other phone numbers.");

        // 4. Seed Hero Slides
        HeroSlide::create([
            'title' => 'THE NEW WAVE',
            'subtitle' => 'Fresh Nairobi Streetwear Drop. Bold layouts, clean finishes.',
            'cta_label' => 'Shop New Wave',
            'cta_link' => '#new-wave',
            'image_path' => '/storage/hero/slide1.jpg',
            'sort_order' => 0,
            'is_active' => true,
        ]);

        HeroSlide::create([
            'title' => 'ARCHIVAL GOLD',
            'subtitle' => 'Warm vintage tones, rich heritage stitches, gold custom accents.',
            'cta_label' => 'Explore Gold',
            'cta_link' => '#gold-designs',
            'image_path' => '/storage/hero/slide2.jpg',
            'sort_order' => 1,
            'is_active' => true,
        ]);

        HeroSlide::create([
            'title' => 'DIAMOND LINE',
            'subtitle' => 'Sleek chrome lines, wide outlines, limited edition gallery fits.',
            'cta_label' => 'View Diamonds',
            'cta_link' => '#diamond-designs',
            'image_path' => '/storage/hero/slide3.jpg',
            'sort_order' => 2,
            'is_active' => true,
        ]);

        // 5. Seed Coupons
        Coupon::create([
            'code' => 'PLOTT10',
            'discount_type' => 'percentage',
            'discount_value' => 10,
            'min_order_amount' => 100000, // KES 1,000 (100000 cents)
            'max_uses' => 100,
            'used_count' => 0,
            'expires_at' => Carbon::now()->addDays(30),
            'is_active' => true,
        ]);

        Coupon::create([
            'code' => 'NAIROBI500',
            'discount_type' => 'flat',
            'discount_value' => 50000, // KES 500 (50000 cents)
            'min_order_amount' => 300000, // KES 3,000 (300000 cents)
            'max_uses' => 50,
            'used_count' => 0,
            'expires_at' => Carbon::now()->addDays(15),
            'is_active' => true,
        ]);

        // 6. Seed Products, Variants, and Images

        // --- NEW WAVE ---
        // Product 1
        $p1 = Product::create([
            'collection_id' => $newWave->id,
            'name' => 'New Wave Heavyweight Tee',
            'description' => '240GSM premium cotton tee. Screenprinted graphics on chest and back. Fits oversized. Double stitched collar.',
            'base_price' => 250000, // KES 2,500
            'is_active' => true,
            'sort_color' => 'black',
        ]);

        // Variants for P1
        $sizes = ['XS', 'S', 'M', 'L', 'XL', 'XXL'];
        $colors = ['Black', 'White', 'Pink'];
        foreach ($colors as $color) {
            foreach ($sizes as $size) {
                ProductVariant::create([
                    'product_id' => $p1->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(10, 25),
                    'price_override' => $color === 'Pink' ? 270000 : null, // pink is premium
                ]);
            }
        }

        // Images for P1
        ProductImage::create([
            'product_id' => $p1->id,
            'image_path' => '/storage/products/tee_black.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);
        ProductImage::create([
            'product_id' => $p1->id,
            'image_path' => '/storage/products/tee_white.jpg',
            'is_primary' => false,
            'sort_order' => 2,
        ]);
        ProductImage::create([
            'product_id' => $p1->id,
            'image_path' => '/storage/products/tee_pink.jpg',
            'is_primary' => false,
            'sort_order' => 3,
        ]);

        // Product 2
        $p2 = Product::create([
            'collection_id' => $newWave->id,
            'name' => 'Washed Street Hoodie',
            'description' => '400GSM french terry cotton hoodie. Acid-washed finish, drop shoulder, cropped body. Screenprint details on sleeves.',
            'base_price' => 450000, // KES 4,500
            'is_active' => true,
            'sort_color' => 'black',
        ]);
        foreach (['Black', 'White'] as $color) {
            foreach (['S', 'M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p2->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(5, 15),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p2->id,
            'image_path' => '/storage/products/hoodie_black.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 3
        $p3 = Product::create([
            'collection_id' => $newWave->id,
            'name' => 'Signature Cargo Jogger',
            'description' => 'Multi-pocket canvas joggers with elastic cuffs and pink branding accents. Heavy duty zippers.',
            'base_price' => 380000, // KES 3,800
            'is_active' => true,
            'sort_color' => 'pink',
        ]);
        foreach (['Black', 'Pink'] as $color) {
            foreach (['S', 'M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p3->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(5, 12),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p3->id,
            'image_path' => '/storage/products/jogger_pink.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // --- NEW WAVE (CONTINUED) ---
        // Product 8
        $p8 = Product::create([
            'collection_id' => $newWave->id,
            'name' => 'Cyberpunk Windbreaker',
            'description' => 'Water-resistant utility jacket with reflective silver decals and adjustable toggles. Packs into itself.',
            'base_price' => 580000, // KES 5,800
            'is_active' => true,
            'sort_color' => 'white',
        ]);
        foreach (['White', 'Black'] as $color) {
            foreach (['S', 'M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p8->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(8, 20),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p8->id,
            'image_path' => '/storage/products/windbreaker_white.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 9
        $p9 = Product::create([
            'collection_id' => $newWave->id,
            'name' => 'Neo-Gothic Graphic Longsleeve',
            'description' => 'Heavyweight longsleeve with high-density puff print gothic script down both arms. Raw edge hem.',
            'base_price' => 320000, // KES 3,200
            'is_active' => true,
            'sort_color' => 'black',
        ]);
        foreach (['Black', 'White'] as $color) {
            foreach (['M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p9->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(10, 25),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p9->id,
            'image_path' => '/storage/products/longsleeve_black.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 10
        $p10 = Product::create([
            'collection_id' => $newWave->id,
            'name' => 'Liquid Metal Print Tee',
            'description' => 'Liquid metallic chrome sheen print on front. 260GSM boxy fit jersey tee. Silicone washed.',
            'base_price' => 260000, // KES 2,600
            'is_active' => true,
            'sort_color' => 'pink',
        ]);
        foreach (['Pink', 'Black'] as $color) {
            foreach (['S', 'M', 'L', 'XL', 'XXL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p10->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(12, 30),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p10->id,
            'image_path' => '/storage/products/tee_liquid.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 11
        $p11 = Product::create([
            'collection_id' => $newWave->id,
            'name' => 'Tokyo Tech Utility Pants',
            'description' => 'Nylon tech pants featuring 3D utility cargo pockets, built-in webbing belt, and ankle adjustment straps.',
            'base_price' => 490000, // KES 4,900
            'is_active' => true,
            'sort_color' => 'black',
        ]);
        foreach (['Black', 'White'] as $color) {
            foreach (['S', 'M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p11->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(5, 15),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p11->id,
            'image_path' => '/storage/products/pants_tokyo.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 12
        $p12 = Product::create([
            'collection_id' => $newWave->id,
            'name' => 'Acid-Wash Boxy Sweater',
            'description' => 'Heavily acid washed knit sweater with distressed frayed detailing around cuffs. Oversized boxy silhouette.',
            'base_price' => 480000, // KES 4,800
            'is_active' => true,
            'sort_color' => 'pink',
        ]);
        foreach (['Pink', 'Black'] as $color) {
            foreach (['M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p12->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(6, 18),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p12->id,
            'image_path' => '/storage/products/sweater_acid.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);


        // --- GOLD DESIGNS ---
        // Product 4
        $p4 = Product::create([
            'collection_id' => $goldDesigns->id,
            'name' => 'Archival Corduroy Shacket',
            'description' => 'Thick ribbed corduroy shirt jacket. Warm parchment lining. Custom gold engraved metal buttons. Vintage fit.',
            'base_price' => 650000, // KES 6,500
            'is_active' => true,
            'sort_color' => 'white', // Warm parchment fits white/gold theme
        ]);
        foreach (['Parchment Gold', 'Vintage Brown'] as $color) {
            foreach (['M', 'L', 'XL', 'XXL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p4->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(3, 8),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p4->id,
            'image_path' => '/storage/products/cord_shacket.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 5
        $p5 = Product::create([
            'collection_id' => $goldDesigns->id,
            'name' => 'Vintage Knit Sweater',
            'description' => 'Heavy knit wool blend sweater. Jacquard knit pattern inspired by Nairobi architecture. Soft feel.',
            'base_price' => 520000, // KES 5,200
            'is_active' => true,
            'sort_color' => 'white',
        ]);
        foreach (['Cream Gold', 'Archive Amber'] as $color) {
            foreach (['S', 'M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p5->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(5, 10),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p5->id,
            'image_path' => '/storage/products/knit_sweater.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 13
        $p13 = Product::create([
            'collection_id' => $goldDesigns->id,
            'name' => 'Classic Golden Embroidered Hoodie',
            'description' => '420GSM luxury fleece hoodie. Featuring heavy satin stitch gold thread embroidery across the chest.',
            'base_price' => 540000, // KES 5,400
            'is_active' => true,
            'sort_color' => 'black',
        ]);
        foreach (['Black', 'White'] as $color) {
            foreach (['S', 'M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p13->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(10, 20),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p13->id,
            'image_path' => '/storage/products/hoodie_gold.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 14
        $p14 = Product::create([
            'collection_id' => $goldDesigns->id,
            'name' => 'Nostalgic Denim Trucker Jacket',
            'description' => '14oz Japanese raw denim trucker jacket. Distressed edges with custom gold rivet details and branded back patch.',
            'base_price' => 720000, // KES 7,200
            'is_active' => true,
            'sort_color' => 'white',
        ]);
        foreach (['Vintage Blue', 'Washed Indigo'] as $color) {
            foreach (['M', 'L', 'XL', 'XXL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p14->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(4, 10),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p14->id,
            'image_path' => '/storage/products/denim_trucker.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 15
        $p15 = Product::create([
            'collection_id' => $goldDesigns->id,
            'name' => 'Corduroy Cargo Trouser',
            'description' => 'Relaxed fit wide-wale corduroy trousers. Generous side cargo pockets with golden brand snaps.',
            'base_price' => 450000, // KES 4,500
            'is_active' => true,
            'sort_color' => 'pink',
        ]);
        foreach (['Warm Tan', 'Olive Gold'] as $color) {
            foreach (['S', 'M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p15->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(5, 12),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p15->id,
            'image_path' => '/storage/products/cord_trousers.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 16
        $p16 = Product::create([
            'collection_id' => $goldDesigns->id,
            'name' => 'Waffle Knit Thermal Top',
            'description' => 'Heavy waffle stitch long sleeve. Perfect for layering under a shacket or coat. Elastic cuffs.',
            'base_price' => 350000, // KES 3,500
            'is_active' => true,
            'sort_color' => 'white',
        ]);
        foreach (['Cream', 'Off-White', 'Black'] as $color) {
            foreach (['S', 'M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p16->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(15, 25),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p16->id,
            'image_path' => '/storage/products/waffle_thermal.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 17
        $p17 = Product::create([
            'collection_id' => $goldDesigns->id,
            'name' => 'Heritage Paisley Shirt',
            'description' => 'Silky soft cupro fabric paisley button-up shirt. Relaced resort collar and drop shoulder detail.',
            'base_price' => 390000, // KES 3,900
            'is_active' => true,
            'sort_color' => 'black',
        ]);
        foreach (['Paisley Black', 'Gold Leaf'] as $color) {
            foreach (['S', 'M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p17->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(8, 15),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p17->id,
            'image_path' => '/storage/products/paisley_shirt.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);


        // --- DIAMOND DESIGNS ---
        // Product 6
        $p6 = Product::create([
            'collection_id' => $diamondDesigns->id,
            'name' => 'Diamond Cut Utility Pants',
            'description' => 'Heavyweight cotton canvas carpenter pants. Features silver metal eyelets, custom diamond panel stitching, and key loops.',
            'base_price' => 550000, // KES 5,500
            'is_active' => true,
            'sort_color' => 'black',
        ]);
        foreach (['Charcoal Black', 'Chrome Gray'] as $color) {
            foreach (['S', 'M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p6->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(5, 10),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p6->id,
            'image_path' => '/storage/products/utility_pant.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 7
        $p7 = Product::create([
            'collection_id' => $diamondDesigns->id,
            'name' => 'Chrome Legacy Blazer',
            'description' => 'Structured linen-blend blazer. Minimalist single-breasted closing. Features metallic silver detailing on the lapel.',
            'base_price' => 800000, // KES 8,000
            'is_active' => true,
            'sort_color' => 'white',
        ]);
        foreach (['Chrome Silver', 'Onyx Black'] as $color) {
            foreach (['M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p7->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(2, 6),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p7->id,
            'image_path' => '/storage/products/legacy_blazer.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 18
        $p18 = Product::create([
            'collection_id' => $diamondDesigns->id,
            'name' => 'Platinum Boxy Hoodie',
            'description' => '450GSM ultra-dense cotton fleece. Dropped shoulders, no drawstrings, customized diamond double-stitched hood.',
            'base_price' => 620000, // KES 6,200
            'is_active' => true,
            'sort_color' => 'white',
        ]);
        foreach (['Platinum Gray', 'Onyx'] as $color) {
            foreach (['S', 'M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p18->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(8, 16),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p18->id,
            'image_path' => '/storage/products/hoodie_platinum.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 19
        $p19 = Product::create([
            'collection_id' => $diamondDesigns->id,
            'name' => 'Silver-Stitched Mockneck',
            'description' => 'Tight knit premium cotton blend mockneck with contrasting silver stitch details outlining panels.',
            'base_price' => 380000, // KES 3,800
            'is_active' => true,
            'sort_color' => 'black',
        ]);
        foreach (['Midnight Black', 'Chrome Ash'] as $color) {
            foreach (['S', 'M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p19->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(10, 20),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p19->id,
            'image_path' => '/storage/products/mockneck_silver.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 20
        $p20 = Product::create([
            'collection_id' => $diamondDesigns->id,
            'name' => 'Minimalist Chrome Zip Fleece',
            'description' => 'Polartec fleece jacket featuring high quality YKK chrome zippers and toggles. Adjustable bottom hem.',
            'base_price' => 590000, // KES 5,900
            'is_active' => true,
            'sort_color' => 'white',
        ]);
        foreach (['Chrome Mist', 'Slate Gray'] as $color) {
            foreach (['S', 'M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p20->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(6, 14),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p20->id,
            'image_path' => '/storage/products/fleece_chrome.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 21
        $p21 = Product::create([
            'collection_id' => $diamondDesigns->id,
            'name' => 'Diamond Jacquard Cardigan',
            'description' => 'Mohair blend relaxed fit cardigan with diamond jacquard knit pattern and brushed metal buttons.',
            'base_price' => 680000, // KES 6,800
            'is_active' => true,
            'sort_color' => 'black',
        ]);
        foreach (['Carbon Diamond', 'Pearl White'] as $color) {
            foreach (['S', 'M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p21->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(5, 10),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p21->id,
            'image_path' => '/storage/products/cardigan_diamond.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // Product 22
        $p22 = Product::create([
            'collection_id' => $diamondDesigns->id,
            'name' => 'Tailored Cargo Trouser',
            'description' => 'Slim fit tailored cargo trousers with hidden zip pockets, custom crease and silver hardware finishing.',
            'base_price' => 520000, // KES 5,200
            'is_active' => true,
            'sort_color' => 'black',
        ]);
        foreach (['Charcoal', 'Onyx'] as $color) {
            foreach (['S', 'M', 'L', 'XL'] as $size) {
                ProductVariant::create([
                    'product_id' => $p22->id,
                    'size' => $size,
                    'color' => $color,
                    'stock_qty' => rand(7, 15),
                ]);
            }
        }
        ProductImage::create([
            'product_id' => $p22->id,
            'image_path' => '/storage/products/trouser_tailored.jpg',
            'is_primary' => true,
            'sort_order' => 1,
        ]);

        // 7. Seed 123 Orders spanning the last 30 days
        $statuses = ['pending', 'paid', 'processing', 'shipped', 'delivered', 'cancelled'];
        $locations = [
            'Nairobi CBD (Pick up)', 
            'Westlands, Nairobi', 
            'Runda, Nairobi', 
            'Karen, Nairobi', 
            'Kiambu Road', 
            'Thika Town', 
            'Ruiru Bypass', 
            'Nakuru CBD', 
            'Mombasa Ganjoni', 
            'Kisumu Milimani'
        ];
        $customerNames = [
            'James Mwangi', 'Amina Yusuf', 'Brian Ochieng', 'Clara Chebet', 
            'David Ndwiga', 'Esther Wanjiku', 'Faraji Otieno', 'Grace Mutua', 
            'Ian Kiprop', 'Joy Nekesa', 'Kevin Omwamba', 'Lydia Nyambura'
        ];

        // Retrieve all variants to pick items from
        $variants = ProductVariant::with('product')->get();
        if ($variants->count() > 0) {
            for ($i = 1; $i <= 123; $i++) {
                // Determine date in the last 30 days
                $daysAgo = rand(0, 30);
                $orderDate = Carbon::now()->subDays($daysAgo)->subHours(rand(0, 23))->subMinutes(rand(0, 59));

                $status = $statuses[array_rand($statuses)];
                $location = $locations[array_rand($locations)];
                $customerName = $customerNames[array_rand($customerNames)];
                $email = strtolower(str_replace(' ', '.', $customerName)) . '@gmail.com';
                $phone = '2547' . rand(10000000, 99999999);

                // Create Order
                $isGuest = (rand(1, 10) > 4);
                $order = Order::create([
                    'user_id' => $isGuest ? null : $customer->id,
                    'guest_name' => $isGuest ? $customerName : null,
                    'guest_phone' => $isGuest ? $phone : null,
                    'delivery_location' => $location,
                    'delivery_method' => rand(0, 1) ? 'ena_coach' : 'wells_fargo',
                    'delivery_fee' => 35000, // KES 350
                    'subtotal' => 0, // updated below
                    'total' => 0,    // updated below
                    'status' => $status,
                    'mpesa_transaction_id' => ($status === 'paid' || $status === 'processing') ? 'MPESA' . rand(100000, 999999) : null,
                    'created_at' => $orderDate,
                    'updated_at' => $orderDate,
                ]);

                // Seed 1-3 items for this order
                $itemCount = rand(1, 3);
                $orderTotal = 0;
                $selectedVariants = $variants->random($itemCount);

                foreach ($selectedVariants as $var) {
                    $qty = rand(1, 2);
                    $price = $var->price_override ?? $var->product->base_price;
                    $lineTotal = $price * $qty;
                    $orderTotal += $lineTotal;

                    OrderItem::create([
                        'order_id' => $order->id,
                        'product_id' => $var->product_id,
                        'variant_id' => $var->id,
                        'quantity' => $qty,
                        'unit_price' => $price,
                        'created_at' => $orderDate,
                        'updated_at' => $orderDate,
                    ]);
                }

                // Update total
                $order->update([
                    'subtotal' => $orderTotal,
                    'total' => $orderTotal + 35000
                ]);
            }
        }
    }
}
