<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Category;
use App\Models\Subcategory;
use App\Models\Brand;
use App\Models\Product;
use App\Models\Productimage;
use App\Models\User;
use App\Models\CustomOffer;
use Illuminate\Support\Str;
use DB;

class ProfessionalSeeder extends Seeder
{
    public function run()
    {
        // 1. Clean up old data
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');
        Category::truncate();
        Subcategory::truncate();
        Brand::truncate();
        Product::truncate();
        Productimage::truncate();
        CustomOffer::truncate();
        DB::table('custom_offer_product')->truncate();
        
        // Remove non-admin/non-customer users
        User::whereNotIn('role', ['admin', 'customer'])->delete();
        
        DB::statement('SET FOREIGN_KEY_CHECKS=1;');

        // 2. Create Brands
        $brandList = ['Pran', 'Radhuni', 'Square', 'Maggi', 'Pampers', 'Dettol', 'Sunsilk', 'Samsung', 'Xiaomi', 'Apple', 'Bata', 'Aarong', 'Lotto', 'Puma', 'Nike'];
        foreach ($brandList as $name) {
            Brand::create([
                'name' => $name,
                'slug' => Str::slug($name),
                'status' => 1
            ]);
        }

        // 3. Categories with Real Photos and Unique Product Image Sets
        $categories = [
            'Food' => [
                'subs' => ['Rice', 'Spices', 'Noodles', 'Oil', 'Snacks', 'Beverages'],
                'img' => 'https://images.unsplash.com/photo-1542831371-29b0f74f9713?auto=format&fit=crop&q=80&w=800',
                'p_imgs' => [
                    'https://images.unsplash.com/photo-1506484334402-40f2dd6bd76a?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1586201375761-83865001e31c?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1596040033229-a9821ebd058d?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1626074353765-517a681e40be?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1516594798947-e65505dbb29d?auto=format&fit=crop&q=80&w=500'
                ]
            ],
            'Baby Food & Care' => [
                'subs' => ['Milk Powder', 'Baby Lotion', 'Baby Shampoo', 'Baby Food'],
                'img' => 'https://images.unsplash.com/photo-1555252333-9f8e92e65df9?auto=format&fit=crop&q=80&w=800',
                'p_imgs' => [
                    'https://images.unsplash.com/photo-1515488764276-beab7607c1e6?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1522771739844-6a9f6d5f14af?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1544126592-807daf21565c?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1519689680058-324335c77eba?auto=format&fit=crop&q=80&w=500'
                ]
            ],
            'Diapers' => [
                'subs' => ['Pampers', 'Molfix', 'Huggies', 'NeoCare'],
                'img' => 'https://images.unsplash.com/photo-1622328531888-824c478a2e79?auto=format&fit=crop&q=80&w=800',
                'p_imgs' => [
                    'https://images.unsplash.com/photo-1601994863335-924c430ff62a?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1599420186946-7b6fb4e297f0?auto=format&fit=crop&q=80&w=500'
                ]
            ],
            'Home Cleaning' => [
                'subs' => ['Dishwash', 'Floor Cleaner', 'Detergent', 'Toilet Cleaner'],
                'img' => 'https://images.unsplash.com/photo-1584622650111-993a426fbf0a?auto=format&fit=crop&q=80&w=800',
                'p_imgs' => [
                    'https://images.unsplash.com/photo-1585421515584-91a77ea15143?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1584622781564-1d9876a13d00?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1563453392212-326f5e854473?auto=format&fit=crop&q=80&w=500'
                ]
            ],
            'Pet Care' => [
                'subs' => ['Cat Food', 'Dog Food', 'Pet Grooming'],
                'img' => 'https://images.unsplash.com/photo-1516734212186-a967f81ad0d7?auto=format&fit=crop&q=80&w=800',
                'p_imgs' => [
                    'https://images.unsplash.com/photo-1583511655857-d19b40a7a54e?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1583337130417-3346a1be7dee?auto=format&fit=crop&q=80&w=500'
                ]
            ],
            'Beauty & Health' => [
                'subs' => ['Skincare', 'Haircare', 'Oral Care', 'Personal Care'],
                'img' => 'https://images.unsplash.com/photo-1522335789203-aabd1fc54bc9?auto=format&fit=crop&q=80&w=800',
                'p_imgs' => [
                    'https://images.unsplash.com/photo-1596462502278-27bfdc4033c8?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1556228578-0d85b1a4d571?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1512496011951-a093a2847c3c?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1571781926291-c477ebfd024b?auto=format&fit=crop&q=80&w=500'
                ]
            ],
            'Fashion & Lifestyle' => [
                'subs' => ['Men', 'Women', 'Accessories', 'Watches'],
                'img' => 'https://images.unsplash.com/photo-1445205170230-053b83016050?auto=format&fit=crop&q=80&w=800',
                'p_imgs' => [
                    'https://images.unsplash.com/photo-1491336477066-31156b5e4f35?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1483985988355-763728e1935b?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1523275335684-37898b6baf30?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1542491595-62e930774705?auto=format&fit=crop&q=80&w=500'
                ]
            ],
            'Home & Kitchen' => [
                'subs' => ['Cookware', 'Storage', 'Decor', 'Kitchen Tools'],
                'img' => 'https://images.unsplash.com/photo-1556911220-e15b29be8c8f?auto=format&fit=crop&q=80&w=800',
                'p_imgs' => [
                    'https://images.unsplash.com/photo-1584622781564-1d9876a13d00?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1520981757719-45adbd7c947b?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1534349762230-e099952615cb?auto=format&fit=crop&q=80&w=500'
                ]
            ],
            'Stationeries' => [
                'subs' => ['Pens', 'Notebooks', 'Art Supplies', 'Calculators'],
                'img' => 'https://images.unsplash.com/photo-1456735190827-d1262f71b8a3?auto=format&fit=crop&q=80&w=800',
                'p_imgs' => [
                    'https://images.unsplash.com/photo-1586075010633-24701bd6e8b7?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1516962080544-eac695c93791?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1510172951991-856a654063f9?auto=format&fit=crop&q=80&w=500'
                ]
            ],
            'Toys & Sports' => [
                'subs' => ['Action Figures', 'Board Games', 'Sports Gear', 'Educational Toys'],
                'img' => 'https://images.unsplash.com/photo-1531564701487-f238224b7ce3?auto=format&fit=crop&q=80&w=800',
                'p_imgs' => [
                    'https://images.unsplash.com/photo-1531564701487-f238224b7ce3?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1558060370-d644479cb6f7?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1581235720704-06d3acfc13b7?auto=format&fit=crop&q=80&w=500'
                ]
            ],
            'Gadget' => [
                'subs' => ['Smartphones', 'Accessories', 'Smart Watches', 'Headphones'],
                'img' => 'https://images.unsplash.com/photo-1511707171634-5f897ff02aa9?auto=format&fit=crop&q=80&w=800',
                'p_imgs' => [
                    'https://images.unsplash.com/photo-1519183071298-a2962edf1a5e?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1523275335684-37898b6baf30?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1505740420928-5e560c06d30e?auto=format&fit=crop&q=80&w=500',
                    'https://images.unsplash.com/photo-1546054454-aa26e2b734c7?auto=format&fit=crop&q=80&w=500'
                ]
            ],
        ];

        $allBrands = Brand::all();

        foreach ($categories as $catName => $data) {
            $category = Category::create([
                'name'   => $catName,
                'slug'   => Str::slug($catName),
                'status' => 1,
                'image'  => $data['img']
            ]);

            foreach ($data['subs'] as $subName) {
                $subcategory = Subcategory::create([
                    'subcategoryName' => $subName,
                    'slug'            => Str::slug($subName . '-' . rand(1,999)),
                    'category_id'     => $category->id,
                    'status'          => 1
                ]);

                // Create 8-10 Products for EACH subcategory to make it truly full
                $productCount = rand(8, 10);
                for ($i = 1; $i <= $productCount; $i++) {
                    $brand = $allBrands->random();
                    $productName = $brand->name . ' ' . $subName . ' ' . $i . ' - ' . Str::random(4);
                    $price = rand(100, 5000);
                    
                    $product = Product::create([
                        'name' => $productName,
                        'slug' => Str::slug($productName . '-' . rand(1,99999)),
                        'category_id' => $category->id,
                        'subcategory_id' => $subcategory->id,
                        'brand_id' => $brand->id,
                        'new_price' => $price,
                        'old_price' => $price + rand(50, 1000),
                        'stock' => rand(10, 500),
                        'purchase_price' => $price * 0.65,
                        'status' => 1,
                        'description' => 'Experience the best quality with this ' . $subName . ' from ' . $brand->name . '. Designed for maximum performance and durability.',
                    ]);

                    // Pick a random image from the category's image set
                    $randomImg = $data['p_imgs'][array_rand($data['p_imgs'])];
                    
                    Productimage::create([
                        'product_id' => $product->id,
                        'image' => $randomImg
                    ]);
                }
            }
        }

        // 5. Custom Offers
        $offer = CustomOffer::create([
            'name' => 'Mega Flash Sale',
            'slug' => 'mega-flash-sale',
            'status' => 1,
            'icon' => 'fa-bolt',
            'color' => '#E31019',
            'start_date' => now(),
            'end_date' => now()->addDays(7),
        ]);

        $offer->products()->attach(Product::inRandomOrder()->limit(15)->pluck('id'));

        echo "Ultra-Premium Seeding with Unique Images Completed!\n";
    }
}
