<?php

namespace Database\Seeders;

use App\Models\User;
use App\Models\Product;
use App\Models\Setting;
use App\Models\DeliveryZone;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // Admin user
        User::updateOrCreate(
            ['email' => 'admin@megandmau.com'],
            [
                'name' => 'MEG & MAU Admin',
                'password' => Hash::make('admin@123'),
                'role' => 'admin',
                'referral_code' => 'ADMIN001',
                'is_active' => true,
            ]
        );

        // Default settings
        $settings = [
            'site_name' => 'MEG & MAU',
            'site_email' => 'contact@megandmau.com',
            'site_phone' => '+91 98765 43210',
            'site_address' => 'Mumbai, India',
            'currency' => 'INR',
            'currency_symbol' => '₹',
            'free_shipping_above' => '2000',
            'default_shipping' => '150',
            'referral_reward' => '100',
            'referral_enabled' => '1',
        ];
        foreach ($settings as $key => $value) {
            Setting::set($key, $value);
        }

        // Sample products
        $products = [
            [
                'name' => 'Lumière Dorée',
                'slug' => 'lumiere-doree',
                'subtitle' => 'Eau de Parfum',
                'description' => 'A radiant symphony of golden amber and rare oud, wrapped in the warmth of Madagascan vanilla.',
                'notes' => ['top' => ['Bergamot', 'Pink Pepper', 'Saffron'], 'heart' => ['Rose Absolute', 'Jasmine Sambac', 'Iris'], 'base' => ['Oud', 'Amber', 'Vanilla', 'Musk']],
                'price' => 38500,
                'size' => '100ml',
                'stock' => 50,
                'badge' => 'Bestseller',
                'color' => '#C9A46A',
                'image' => 'https://images.unsplash.com/photo-1541643600914-78b084683702?w=600&q=80',
                'is_active' => true,
                'is_featured' => true,
            ],
            [
                'name' => 'Nuit Éternelle',
                'slug' => 'nuit-eternelle',
                'subtitle' => 'Eau de Parfum Intense',
                'description' => 'The essence of midnight — deep, mysterious, and utterly captivating.',
                'notes' => ['top' => ['Black Pepper', 'Cardamom', 'Incense'], 'heart' => ['Dark Rose', 'Patchouli', 'Vetiver'], 'base' => ['Sandalwood', 'Leather', 'Benzoin', 'Musk']],
                'price' => 42500,
                'size' => '100ml',
                'stock' => 30,
                'badge' => 'New',
                'color' => '#2A2A2A',
                'image' => 'https://images.unsplash.com/photo-1592945403244-b3fbafd7f539?w=600&q=80',
                'is_active' => true,
                'is_featured' => true,
            ],
            [
                'name' => 'Fleur Céleste',
                'slug' => 'fleur-celeste',
                'subtitle' => 'Eau de Parfum',
                'description' => 'An ethereal bouquet of rare white flowers, kissed by morning dew.',
                'notes' => ['top' => ['Neroli', 'Lemon Blossom', 'Green Tea'], 'heart' => ['Tuberose', 'Gardenia', 'White Peony'], 'base' => ['White Musk', 'Cedarwood', 'Ambrette']],
                'price' => 36500,
                'size' => '100ml',
                'stock' => 25,
                'badge' => 'Limited',
                'color' => '#E8DDD0',
                'image' => 'https://images.unsplash.com/photo-1563170351-be82bc888aa4?w=600&q=80',
                'is_active' => true,
                'is_featured' => false,
            ],
            [
                'name' => "Désert d'Or",
                'slug' => 'desert-dor',
                'subtitle' => 'Eau de Parfum',
                'description' => 'The golden sands of Arabia distilled into liquid luxury.',
                'notes' => ['top' => ['Frankincense', 'Cinnamon', 'Grapefruit'], 'heart' => ['Oud Rose', 'Amber', 'Jasmine'], 'base' => ['Sandalwood', 'Vanilla', 'Labdanum', 'Musk']],
                'price' => 44500,
                'size' => '100ml',
                'stock' => 20,
                'badge' => 'Exclusive',
                'color' => '#A8834A',
                'image' => 'https://images.unsplash.com/photo-1587017539504-67cfbddac569?w=600&q=80',
                'is_active' => true,
                'is_featured' => true,
            ],
            [
                'name' => 'Jardin Secret',
                'slug' => 'jardin-secret',
                'subtitle' => 'Eau de Parfum',
                'description' => 'A hidden garden in full bloom — fresh, green, and intoxicatingly floral.',
                'notes' => ['top' => ['Violet Leaf', 'Blackcurrant', 'Peach'], 'heart' => ['Rose de Mai', 'Peony', 'Magnolia'], 'base' => ['Oakmoss', 'Vetiver', 'Musk', 'Amber']],
                'price' => 35500,
                'size' => '100ml',
                'stock' => 40,
                'badge' => null,
                'color' => '#8B9E6E',
                'image' => 'https://images.unsplash.com/photo-1571781926291-c477ebfd024b?w=600&q=80',
                'is_active' => true,
                'is_featured' => false,
            ],
        ];

        foreach ($products as $product) {
            Product::updateOrCreate(['slug' => $product['slug']], $product);
        }

        // Sample delivery zones
        $zones = [
            ['pincode' => '380001', 'city' => 'Ahmedabad', 'state' => 'Gujarat', 'delivery_charge' => 0, 'estimated_days' => 2],
            ['pincode' => '380015', 'city' => 'Ahmedabad', 'state' => 'Gujarat', 'delivery_charge' => 0, 'estimated_days' => 2],
            ['pincode' => '390001', 'city' => 'Vadodara', 'state' => 'Gujarat', 'delivery_charge' => 50, 'estimated_days' => 3],
            ['pincode' => '395001', 'city' => 'Surat', 'state' => 'Gujarat', 'delivery_charge' => 50, 'estimated_days' => 3],
            ['pincode' => '360001', 'city' => 'Rajkot', 'state' => 'Gujarat', 'delivery_charge' => 80, 'estimated_days' => 4],
            ['pincode' => '400001', 'city' => 'Mumbai', 'state' => 'Maharashtra', 'delivery_charge' => 0, 'estimated_days' => 2],
            ['pincode' => '110001', 'city' => 'New Delhi', 'state' => 'Delhi', 'delivery_charge' => 100, 'estimated_days' => 4],
            ['pincode' => '560001', 'city' => 'Bangalore', 'state' => 'Karnataka', 'delivery_charge' => 100, 'estimated_days' => 5],
        ];

        foreach ($zones as $zone) {
            DeliveryZone::updateOrCreate(['pincode' => $zone['pincode']], $zone);
        }

        $this->command->info('Done! Admin: admin@megandmau.com / admin@123');
    }
}
