<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class DemoProductImagesSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $this->command->info('Creating demo product images...');
        
        // Ensure storage directories exist
        $storagePath = storage_path('app/public');
        if (!is_dir($storagePath)) {
            mkdir($storagePath, 0755, true);
        }
        
        // Create directories
        $productImagesDir = 'public/products';
        $categoryImagesDir = 'public/categories';
        $brandImagesDir = 'public/brands';
        
        // Create directories using Storage facade
        if (!Storage::exists($productImagesDir)) {
            Storage::makeDirectory($productImagesDir);
        }
        if (!Storage::exists($categoryImagesDir)) {
            Storage::makeDirectory($categoryImagesDir);
        }
        if (!Storage::exists($brandImagesDir)) {
            Storage::makeDirectory($brandImagesDir);
        }
        
        // Product image templates with different colors and icons
        $productTemplates = [
            ['bg' => '#1e40af', 'icon' => '⚙️', 'name' => 'engine'],
            ['bg' => '#dc2626', 'icon' => '🛑', 'name' => 'brake'],
            ['bg' => '#059669', 'icon' => '🔧', 'name' => 'suspension'],
            ['bg' => '#7c3aed', 'icon' => '⚡', 'name' => 'electrical'],
            ['bg' => '#ea580c', 'icon' => '💨', 'name' => 'filter'],
            ['bg' => '#0891b2', 'icon' => '🔩', 'name' => 'exhaust'],
            ['bg' => '#be123c', 'icon' => '⚙️', 'name' => 'transmission'],
            ['bg' => '#4338ca', 'icon' => '🚗', 'name' => 'body'],
        ];
        
        // Generate 20 product images
        for ($i = 1; $i <= 20; $i++) {
            $template = $productTemplates[$i % count($productTemplates)];
            $svg = $this->generateProductSVG($template['bg'], $template['icon'], "Product $i");
            Storage::put("$productImagesDir/product_$i.svg", $svg);
        }
        
        // Generate category images
        $categories = [
            ['name' => 'Engine Parts', 'color' => '#1e40af', 'icon' => '⚙️'],
            ['name' => 'Brakes & Rotors', 'color' => '#dc2626', 'icon' => '🛑'],
            ['name' => 'Suspension', 'color' => '#059669', 'icon' => '🔧'],
            ['name' => 'Electrical', 'color' => '#7c3aed', 'icon' => '⚡'],
            ['name' => 'Filters', 'color' => '#ea580c', 'icon' => '💨'],
            ['name' => 'Exhaust', 'color' => '#0891b2', 'icon' => '🔩'],
            ['name' => 'Transmission', 'color' => '#be123c', 'icon' => '⚙️'],
            ['name' => 'Body Parts', 'color' => '#4338ca', 'icon' => '🚗'],
        ];
        
        foreach ($categories as $index => $cat) {
            $svg = $this->generateCategorySVG($cat['color'], $cat['icon'], $cat['name']);
            Storage::put("$categoryImagesDir/category_" . ($index + 1) . ".svg", $svg);
        }
        
        // Generate brand logos
        $brands = ['Bosch', 'ACDelco', 'Moog', 'Monroe', 'Denso', 'Gates', 'NGK', 'Dorman', 'Raybestos', 'KYB'];
        foreach ($brands as $index => $brand) {
            $svg = $this->generateBrandSVG($brand);
            Storage::put("$brandImagesDir/brand_" . ($index + 1) . ".svg", $svg);
        }
        
        // Create a banner image
        $bannerSvg = $this->generateBannerSVG();
        Storage::put('public/banner.svg', $bannerSvg);
        
        $this->command->info('Demo images created successfully!');
        $this->command->info('Product images: storage/app/public/products/');
        $this->command->info('Category images: storage/app/public/categories/');
        $this->command->info('Brand images: storage/app/public/brands/');
    }
    
    private function generateProductSVG($bgColor, $icon, $label)
    {
        return '<svg xmlns="http://www.w3.org/2000/svg" width="600" height="600" viewBox="0 0 600 600">
            <rect width="600" height="600" fill="' . $bgColor . '"/>
            <rect x="50" y="50" width="500" height="500" rx="20" fill="rgba(255,255,255,0.1)"/>
            <text x="300" y="280" font-size="120" text-anchor="middle" dominant-baseline="middle">' . $icon . '</text>
            <text x="300" y="420" font-family="Arial, sans-serif" font-size="32" fill="white" text-anchor="middle" font-weight="bold">' . htmlspecialchars($label) . '</text>
            <text x="300" y="470" font-family="Arial, sans-serif" font-size="20" fill="rgba(255,255,255,0.8)" text-anchor="middle">Premium Quality</text>
            <circle cx="300" cy="300" r="200" fill="none" stroke="rgba(255,255,255,0.2)" stroke-width="2"/>
            <circle cx="300" cy="300" r="150" fill="none" stroke="rgba(255,255,255,0.15)" stroke-width="2"/>
        </svg>';
    }
    
    private function generateCategorySVG($bgColor, $icon, $name)
    {
        return '<svg xmlns="http://www.w3.org/2000/svg" width="400" height="300" viewBox="0 0 400 300">
            <rect width="400" height="300" fill="' . $bgColor . '"/>
            <rect x="20" y="20" width="360" height="260" rx="15" fill="rgba(255,255,255,0.1)"/>
            <text x="200" y="140" font-size="80" text-anchor="middle" dominant-baseline="middle">' . $icon . '</text>
            <text x="200" y="230" font-family="Arial, sans-serif" font-size="28" fill="white" text-anchor="middle" font-weight="bold">' . htmlspecialchars($name) . '</text>
            <circle cx="200" cy="130" r="90" fill="none" stroke="rgba(255,255,255,0.2)" stroke-width="2"/>
        </svg>';
    }
    
    private function generateBrandSVG($brandName)
    {
        $colors = ['#1e40af', '#dc2626', '#059669', '#7c3aed', '#ea580c', '#0891b2', '#be123c', '#4338ca', '#854d0e', '#0f766e'];
        $bgColor = $colors[array_rand($colors)];
        $initials = strtoupper(substr($brandName, 0, 2));
        
        return '<svg xmlns="http://www.w3.org/2000/svg" width="300" height="150" viewBox="0 0 300 150">
            <rect width="300" height="150" fill="' . $bgColor . '" rx="10"/>
            <rect x="10" y="10" width="280" height="130" rx="8" fill="rgba(255,255,255,0.1)"/>
            <text x="150" y="85" font-family="Arial, sans-serif" font-size="48" fill="white" text-anchor="middle" font-weight="bold">' . $initials . '</text>
            <text x="150" y="120" font-family="Arial, sans-serif" font-size="16" fill="rgba(255,255,255,0.9)" text-anchor="middle">' . htmlspecialchars($brandName) . '</text>
        </svg>';
    }
    
    private function generateBannerSVG()
    {
        return '<svg xmlns="http://www.w3.org/2000/svg" width="1200" height="400" viewBox="0 0 1200 400">
            <defs>
                <linearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="100%">
                    <stop offset="0%" style="stop-color:#1e40af;stop-opacity:1" />
                    <stop offset="100%" style="stop-color:#dc2626;stop-opacity:1" />
                </linearGradient>
            </defs>
            <rect width="1200" height="400" fill="url(#grad)"/>
            <rect x="50" y="50" width="1100" height="300" rx="20" fill="rgba(255,255,255,0.1)"/>
            <text x="600" y="160" font-family="Arial, sans-serif" font-size="56" fill="white" text-anchor="middle" font-weight="bold">AutoParts Marketplace</text>
            <text x="600" y="220" font-family="Arial, sans-serif" font-size="28" fill="rgba(255,255,255,0.9)" text-anchor="middle">Premium Auto Parts for Every Vehicle</text>
            <text x="600" y="270" font-family="Arial, sans-serif" font-size="20" fill="rgba(255,255,255,0.8)" text-anchor="middle">🔧 Quality Parts | 🚚 Fast Shipping | 💯 Satisfaction Guaranteed</text>
            <circle cx="200" cy="200" r="100" fill="none" stroke="rgba(255,255,255,0.1)" stroke-width="2"/>
            <circle cx="1000" cy="200" r="150" fill="none" stroke="rgba(255,255,255,0.1)" stroke-width="2"/>
        </svg>';
    }
}
