<?php

namespace Database\Seeders;

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

/**
 * Category-aware product attributes, modelled on Daraz/Amazon specification sets.
 * Each attribute is attached to one or more categories (by name, anywhere in the
 * tree) and inherited by all descendants via Category::resolveAttributes().
 */
class AttributeSeeder extends Seeder
{
    /** The 12 top categories — attributes here apply to their whole subtree. */
    private const ALL_TOPS = [
        'Groceries & Pets', 'Health & Beauty', "Men's Fashion", "Women's Fashion",
        'Electronic Devices', 'Electronic Accessories', 'TV & Home Appliances',
        'Home & Lifestyle', 'Watches, Bags & Jewellery', 'Mother & Baby',
        'Sports & Outdoor', 'Automotive & Motorbike',
    ];

    private const FASHION_TOPS = ["Men's Fashion", "Women's Fashion"];

    /** Clothing sub-categories (so apparel size doesn't land on shoes/bags). */
    private const CLOTHING = [
        'T-Shirts & Tanks', 'Shirts & Polo', 'Pants & Jeans', 'Shorts, Joggers & Sweats',
        'Kurtas & Shalwar Kameez', 'Winter Clothing', 'Inner Wear', "Boy's Clothing",
        'Tops', 'Dresses & Skirts', 'Pants, Jeans & Leggings', 'Sleepwear & Innerwear',
        'Girls Clothing', 'Bras, Panties & Lingerie', 'Muslim Wear', 'Unstitched Fabric',
    ];

    private const SHOES = ['Shoes', "Boy's Shoes", 'Girls Shoes'];

    private const ELECTRONICS_TOPS = ['Electronic Devices', 'Electronic Accessories', 'TV & Home Appliances'];

    private const COMPUTING = ['Smart Phones', 'Laptops', 'Desktops', 'Like New'];

    public function run(): void
    {
        $defs = [
            ['Brand', 'brand', 'text', null, true, false, 1, [], self::ALL_TOPS],
            ['Model', 'model', 'text', null, false, false, 2, [], ['Electronic Devices', 'Electronic Accessories', 'TV & Home Appliances', 'Automotive & Motorbike']],
            ['Color', 'color', 'multiselect', null, true, false, 3,
                ['Black', 'White', 'Red', 'Blue', 'Green', 'Yellow', 'Pink', 'Purple', 'Orange', 'Grey', 'Brown', 'Beige', 'Gold', 'Silver', 'Multicolor'],
                ["Men's Fashion", "Women's Fashion", 'Electronic Devices', 'Electronic Accessories', 'TV & Home Appliances', 'Watches, Bags & Jewellery', 'Home & Lifestyle', 'Mother & Baby', 'Sports & Outdoor', 'Automotive & Motorbike']],
            ['Material', 'material', 'select', null, false, false, 4,
                ['Cotton', 'Leather', 'Polyester', 'Denim', 'Wool', 'Silk', 'Linen', 'Synthetic', 'Plastic', 'Metal', 'Stainless Steel', 'Wood', 'Glass', 'Ceramic', 'Rubber'],
                ["Men's Fashion", "Women's Fashion", 'Home & Lifestyle', 'Watches, Bags & Jewellery', 'Mother & Baby']],
            ['Warranty', 'warranty', 'select', null, true, false, 5,
                ['No Warranty', '7 Days', '14 Days', '1 Month', '3 Months', '6 Months', '1 Year', '2 Years'],
                ['Electronic Devices', 'Electronic Accessories', 'TV & Home Appliances', 'Watches, Bags & Jewellery', 'Sports & Outdoor', 'Automotive & Motorbike', 'Home & Lifestyle']],
            ['Apparel Size', 'apparel-size', 'multiselect', null, true, false, 6,
                ['XS', 'S', 'M', 'L', 'XL', 'XXL', '3XL', 'Free Size'],
                self::CLOTHING],
            ['Shoe Size (EU)', 'shoe-size', 'multiselect', null, true, false, 7,
                ['36', '37', '38', '39', '40', '41', '42', '43', '44', '45'],
                self::SHOES],
            ['Gender', 'gender', 'select', null, true, false, 8,
                ['Men', 'Women', 'Unisex', 'Boys', 'Girls'],
                ["Men's Fashion", "Women's Fashion", 'Watches, Bags & Jewellery', 'Sports & Outdoor']],
            ['Age Range', 'age-range', 'select', null, false, false, 9,
                ['Adult', 'Teen (13-18)', 'Kids (3-12)', 'Toddler (1-3)', 'Infant (0-12 months)'],
                ['Mother & Baby', "Boy's Clothing", 'Girls Clothing', "Boy's Shoes", 'Girls Shoes']],
            ['Occasion', 'occasion', 'multiselect', null, false, false, 10,
                ['Casual', 'Formal', 'Party', 'Wedding', 'Office', 'Sports', 'Eid', 'Outdoor'],
                ["Men's Fashion", "Women's Fashion", 'Watches, Bags & Jewellery']],
            ['Season', 'season', 'multiselect', null, false, false, 11,
                ['Summer', 'Winter', 'Spring', 'Autumn', 'All Season'],
                self::FASHION_TOPS],
            ['RAM', 'ram', 'select', null, true, false, 12,
                ['2 GB', '3 GB', '4 GB', '6 GB', '8 GB', '12 GB', '16 GB', '32 GB'],
                self::COMPUTING],
            ['Storage', 'storage', 'select', null, true, false, 13,
                ['16 GB', '32 GB', '64 GB', '128 GB', '256 GB', '512 GB', '1 TB'],
                self::COMPUTING],
            ['Screen Size', 'screen-size', 'text', 'inch', false, false, 14, [],
                ['Smart Phones', 'Laptops', 'Monitors', 'Televisions', 'Like New']],
            ['Capacity', 'capacity', 'text', null, false, false, 15, [],
                ['Refrigerators & Freezers', 'Air Conditioner', 'Washing Machine', 'Kitchen Appliances']],
            ['Power', 'power', 'text', 'watt', false, false, 16, [],
                ['Kitchen Appliances', 'Cooling & Heating', 'Generator, UPS & Solar']],
            ['Country of Origin', 'country-of-origin', 'select', null, false, false, 17,
                ['China', 'Pakistan', 'USA', 'Japan', 'South Korea', 'Germany', 'Thailand', 'India', 'Vietnam'],
                ['Electronic Devices', 'Electronic Accessories', 'TV & Home Appliances', 'Automotive & Motorbike']],
        ];

        foreach ($defs as [$name, $slug, $type, $unit, $filterable, $required, $sort, $options, $cats]) {
            $attr = Attribute::updateOrCreate(['slug' => $slug], [
                'name'          => $name,
                'type'          => $type,
                'unit'          => $unit,
                'is_filterable' => $filterable,
                'is_required'   => $required,
                'sort_order'    => $sort,
            ]);

            foreach ($options as $i => $opt) {
                $attr->options()->updateOrCreate(
                    ['slug' => Str::slug($opt)],
                    ['value' => $opt, 'sort_order' => $i],
                );
            }

            $categoryIds = Category::whereIn('name_en', $cats)->pluck('id');
            $attr->categories()->sync($categoryIds);
        }

        $this->command?->info('Attributes seeded: ' . Attribute::count() . ' attributes, '
            . \App\Models\AttributeOption::count() . ' options, attached across categories.');
    }
}
