<?php

namespace Database\Seeders;

/*
 * CarGallerySeeder — скачивает 3 дополнительных фото на тип кузова
 * и заполняет поле photos_gallery для всех автомобилей.
 */

use App\Models\Car;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;

class CarGallerySeeder extends Seeder
{
    // 3 фото на тип кузова для галереи
    private const GALLERY = [
        'suv' => [
            'https://images.unsplash.com/photo-1503376780353-7e6692767b70?w=800&q=75',
            'https://images.unsplash.com/photo-1549317661-bd32c8ce0db2?w=800&q=75',
            'https://images.unsplash.com/photo-1544636331-e26879cd4d9b?w=800&q=75',
        ],
        'sedan' => [
            'https://images.unsplash.com/photo-1494976388531-d1058494cdd8?w=800&q=75',
            'https://images.unsplash.com/photo-1492144534655-ae79c964c9d7?w=800&q=75',
            'https://images.unsplash.com/photo-1549399542-7e3f8b79c341?w=800&q=75',
        ],
        'hatchback' => [
            'https://images.unsplash.com/photo-1541899481282-d53bffe3c35d?w=800&q=75',
            'https://images.unsplash.com/photo-1549317661-bd32c8ce0db2?w=800&q=75',
            'https://images.unsplash.com/photo-1503376780353-7e6692767b70?w=800&q=75',
        ],
        'wagon' => [
            'https://images.unsplash.com/photo-1544636331-e26879cd4d9b?w=800&q=75',
            'https://images.unsplash.com/photo-1503376780353-7e6692767b70?w=800&q=75',
            'https://images.unsplash.com/photo-1492144534655-ae79c964c9d7?w=800&q=75',
        ],
        'pickup' => [
            'https://images.unsplash.com/photo-1558618666-fcd25c85cd64?w=800&q=75',
            'https://images.unsplash.com/photo-1503376780353-7e6692767b70?w=800&q=75',
            'https://images.unsplash.com/photo-1494976388531-d1058494cdd8?w=800&q=75',
        ],
        'van' => [
            'https://images.unsplash.com/photo-1519641471654-76ce0107ad1b?w=800&q=75',
            'https://images.unsplash.com/photo-1503376780353-7e6692767b70?w=800&q=75',
            'https://images.unsplash.com/photo-1544636331-e26879cd4d9b?w=800&q=75',
        ],
        'minivan' => [
            'https://images.unsplash.com/photo-1449965408869-eaa3f722e40d?w=800&q=75',
            'https://images.unsplash.com/photo-1544636331-e26879cd4d9b?w=800&q=75',
            'https://images.unsplash.com/photo-1503376780353-7e6692767b70?w=800&q=75',
        ],
        'default' => [
            'https://images.unsplash.com/photo-1494976388531-d1058494cdd8?w=800&q=75',
            'https://images.unsplash.com/photo-1503376780353-7e6692767b70?w=800&q=75',
            'https://images.unsplash.com/photo-1549317661-bd32c8ce0db2?w=800&q=75',
        ],
    ];

    public function run(): void
    {
        $dir = 'cars/demo';
        Storage::disk('public')->makeDirectory($dir);

        // Собираем все уникальные URL
        $allUrls = [];
        foreach (self::GALLERY as $pool) {
            foreach ($pool as $url) {
                $allUrls[$url] = true;
            }
        }
        $allUrls = array_keys($allUrls);

        // Скачиваем
        $downloaded = [];
        foreach ($allUrls as $url) {
            $hash = md5($url);
            $file = $dir . '/' . $hash . '.jpg';

            if (Storage::disk('public')->exists($file)) {
                $downloaded[$url] = $file;
                $this->command->line("  [skip] {$hash}.jpg");
                continue;
            }

            $this->command->line("  [download] {$hash}.jpg...");
            try {
                $response = Http::timeout(25)->get($url);
                if ($response->successful()) {
                    Storage::disk('public')->put($file, $response->body());
                    $downloaded[$url] = $file;
                    $this->command->line('    ✓ ' . round(strlen($response->body()) / 1024) . ' KB');
                } else {
                    $this->command->warn('    ✗ HTTP ' . $response->status());
                }
            } catch (\Exception $e) {
                $this->command->warn('    ✗ ' . $e->getMessage());
            }
        }

        // Присваиваем галереи
        $updated = 0;
        foreach (Car::all() as $car) {
            $pool = self::GALLERY[$car->body_type] ?? self::GALLERY['default'];
            $gallery = [];
            foreach ($pool as $url) {
                if (isset($downloaded[$url])) {
                    $gallery[] = $downloaded[$url];
                }
            }
            if (!empty($gallery)) {
                $car->update(['photos_gallery' => $gallery]);
                $updated++;
            }
        }

        $this->command->info("Галерея обновлена: {$updated} авто.");
    }
}
