<?php

namespace Database\Seeders;

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

class EnhanceProductImagesSeeder extends Seeder
{
    public function run()
    {
        $this->command->info('🎬 Starting Product Gallery Enhancement...');
        
        Storage::disk('public')->makeDirectory('products');

        // Image Pools by Category Keyword
        $pools = [
            'agriculture' => [
                'https://images.unsplash.com/photo-1625246333195-78d9c38ad449',
                'https://images.unsplash.com/photo-1523348837708-15d4a09cfac2',
                'https://images.unsplash.com/photo-1592982537447-7440770cbfc9',
                'https://images.unsplash.com/photo-1464226184884-fa280b87c399'
            ],
            'electronics' => [
                'https://images.unsplash.com/photo-1498049860654-af1a5c5668ba',
                'https://images.unsplash.com/photo-1550009158-9ebf69173e03',
                'https://images.unsplash.com/photo-1526733158133-59f10a16a44c',
                'https://images.unsplash.com/photo-1588508065123-287b28e013da'
            ],
            'furniture' => [
                'https://images.unsplash.com/photo-1524758631624-e2822e304c36',
                'https://images.unsplash.com/photo-1555041469-a586c61ea9bc',
                'https://images.unsplash.com/photo-1583847268964-b28dc2f51f92',
                'https://images.unsplash.com/photo-1538688525198-9b88f6f53126'
            ],
            'fashion' => [
                'https://images.unsplash.com/photo-1445205170230-053b83016050',
                'https://images.unsplash.com/photo-1490481651871-ab68de25d43d',
                'https://images.unsplash.com/photo-1483985988355-763728e1935b',
                'https://images.unsplash.com/photo-1558769132-cb1aea458c5e'
            ],
            'industrial' => [
                'https://images.unsplash.com/photo-1504917595217-d4dc5f64a044',
                'https://images.unsplash.com/photo-1518709268805-4e9042af9f23',
                'https://images.unsplash.com/photo-1581091226825-a6a2a5aee158',
                'https://images.unsplash.com/photo-1565608438257-fac3c27beb36'
            ],
            'food' => [
                'https://images.unsplash.com/photo-1504674900247-0877df9cc836',
                'https://images.unsplash.com/photo-1498837167922-ddd27525d352',
                'https://images.unsplash.com/photo-1540189549336-e6e99c357fcd',
                'https://images.unsplash.com/photo-1476514525535-07fb3b4ae5f1'
            ],
            'general' => [
                'https://images.unsplash.com/photo-1574634534894-89d7576c8259',
                'https://images.unsplash.com/photo-1579389083078-4e7018379f7e',
                'https://images.unsplash.com/photo-1586528116311-ad8dd3c8310d',
                'https://images.unsplash.com/photo-1566576912902-4b61e8781543'
            ]
        ];

        $products = Product::all();
        $this->command->getOutput()->progressStart($products->count());

        foreach ($products as $product) {
            $catSlug = Str::lower($product->category->name ?? 'general');
            
            // Find matched pool
            $poolKey = 'general';
            foreach (array_keys($pools) as $key) {
                if (Str::contains($catSlug, $key)) {
                    $poolKey = $key;
                    break;
                }
            }

            $imagePool = $pools[$poolKey];
            $imagesJson = [];

            foreach ($imagePool as $index => $url) {
                $filename = 'products/prod_' . $product->id . '_' . $index . '.jpg';
                
                // Download only if not exists
                if (!Storage::disk('public')->exists($filename)) {
                    try {
                        $content = @file_get_contents($url . '?w=800&q=80');
                        if ($content) {
                            Storage::disk('public')->put($filename, $content);
                        }
                    } catch (\Exception $e) {
                         // Soft fail if download fails
                    }
                }

                if (Storage::disk('public')->exists($filename)) {
                    $imagesJson[] = [
                        'thumbnail' => $filename,
                        'original' => $filename
                    ];
                }
            }

            // Fallback to original if pool download failed completely
            if (empty($imagesJson) && !empty($product->images_json)) {
                $imagesJson = $product->images_json;
            }

            $product->update([
                'images_json' => $imagesJson
            ]);

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

        $this->command->getOutput()->progressFinish();
        $this->command->info('✅ Successfully updated ' . $products->count() . ' products with gallery images.');
    }
}
