<?php

namespace Database\Seeders;

use App\Models\Article;
use App\Models\Category;
use App\Models\Product;
use App\Models\ProductImage;
use App\Models\ProductVariant;
use App\Models\Review;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // 1. Admin & Customer Users
        User::create([
            'name' => 'Home Decor Admin',
            'email' => 'admin@homedecor.com',
            'password' => bcrypt('pass@homedecore'),
            'is_admin' => true,
        ]);

        for ($i = 1; $i <= 5; $i++) {
            User::create([
                'name' => "Customer User {$i}",
                'email' => "customer{$i}@example.com",
                'password' => bcrypt('password'),
                'is_admin' => false,
            ]);
        }

        // 2. Settings
        $settings = [
            'business_name' => 'Home Decor Dubai - Wall Panels & Gypsum Molding Catalog',
            'logo' => '/logo.jpg',
            'whatsapp_number' => '+971523707206',
            'email' => 'bilaltajamaldxb@gmail.com',
            'phone' => '+971 523707206',
            'address' => 'UAE , Dubai Al Qouz industrial area 3 wharehouse no , C12',
            'announcement_bar' => 'HOME DECOR DUBAI • AL QUOZ INDUSTRIAL AREA 3 WAREHOUSE C12 • DIRECT WHATSAPP +971 523707206',
            'footer_text' => 'Curators of turn-of-the-century wall panels, gypsum moulding, corbels, ceiling rosettes, crown cornices, and architectural plaster fit-out details across Dubai and the UAE.',
            'instagram' => 'https://instagram.com',
            'pinterest' => 'https://pinterest.com',
        ];

        foreach ($settings as $key => $value) {
            Setting::create(['key' => $key, 'value' => $value]);
        }

        // 3. Categories & Subcategories
        $categoriesData = [
            [
                'name' => 'Gypsum Wall Molding & Panel',
                'description' => 'Architectural wall mouldings, wainscoting, decorative wall panels, cornices, media wall frames, and wood/PVC wall cladding.',
                'image' => 'https://images.unsplash.com/photo-1513694203232-719a280e022f?auto=format&fit=crop&w=800&q=80',
                'featured' => true,
                'subcategories' => [
                    'Wall Molding & Installation',
                    'Wainscoting Panels',
                    'Decorative Wall Panels',
                    'Media Wall Molding & Panels',
                    'PVC Wall Paneling',
                    'Wood Wall Cladding & Paint',
                    'Round Corners Molding Frames',
                    'Crown Molding & Ceiling Cornices',
                    'Ceiling Medallion Design & Installation',
                ]
            ],
            [
                'name' => 'Gypsum Ceiling & Partition',
                'description' => 'Bespoke gypsum false ceilings, drywall partitions, tray and cove-lit designs, coffered ceilings, and plastering services.',
                'image' => 'https://images.unsplash.com/photo-1513519245088-0e12902e5a38?auto=format&fit=crop&w=800&q=80',
                'featured' => true,
                'subcategories' => [
                    'Gypsum False Ceilings',
                    'Plain & Flat Ceilings',
                    'Tray & Cove-Lit Ceilings',
                    'Multi-Level & Coffered Ceiling Designs',
                    'POP Ceiling Design & Gypsum Corners',
                    'Suspended Ceiling Tiles',
                    'Drywall & Partition Systems',
                    'Standard Wall Linings',
                    'Wall & Ceiling Plastering',
                    'Residential & Commercial Project Solutions',
                ]
            ],
            [
                'name' => 'Interior & Exterior Paint',
                'description' => 'Professional interior and exterior painting services for residential villas, apartments, commercial offices, doors, and trim.',
                'image' => 'https://images.unsplash.com/photo-1584622650111-993a426fbf0a?auto=format&fit=crop&w=800&q=80',
                'featured' => true,
                'subcategories' => [
                    'Apartment Painting',
                    'Villa Painting',
                    'Office & Commercial Painting',
                    'Doors, Trim & Wood Painting',
                ]
            ],
            [
                'name' => 'Glass Work',
                'description' => 'Custom architectural glass installations, glass partitions, mirror walls, shower enclosures, and commercial glass solutions.',
                'image' => 'https://images.unsplash.com/photo-1507473885765-e6ed057f782c?auto=format&fit=crop&w=800&q=80',
                'featured' => true,
                'subcategories' => [
                    'Glass Partition Walls',
                    'Shower Enclosures & Glass Doors',
                    'Custom Mirrors & Glass Wall Panels',
                    'Decorative & Stained Glass',
                    'Commercial & Residential Glass Fit-Outs',
                ]
            ],
        ];

        $createdCategories = [];
        $sort = 1;
        foreach ($categoriesData as $cat) {
            $mainCategory = Category::create([
                'name' => $cat['name'],
                'slug' => Str::slug($cat['name']),
                'description' => $cat['description'],
                'image' => $cat['image'],
                'featured' => $cat['featured'],
                'sort_order' => $sort++,
                'status' => 'published',
            ]);

            $createdCategories[$cat['name']] = $mainCategory;

            $subSort = 1;
            foreach ($cat['subcategories'] as $subName) {
                $subCat = Category::create([
                    'parent_id' => $mainCategory->id,
                    'name' => $subName,
                    'slug' => Str::slug($cat['name'] . '-' . $subName),
                    'description' => "Premium {$subName} in our {$cat['name']} collection.",
                    'image' => $cat['image'],
                    'featured' => rand(0, 1) === 1,
                    'sort_order' => $subSort++,
                    'status' => 'published',
                ]);
                $createdCategories[$subName] = $subCat;
            }
        }

        // 4. Products Master Catalog Data (50+ products)
        $productsRaw = [
            // Lighting
            [
                'name' => 'Windsor Brass Wall Lamp',
                'category' => 'Wall Lamps',
                'sku' => 'LGT-WIN-001',
                'price' => 385.00,
                'material' => 'Solid Brass & Mouth-Blown Glass',
                'finish' => 'Unlacquered Aged Brass',
                'dimensions' => 'H 38cm x W 18cm x D 22cm',
                'weight' => '2.4 kg',
                'short' => 'An elegant Victorian-inspired wall sconce in unlacquered solid brass with an opal glass globe.',
                'desc' => 'Handcrafted by artisan brass founders, the Windsor Wall Lamp brings refined period warmth to hallways, dining rooms, and bedside alcoves. Over time, the unlacquered brass will naturally acquire a rich, warm patina.',
                'featured' => true, 'new' => true, 'bestseller' => true, 'editor' => true,
                'images' => [
                    'https://images.unsplash.com/photo-1507473885765-e6ed057f782c?auto=format&fit=crop&w=800&q=80',
                    'https://images.unsplash.com/photo-1540932239986-30128078f3c5?auto=format&fit=crop&w=800&q=80'
                ]
            ],
            [
                'name' => 'Heritage Opal Pendant Lamp',
                'category' => 'Pendant Lamps',
                'sku' => 'LGT-HER-002',
                'price' => 450.00,
                'material' => 'Opal Glass & Antiqued Brass',
                'finish' => 'Antiqued Brass',
                'dimensions' => 'Diameter 32cm, Chain 150cm',
                'weight' => '3.1 kg',
                'short' => 'Classic ribbed opal glass pendant shade suspended by a twisted fabric cord and aged brass gallery.',
                'desc' => 'Inspired by 1920s Parisian bistro fixtures, the Heritage Opal Pendant delivers soft ambient illumination above kitchen islands or breakfast nooks.',
                'featured' => true, 'new' => true, 'bestseller' => false, 'editor' => true,
                'images' => [
                    'https://images.unsplash.com/photo-1524484485831-a92ffc0de03f?auto=format&fit=crop&w=800&q=80',
                    'https://images.unsplash.com/photo-1513506003901-1e6a229e2d15?auto=format&fit=crop&w=800&q=80'
                ]
            ],
            [
                'name' => 'Edwardian Fluted Table Lamp',
                'category' => 'Table Lamps',
                'sku' => 'LGT-EDW-003',
                'price' => 295.00,
                'material' => 'Cast Brass & Silk Shade',
                'finish' => 'Polished Nickel',
                'dimensions' => 'H 55cm, Base 16cm',
                'weight' => '3.8 kg',
                'short' => 'Heavy cast brass table lamp base with fluted detailing and ivory silk pleated shade.',
                'desc' => 'Constructed with substantial cast weight, this table lamp features hand-chased fluting and a warm ivory silk shade.',
                'featured' => false, 'new' => false, 'bestseller' => true, 'editor' => false,
                'images' => [
                    'https://images.unsplash.com/photo-1517991104123-1d56a6e81ed9?auto=format&fit=crop&w=800&q=80'
                ]
            ],
            [
                'name' => 'Strand Classic Outdoor Lantern',
                'category' => 'Outdoor Lamps',
                'sku' => 'LGT-STR-004',
                'price' => 520.00,
                'material' => 'Copper & Brass',
                'finish' => 'Raw Copper & Dark Bronze',
                'dimensions' => 'H 48cm x W 24cm',
                'weight' => '4.2 kg',
                'short' => 'Weatherproof outdoor wall lantern forged in solid copper with clear bevelled glass panels.',
                'desc' => 'Designed to withstand coastal weather conditions while aging gracefully into an unmistakable verdigris hue.',
                'featured' => false, 'new' => true, 'bestseller' => false, 'editor' => true,
                'images' => [
                    'https://images.unsplash.com/photo-1509316975850-ff9c5deb0cd9?auto=format&fit=crop&w=800&q=80'
                ]
            ],
            [
                'name' => 'Somerset Flush Ceiling Light',
                'category' => 'Ceiling Lamps',
                'sku' => 'LGT-SOM-005',
                'price' => 310.00,
                'material' => 'Etched Glass & Brass',
                'finish' => 'Burnished Brass',
                'dimensions' => 'Diameter 30cm x H 14cm',
                'weight' => '2.1 kg',
                'short' => 'Architectural flush-mount ceiling fixture with frosted starburst etched glass dome.',
                'desc' => 'Ideal for rooms with lower ceiling heights needing sophisticated ambient illumination without compromising on historical detail.',
                'featured' => false, 'new' => false, 'bestseller' => false, 'editor' => false,
                'images' => [
                    'https://images.unsplash.com/photo-1513506003901-1e6a229e2d15?auto=format&fit=crop&w=800&q=80'
                ]
            ],

            // Bathroom
            [
                'name' => 'Kensington Heritage Basin Tap',
                'category' => 'Faucets',
                'sku' => 'BTH-KEN-001',
                'price' => 420.00,
                'material' => 'Solid Brass & Ceramic Indices',
                'finish' => 'Unlacquered Brass',
                'dimensions' => 'Spout Reach 145mm, H 210mm',
                'weight' => '2.9 kg',
                'short' => 'Traditional three-hole basin mixer tap with crosshead handles and white porcelain HOT/COLD indices.',
                'desc' => 'The Kensington Basin Tap is modeled after turn-of-the-century English sanitary fittings. Precision engineered ceramic disc valves ensure effortless control.',
                'featured' => true, 'new' => false, 'bestseller' => true, 'editor' => true,
                'images' => [
                    'https://images.unsplash.com/photo-1584622650111-993a426fbf0a?auto=format&fit=crop&w=800&q=80',
                    'https://images.unsplash.com/photo-1620626011761-996317b8d101?auto=format&fit=crop&w=800&q=80'
                ]
            ],
            [
                'name' => 'Burlington Ceramic Washbasin',
                'category' => 'Washbasins',
                'sku' => 'BTH-BUR-002',
                'price' => 680.00,
                'material' => 'Vitreous China & Cast Iron Pedestal',
                'finish' => 'Glazed White Ceramic',
                'dimensions' => 'W 650mm x D 475mm x H 880mm',
                'weight' => '28 kg',
                'short' => 'Freestanding Victorian pedestal washbasin with splashback rim and 3 tap holes.',
                'desc' => 'Crafted from heavy vitreous china with high-gloss glaze resistant to scratching and fading. Features classic period splashguard detailing.',
                'featured' => true, 'new' => true, 'bestseller' => true, 'editor' => false,
                'images' => [
                    'https://images.unsplash.com/photo-1584622650111-993a426fbf0a?auto=format&fit=crop&w=800&q=80'
                ]
            ],
            [
                'name' => 'Mayfair Cast Iron Bathtub',
                'category' => 'Bathtubs',
                'sku' => 'BTH-MAY-003',
                'price_on_request' => true,
                'price' => null,
                'material' => 'Enamelled Cast Iron & Eagle Claw Feet',
                'finish' => 'Raw Exterior (Ready for Paint)',
                'dimensions' => 'L 1700mm x W 760mm x H 680mm',
                'weight' => '142 kg',
                'short' => 'Roll-top double-ended clawfoot bathtub cast in solid iron with genuine white titanium enamel interior.',
                'desc' => 'The crown jewel of period bathroom architecture. Excellent heat retention properties ensure luxurious prolonged bathing sessions.',
                'featured' => true, 'new' => false, 'bestseller' => true, 'editor' => true,
                'images' => [
                    'https://images.unsplash.com/photo-1584622650111-993a426fbf0a?auto=format&fit=crop&w=800&q=80'
                ]
            ],
            [
                'name' => 'Thermostatic Exposed Shower Suite',
                'category' => 'Showers',
                'sku' => 'BTH-SHO-004',
                'price' => 1250.00,
                'material' => 'Solid Brass & Porcelain Handset',
                'finish' => 'Polished Nickel',
                'dimensions' => 'Overhead Rose 200mm, Rail H 1200mm',
                'weight' => '7.5 kg',
                'short' => 'Exposed thermostatic shower column with 8-inch rain rose and telephone-style hand spray.',
                'desc' => 'Delivers high-flow thermostatic water pressure while maintaining precise temperature stability to prevent scalding.',
                'featured' => false, 'new' => true, 'bestseller' => false, 'editor' => true,
                'images' => [
                    'https://images.unsplash.com/photo-1620626011761-996317b8d101?auto=format&fit=crop&w=800&q=80'
                ]
            ],
            [
                'name' => 'Edwardian Mahogany Vanity Unit',
                'category' => 'Bathroom Furniture',
                'sku' => 'BTH-EDW-005',
                'price' => 1850.00,
                'material' => 'Solid Oak Wood & Carrara Marble Top',
                'finish' => 'Aged Oak & White Marble',
                'dimensions' => 'W 900mm x D 550mm x H 850mm',
                'weight' => '54 kg',
                'short' => 'Hand-carved solid oak vanity with genuine Italian Carrara marble countertop and integrated basin.',
                'desc' => 'Features soft-closing dovetail drawers and brass cup pull handles designed specifically for humid bathroom environments.',
                'featured' => true, 'new' => false, 'bestseller' => false, 'editor' => true,
                'images' => [
                    'https://images.unsplash.com/photo-1584622650111-993a426fbf0a?auto=format&fit=crop&w=800&q=80'
                ]
            ],

            // Kitchen
            [
                'name' => 'Cotswold Bridge Kitchen Tap',
                'category' => 'Kitchen Faucets',
                'sku' => 'KTC-COT-001',
                'price' => 490.00,
                'material' => 'Solid Brass',
                'finish' => 'Aged Brass',
                'dimensions' => 'Height 360mm, Spout Swivel 360°',
                'weight' => '3.4 kg',
                'short' => 'High-arch bridge kitchen faucet with twin lever porcelain handles and swivel spout.',
                'desc' => 'Hand-assembled bridge mixer tap featuring Swiss aerator nozzles for splash-free stream performance.',
                'featured' => true, 'new' => true, 'bestseller' => true, 'editor' => true,
                'images' => [
                    'https://images.unsplash.com/photo-1556911220-e15b29be8c8f?auto=format&fit=crop&w=800&q=80'
                ]
            ],
            [
                'name' => 'Belfast Double Bowl Apron Sink',
                'category' => 'Sinks',
                'sku' => 'KTC-BEL-002',
                'price' => 740.00,
                'material' => 'Fireclay Ceramic',
                'finish' => 'Glazed White',
                'dimensions' => 'L 800mm x W 500mm x D 250mm',
                'weight' => '48 kg',
                'short' => 'Double-bowl fireclay butler sink with deep basin walls and central weir overflow.',
                'desc' => 'Fired at 1200°C for extreme durability, resistant to thermal shock, chemicals, and everyday impact.',
                'featured' => true, 'new' => false, 'bestseller' => true, 'editor' => false,
                'images' => [
                    'https://images.unsplash.com/photo-1556911220-e15b29be8c8f?auto=format&fit=crop&w=800&q=80'
                ]
            ],
            [
                'name' => 'Raw Brass Cabinet Cup Pull',
                'category' => 'Handles',
                'sku' => 'HWD-PUL-001',
                'price' => 18.50,
                'material' => 'Heavy Cast Brass',
                'finish' => 'Unlacquered Raw Brass',
                'dimensions' => 'Hole Centers 96mm, Overall 112mm',
                'weight' => '0.18 kg',
                'short' => 'Traditional shell cup pull handle for kitchen drawers and pantry cabinetry.',
                'desc' => 'Cast in unlacquered solid brass with rear fixing screws. Ages over time into an authentic warm olive brass tone.',
                'featured' => false, 'new' => true, 'bestseller' => true, 'editor' => false,
                'images' => [
                    'https://images.unsplash.com/photo-1513694203232-719a280e022f?auto=format&fit=crop&w=800&q=80'
                ]
            ],

            // Hardware
            [
                'name' => 'Victorian Oak Door Handle Set',
                'category' => 'Door Handles',
                'sku' => 'HWD-VIC-002',
                'price' => 145.00,
                'material' => 'FSC Certified Oak & Brass Backplate',
                'finish' => 'Natural Oak & Polished Brass',
                'dimensions' => 'Backplate 210mm x 45mm, Knob 52mm',
                'weight' => '0.85 kg',
                'short' => 'Turned wooden mortise door knob set with detailed brass escutcheon backplate.',
                'desc' => 'Faithful reproduction of 19th-century Scandinavian and English interior door hardware.',
                'featured' => true, 'new' => true, 'bestseller' => true, 'editor' => true,
                'images' => [
                    'https://images.unsplash.com/photo-1513694203232-719a280e022f?auto=format&fit=crop&w=800&q=80'
                ]
            ],
            [
                'name' => 'Edwardian Triple Coat Hook',
                'category' => 'Hooks',
                'sku' => 'HWD-HK-003',
                'price' => 32.00,
                'material' => 'Solid Brass',
                'finish' => 'Aged Antique Brass',
                'dimensions' => 'H 140mm x Projection 90mm',
                'weight' => '0.35 kg',
                'short' => 'Heavy duty triple hat and coat hook with top porcelain tip button.',
                'desc' => 'Ideal for hallway coat racks, mudrooms, and behind bathroom doors.',
                'featured' => false, 'new' => false, 'bestseller' => true, 'editor' => false,
                'images' => [
                    'https://images.unsplash.com/photo-1513694203232-719a280e022f?auto=format&fit=crop&w=800&q=80'
                ]
            ],
            [
                'name' => 'Heavy Brass Rim Lock Set',
                'category' => 'Locks',
                'sku' => 'HWD-LCK-004',
                'price' => 210.00,
                'material' => 'Solid Brass & Cast Iron Mechanism',
                'finish' => 'Blackened Iron & Polished Brass',
                'dimensions' => 'Lockcase 150mm x 105mm',
                'weight' => '1.9 kg',
                'short' => 'Surface-mounted period door rim lock with skeleton keys and brass keeper.',
                'desc' => 'Hand-assembled dual lever mechanism designed for internal timber doors.',
                'featured' => false, 'new' => true, 'bestseller' => false, 'editor' => true,
                'images' => [
                    'https://images.unsplash.com/photo-1513694203232-719a280e022f?auto=format&fit=crop&w=800&q=80'
                ]
            ],

            // Tiles
            [
                'name' => 'Regent Patterned Encaustic Tile',
                'category' => 'Encaustic Patterned Tiles',
                'sku' => 'TIL-REG-001',
                'price' => 88.00,
                'material' => 'Handmade Cement & Natural Pigment',
                'finish' => 'Matte Sealant Finish',
                'dimensions' => '20cm x 20cm x 1.6cm thickness',
                'weight' => '1.3 kg / tile',
                'short' => 'Hand-pressed cement floor tile with classic geometric star and floral motif.',
                'desc' => 'Made using traditional 19th-century hydraulic press techniques. Suitable for indoor hallways, kitchens, and bathrooms.',
                'featured' => true, 'new' => true, 'bestseller' => true, 'editor' => true,
                'images' => [
                    'https://images.unsplash.com/photo-1523413651479-597ee2dae4bc?auto=format&fit=crop&w=800&q=80'
                ]
            ],
            [
                'name' => 'Belgrave Octagon & Dot Floor Tile',
                'category' => 'Victorian Floor Tiles',
                'sku' => 'TIL-BEL-002',
                'price' => 95.00,
                'material' => 'Fully Vitrified Porcelain',
                'finish' => 'Unglazed Matte Black & Cream',
                'dimensions' => 'Octagon 15cm x 15cm, Taco Dot 3.5cm',
                'weight' => '22 kg / sq metre',
                'short' => 'Traditional Victorian hallway ceramic floor tile sheet with contrasting black square dots.',
                'desc' => 'Extremely hardwearing vitrified porcelain engineered to endure generations of foot traffic.',
                'featured' => true, 'new' => false, 'bestseller' => true, 'editor' => false,
                'images' => [
                    'https://images.unsplash.com/photo-1523413651479-597ee2dae4bc?auto=format&fit=crop&w=800&q=80'
                ]
            ],

            // Wood
            [
                'name' => 'Classic Oak Wall Panel',
                'category' => 'Wall Panels',
                'sku' => 'WOD-OAK-001',
                'price' => 125.00,
                'material' => '100% Solid European Oak',
                'finish' => 'Natural Hand-Waxed Oil',
                'dimensions' => 'W 600mm x H 1200mm per section',
                'weight' => '8.2 kg',
                'short' => 'Bespoke wainscoting timber wall panel with tongue and groove fitting edge.',
                'desc' => 'Instant architectural elevation for dining rooms, staircases, and master suites.',
                'featured' => true, 'new' => true, 'bestseller' => true, 'editor' => true,
                'images' => [
                    'https://images.unsplash.com/photo-1513694203232-719a280e022f?auto=format&fit=crop&w=800&q=80'
                ]
            ],
            [
                'name' => 'Herringbone Aged Oak Parquet',
                'category' => 'Parquet Flooring',
                'sku' => 'WOD-HER-002',
                'price' => 110.00,
                'material' => 'Engineered European Oak',
                'finish' => 'Smoked & Brushed Natural Matte Oil',
                'dimensions' => '70mm x 490mm x 15mm thickness',
                'weight' => '14 kg / pack',
                'short' => 'Smoked oak parquet blocks for classic French herringbone and chevron floor patterns.',
                'desc' => 'Distressed edge bevels provide instant antique character while compatible with underfloor heating systems.',
                'featured' => false, 'new' => false, 'bestseller' => true, 'editor' => true,
                'images' => [
                    'https://images.unsplash.com/photo-1513694203232-719a280e022f?auto=format&fit=crop&w=800&q=80'
                ]
            ],

            // Home Decor
            [
                'name' => 'Archival Gilded Overmantel Mirror',
                'category' => 'Decorative Mirrors',
                'sku' => 'DEC-MIR-001',
                'price' => 890.00,
                'material' => 'Hand-Carved Wood & Gold Leaf',
                'finish' => 'Antiqued Gold Leaf',
                'dimensions' => 'W 110cm x H 135cm',
                'weight' => '18.5 kg',
                'short' => 'Grand arched fireplace overmantel mirror with beaded frame and crest moulding.',
                'desc' => 'Features subtle antiqued mercury mirror glass foxing for authentic period charm above hearths.',
                'featured' => true, 'new' => true, 'bestseller' => true, 'editor' => true,
                'images' => [
                    'https://images.unsplash.com/photo-1513519245088-0e12902e5a38?auto=format&fit=crop&w=800&q=80'
                ]
            ]
        ];

        // Expand to generate 50+ total rich realistic items across all category groups
        $sampleFinishes = ['Unlacquered Brass', 'Polished Nickel', 'Antique Bronze', 'Aged Copper', 'Burnished Brass', 'Chrome'];
        $sampleMaterials = ['Solid Brass', 'Vitreous China', 'European Oak', 'Carrara Marble', 'Cast Iron', 'Mouth-Blown Glass'];

        $allCategoriesList = Category::whereNotNull('parent_id')->get();
        $counter = count($productsRaw) + 1;

        foreach ($allCategoriesList as $subCat) {
            // Add 2 extra products per subcategory to ensure 50+ catalog depth
            for ($k = 1; $k <= 2; $k++) {
                $pName = "{$subCat->name} Collection Piece {$k}";
                $pSku = strtoupper(substr($subCat->slug, 0, 3)) . "-DEMO-" . str_pad($counter, 3, '0', STR_PAD_LEFT);
                $pPrice = rand(45, 950) + (rand(0, 1) === 0 ? 0.00 : 0.50);
                $pPriceOnRequest = ($pPrice > 750 && rand(0, 1) === 1);

                $pObj = Product::create([
                    'category_id' => $subCat->id,
                    'name' => $pName,
                    'slug' => Str::slug($pName . '-' . $counter),
                    'sku' => $pSku,
                    'short_description' => "Architectural {$subCat->name} featuring hand-finished detailing and timeless craftsmanship.",
                    'description' => "Designed for heritage restorations and contemporary period interiors alike, this {$subCat->name} is crafted from {$sampleMaterials[array_rand($sampleMaterials)]} with an authentic {$sampleFinishes[array_rand($sampleFinishes)]} finish. Built to endure and age beautifully over time.",
                    'price' => $pPriceOnRequest ? null : $pPrice,
                    'price_on_request' => $pPriceOnRequest,
                    'availability' => 'in_stock',
                    'stock_status' => 'in_stock',
                    'featured' => rand(0, 3) === 1,
                    'new_arrival' => rand(0, 2) === 1,
                    'best_seller' => rand(0, 2) === 1,
                    'editor_pick' => rand(0, 3) === 1,
                    'status' => 'published',
                    'material' => $sampleMaterials[array_rand($sampleMaterials)],
                    'color' => 'Neutral Warm',
                    'finish' => $sampleFinishes[array_rand($sampleFinishes)],
                    'dimensions' => 'Standard Architectural Fit',
                    'weight' => rand(1, 15) . ' kg',
                    'care_instructions' => 'Wipe clean with a soft dry cloth. Avoid abrasive household chemical detergents to preserve natural patina.',
                    'shipping_information' => 'Dispatched in protective wooden crate packaging within 3-5 business days.',
                    'seo_title' => "{$pName} | Sekelskifte Heritage Catalog",
                    'seo_description' => "Discover {$pName} handcrafted for period interiors.",
                ]);

                // Product Images
                ProductImage::create([
                    'product_id' => $pObj->id,
                    'image_path' => $subCat->parent ? $subCat->parent->image : $subCat->image,
                    'sort_order' => 1,
                    'is_primary' => true,
                ]);

                // Product Variants
                foreach (array_slice($sampleFinishes, 0, 3) as $idx => $f) {
                    ProductVariant::create([
                        'product_id' => $pObj->id,
                        'name' => "Finish: {$f}",
                        'sku' => "{$pSku}-{$idx}",
                        'price' => $pPriceOnRequest ? null : ($pPrice + ($idx * 15)),
                        'stock_status' => 'in_stock',
                        'attributes' => ['finish' => $f],
                    ]);
                }

                $counter++;
            }
        }

        // Insert explicit master raw products
        foreach ($productsRaw as $item) {
            $matchedCategory = Category::where('name', $item['category'])->first();
            if (!$matchedCategory) {
                $matchedCategory = Category::whereNotNull('parent_id')->first();
            }

            $prod = Product::create([
                'category_id' => $matchedCategory->id,
                'name' => $item['name'],
                'slug' => Str::slug($item['name']),
                'sku' => $item['sku'],
                'short_description' => $item['short'],
                'description' => $item['desc'],
                'price' => isset($item['price_on_request']) && $item['price_on_request'] ? null : $item['price'],
                'price_on_request' => $item['price_on_request'] ?? false,
                'availability' => 'in_stock',
                'stock_status' => 'in_stock',
                'featured' => $item['featured'] ?? false,
                'new_arrival' => $item['new'] ?? false,
                'best_seller' => $item['bestseller'] ?? false,
                'editor_pick' => $item['editor'] ?? false,
                'status' => 'published',
                'material' => $item['material'] ?? 'Solid Brass',
                'color' => 'Natural / Warm Neutral',
                'finish' => $item['finish'] ?? 'Unlacquered Brass',
                'dimensions' => $item['dimensions'] ?? 'Standard Fit',
                'weight' => $item['weight'] ?? '2.0 kg',
                'care_instructions' => 'Wipe with soft lint-free cloth. Do not use chemical polished cleaners.',
                'shipping_information' => 'Shipped in anti-tarnish protective wrapping with signature delivery.',
                'seo_title' => "{$item['name']} | Heritage Interiors",
                'seo_description' => $item['short'],
            ]);

            foreach ($item['images'] as $imgIndex => $imgUrl) {
                ProductImage::create([
                    'product_id' => $prod->id,
                    'image_path' => $imgUrl,
                    'sort_order' => $imgIndex + 1,
                    'is_primary' => $imgIndex === 0,
                ]);
            }

            // Variants
            foreach (['Unlacquered Brass', 'Polished Nickel', 'Antique Bronze'] as $vIdx => $vFinish) {
                ProductVariant::create([
                    'product_id' => $prod->id,
                    'name' => "Finish: {$vFinish}",
                    'sku' => "{$prod->sku}-{$vIdx}",
                    'price' => $prod->price_on_request ? null : ($prod->price ? $prod->price + ($vIdx * 20) : null),
                    'stock_status' => 'in_stock',
                    'attributes' => ['finish' => $vFinish],
                ]);
            }
        }

        // 5. Journal Articles (10 Articles)
        $articles = [
            [
                'title' => 'How to Create a Period-Style Bathroom',
                'category' => 'Bathroom Inspiration',
                'excerpt' => 'Key architectural proportions, traditional sanitariware selection, and unlacquered brass fitting pairings.',
                'content' => 'Restoring or constructing a period bathroom requires careful attention to historical balance. High-tank toilets, cast iron pedestal washbasins, and thermostatic exposed shower columns create an authentic Victorian atmosphere while providing modern performance.',
                'image' => 'https://images.unsplash.com/photo-1584622650111-993a426fbf0a?auto=format&fit=crop&w=800&q=80',
            ],
            [
                'title' => 'Choosing the Right Brass Hardware',
                'category' => 'Hardware Guide',
                'excerpt' => 'Understanding raw unlacquered brass, burnished finishes, and natural aging patinas.',
                'content' => 'Unlacquered brass is a living finish that evolves alongside your home. Unlike lacquered alternatives that crack and peel over time, unlacquered brass reacts gracefully to touch and atmosphere, acquiring a soft, rich olive bronze patina.',
                'image' => 'https://images.unsplash.com/photo-1513694203232-719a280e022f?auto=format&fit=crop&w=800&q=80',
            ],
            [
                'title' => 'Classic Kitchen Design Ideas',
                'category' => 'Kitchen Editorial',
                'excerpt' => 'Combining fireclay Belfast sinks, hand-carved oak, and bridge taps for timeless hearth living.',
                'content' => 'The traditional kitchen is built around central work zones: the generous aproned sink, solid timber prep surfaces, and substantial cabinet hardware.',
                'image' => 'https://images.unsplash.com/photo-1556911220-e15b29be8c8f?auto=format&fit=crop&w=800&q=80',
            ],
            [
                'title' => 'How to Mix Brass and Chrome',
                'category' => 'Interior Design',
                'excerpt' => 'Rules for mixing warm and cool metallic finishes across hardware, faucets, and lighting fixtures.',
                'content' => 'Mixing metal finishes adds depth and layered history to an interior. Use brass for warm touchpoints like handles and light sconces, while anchoring wet zones with polished chrome taps.',
                'image' => 'https://images.unsplash.com/photo-1620626011761-996317b8d101?auto=format&fit=crop&w=800&q=80',
            ],
            [
                'title' => 'Restoring a Heritage Home',
                'category' => 'Architecture',
                'excerpt' => 'Preserving original timber mouldings, ceiling roses, and period window fasteners.',
                'content' => 'Architectural preservation focuses on repairing original timber details before considering replacement. When replacing missing items, select faithful historic reproductions.',
                'image' => 'https://images.unsplash.com/photo-1513519245088-0e12902e5a38?auto=format&fit=crop&w=800&q=80',
            ],
            [
                'title' => 'Choosing Vintage-Inspired Lighting',
                'category' => 'Lighting Guide',
                'excerpt' => 'Selecting mouth-blown glass globes, fluted shades, and ambient wall sconces.',
                'content' => 'Lighting sets the atmospheric mood of period architecture. Dimmer-controlled warm incandescent or filament LED bulbs paired with opal glass globes soften sharp room angles.',
                'image' => 'https://images.unsplash.com/photo-1507473885765-e6ed057f782c?auto=format&fit=crop&w=800&q=80',
            ],
            [
                'title' => 'Victorian Tile Patterns Explained',
                'category' => 'Tiles & Flooring',
                'excerpt' => 'Geometric hallway layouts, taco dot borders, and encaustic cement craftsmanship.',
                'content' => 'Victorian tile floors are renowned for intricate geometric precision. Laying octagonal cream tiles with black taco inserts frames entryways with structural rhythm.',
                'image' => 'https://images.unsplash.com/photo-1523413651479-597ee2dae4bc?auto=format&fit=crop&w=800&q=80',
            ],
            [
                'title' => 'The Art of Timber Wainscoting',
                'category' => 'Woodwork',
                'excerpt' => 'Wall panel proportions, dado rails, and period skirting height guidelines.',
                'content' => 'Wainscoting paneling protects lower walls while introducing vertical perspective. Height proportioning according to the golden ratio yields harmonious room balance.',
                'image' => 'https://images.unsplash.com/photo-1513694203232-719a280e022f?auto=format&fit=crop&w=800&q=80',
            ],
            [
                'title' => 'Outdoor Architectural Lighting',
                'category' => 'Outdoor & Exterior',
                'excerpt' => 'Copper lanterns, verdigris patinas, and IP-rated weatherproof garden fixtures.',
                'content' => 'Exterior lighting should greet guests with warm, low-glare illumination. Solid copper lanterns oxidize naturally over time into an unmistakable verdigris patina.',
                'image' => 'https://images.unsplash.com/photo-1509316975850-ff9c5deb0cd9?auto=format&fit=crop&w=800&q=80',
            ],
            [
                'title' => 'Caring for Handcrafted Ceramics',
                'category' => 'Care & Maintenance',
                'excerpt' => 'Maintaining high-gloss fireclay glazes and vintage vitreous china pedestal sinks.',
                'content' => 'Vitreous china and fireclay are durable ceramic materials. Regular gentle cleaning with mild liquid soap preserves high-gloss protective glazes for decades.',
                'image' => 'https://images.unsplash.com/photo-1584622650111-993a426fbf0a?auto=format&fit=crop&w=800&q=80',
            ]
        ];

        foreach ($articles as $art) {
            Article::create([
                'title' => $art['title'],
                'slug' => Str::slug($art['title']),
                'category' => $art['category'],
                'excerpt' => $art['excerpt'],
                'content' => $art['content'],
                'image' => $art['image'],
                'status' => 'published',
                'seo_title' => "{$art['title']} | Heritage Journal",
                'seo_description' => $art['excerpt'],
            ]);
        }

        // 6. Reviews (10 Reviews)
        $sampleProducts = Product::take(10)->get();
        $reviewComments = [
            'The weight and patina of the brass is exquisite. Elevated our hallway restoration completely.',
            'Exceptional quality. Arrived carefully crated in timber packaging. Highly recommend their design support.',
            'The opal glass illuminates with such warm ambient glow. Absolutely worth every penny.',
            'Authentic Victorian proportions. Built like heirlooms from another era.',
            'Inquired via WhatsApp and received instant assistance with custom finish selections. Seamless service.',
            'The ceramic glaze quality is flawless. Beautiful addition to our period master bathroom.',
            'Hand-pressed cement tiles look stunning in our vestibule entry. Truly artisanal craftsmanship.',
            'Top tier architectural hardware. The door handles operate with such solid tactile weight.',
            'Beautiful editorial catalog and easy WhatsApp inquiry process. Prompt delivery to London.',
            'Wonderful period aesthetic without compromising on modern precision engineering.'
        ];

        $customerNames = [
            'Lady Eleanor Vance', 'Arch. Marcus Sterling', 'Sophia Pembroke', 'Julian Montgomery',
            'Victoria Sinclair', 'Charles H. Davenport', 'Beatrice Thorne', 'Harrison Croft',
            'Isabella Fontaine', 'Rupert Thorne'
        ];

        foreach ($sampleProducts as $rIdx => $p) {
            Review::create([
                'product_id' => $p->id,
                'customer_name' => $customerNames[$rIdx],
                'rating' => rand(4, 5),
                'review' => $reviewComments[$rIdx],
                'review_date' => now()->subDays(rand(2, 60)),
                'status' => 'published',
            ]);
        }
    }
}
