<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Category;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class EnhanceCategoryImagesSeeder extends Seeder
{
    public function run()
    {
        $this->command->info('🎨 Starting Category Visual Enhancement...');
        
        Storage::disk('public')->makeDirectory('categories');

        // High-quality cover images for parent categories
        $mappings = [
            'Agriculture' => 'https://images.unsplash.com/photo-1625246333195-78d9c38ad449',
            'Apparel & Fashion' => 'https://images.unsplash.com/photo-1490481651871-ab68de25d43d',
            'Automobile' => 'https://images.unsplash.com/photo-1541899481282-d53bffe3c35d',
            'Beauty & Cosmetics' => 'https://images.unsplash.com/photo-1596462502278-27bfdd403348',
            'Bags & Luggage' => 'https://images.unsplash.com/photo-1547949003-9792a18a2601',
            'Chemicals' => 'https://images.unsplash.com/photo-1532187863486-abf9d3a3622a',
            'Computer Hardware & Software' => 'https://images.unsplash.com/photo-1544244015-0df4b3ffc6b0',
            'Construction & Real Estate' => 'https://images.unsplash.com/photo-1503387762-592dee58c460',
            'Consumer Electronics' => 'https://images.unsplash.com/photo-1498049860654-af1a5c5668ba',
            'Electronics & Electrical Supplies' => 'https://images.unsplash.com/photo-1550009158-9ebf69173e03',
            'Energy & Power' => 'https://images.unsplash.com/photo-1473341304170-971dccb5ac1e',
            'Food & Beverage' => 'https://images.unsplash.com/photo-1504674900247-0877df9cc836',
            'Footwear & Shoes' => 'https://images.unsplash.com/photo-1542291026-7eec264c27ff',
            'Furniture' => 'https://images.unsplash.com/photo-1524758631624-e2822e304c36',
            'Gifts & Handicrafts' => 'https://images.unsplash.com/photo-1533158326339-7f3cf2404354',
            'Health & Personal Care' => 'https://images.unsplash.com/photo-1576091160550-2173bdd99602',
            'Home Supplies' => 'https://images.unsplash.com/photo-1584622650111-993a426fbf0a',
            'Home Textiles & Furnishings' => 'https://images.unsplash.com/photo-1522771739844-6a9f6d5f14af',
            'Hospital & Medical Supplies' => 'https://images.unsplash.com/photo-1516549655169-df83a0774514',
            'Hotel Supplies & Equipment' => 'https://images.unsplash.com/photo-1566073771259-6a8506099945',
            'Industrial Supplies' => 'https://images.unsplash.com/photo-1504917595217-d4dc5f64a044',
            'Mobile Phones & Accessories' => 'https://images.unsplash.com/photo-1511707171634-5f897ff02aa9',
            'Office & School Supplies' => 'https://images.unsplash.com/photo-1497032628192-86f99bcd76bc',
            'Packaging & Paper' => 'https://images.unsplash.com/photo-1589939705384-5185137a7f0f',
            'Pharmaceuticals' => 'https://images.unsplash.com/photo-1584017947474-d02ce3f73ae1',
            'Plastics & Products' => 'https://images.unsplash.com/photo-1591405351990-4726e331f141',
            'Printing & Publishing' => 'https://images.unsplash.com/photo-1562654501-a0ccc0afaf03',
            'Security & Protection' => 'https://images.unsplash.com/photo-1557597774-9d2739f85a94',
            'Sports & Fitness' => 'https://images.unsplash.com/photo-1461896836934-ffe607ba8211',
            'Telecommunications' => 'https://images.unsplash.com/photo-1523966211575-eb4a01e7dd51',
            'Textiles & Fabrics' => 'https://images.unsplash.com/photo-1524234107056-1c1f48f64ab8',
            'Toys & Games' => 'https://images.unsplash.com/photo-1566576912902-4b61e8781543',
            'Transportation' => 'https://images.unsplash.com/photo-1586528116311-ad8dd3c8310d',
            'Electronics' => 'https://images.unsplash.com/photo-1550009158-9ebf69173e03',
            'Fashion' => 'https://images.unsplash.com/photo-1445205170230-053b83016050',
            'Home & Garden' => 'https://images.unsplash.com/photo-1484101403633-562f891dc89a',
            'Industrial' => 'https://images.unsplash.com/photo-1581091226825-a6a2a5aee158',
            'Beauty & Health' => 'https://images.unsplash.com/photo-1596462502278-27bfdd403348',
            'Automotive' => 'https://images.unsplash.com/photo-1568605117036-5fe5e7bab0b7',
            'Sports' => 'https://images.unsplash.com/photo-1461896836934-ffe607ba8211',
            'Toys & Hobbies' => 'https://images.unsplash.com/photo-1566576912902-4b61e8781543',
            'Bakery Equipments' => 'https://images.unsplash.com/photo-1509440159596-0249088772ff',
            'Marine Equipment' => 'https://images.unsplash.com/photo-1569263979104-865ab7cd8d13',
            'General Products' => 'https://images.unsplash.com/photo-1513883049090-d0b7439799bf',
            'Scientific Instruments' => 'https://images.unsplash.com/photo-1532094349884-543bc11b234d',
            'Hospital & Diagnostic Equipment' => 'https://images.unsplash.com/photo-1584017911766-d451b3d0e843',
            'Mechanical Parts & SparedPreferences' => 'https://images.unsplash.com/photo-1531297461136-82lw9f3a9a6a', // Fixed/Updated
        ];

        $categories = Category::whereNull('parent_id')->get();
        $this->command->getOutput()->progressStart($categories->count());

        foreach ($categories as $category) {
            $url = $mappings[$category->name] ?? 'https://images.unsplash.com/photo-1579389083078-4e7018379f7e'; // Fallback
            
            $filename = 'categories/' . Str::slug($category->name) . '.jpg';
            
            // force download if file doesn't exist OR is empty
            $shouldDownload = !Storage::disk('public')->exists($filename) || Storage::disk('public')->size($filename) == 0;
            
            if ($shouldDownload) {
                try {
                    // Use a slightly different beauty image to be safe
                    if ($category->name == 'Beauty & Cosmetics') {
                         $url = 'https://images.unsplash.com/photo-1616683693504-3ea7e9ad6fec';
                    }

                    $content = @file_get_contents($url . '?w=800&q=80');
                    if ($content) {
                        Storage::disk('public')->put($filename, $content);
                    } else {
                        $this->command->warn("Downloaded content input was empty for: " . $category->name);
                    }
                } catch (\Exception $e) {
                    $this->command->warn("Failed to download image for: " . $category->name);
                }
            }

            if (Storage::disk('public')->exists($filename) && Storage::disk('public')->size($filename) > 0) {
                $category->update(['image' => $filename]);
            }

            $this->command->getOutput()->progressAdvance();
        }

        $this->command->getOutput()->progressFinish();
        $this->command->info('✅ Successfully updated category images.');
    }
}
