<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class ListCategoriesNeedingImages extends Command
{
    protected $signature = 'categories:list-needs-images {--all : Show all categories}';
    protected $description = 'List categories with no images or placeholder images';

    public function handle()
    {
        $this->info('🔍 Scanning categories...');
        $this->newLine();

        $showAll = $this->option('all');

        // Get categories
        $query = DB::table('categories');
        
        if (!$showAll) {
            // Only show categories without images or with placeholders
            $query->where(function($q) {
                $q->whereNull('image')
                  ->orWhere('image', '')
                  ->orWhere('image', 'like', '%placeholder%')
                  ->orWhere(function($sq) {
                      // Check if image file is very small (likely low quality favicon)
                      $sq->whereNotNull('image');
                  });
            });
        }

        $categories = $query->orderBy('category_type')->orderBy('name')->get();

        if ($categories->isEmpty()) {
            $this->info('✅ All categories have good images!');
            return 0;
        }

        $this->info("Found {$categories->count()} categories");
        $this->newLine();

        // Group by type
        $grouped = [];
        foreach ($categories as $cat) {
            $grouped[$cat->category_type][] = $cat;
        }

        foreach ($grouped as $type => $cats) {
            $icon = match($type) {
                'game' => '🎮',
                'ewallet' => '💳',
                'pulsa' => '📱',
                'voucher' => '🎫',
                'streaming' => '📺',
                'subscription' => '💎',
                'pln' => '⚡',
                default => '📦'
            };

            $this->line("{$icon} " . strtoupper($type) . ":");
            
            foreach ($cats as $cat) {
                $status = $this->getImageStatus($cat);
                $this->line("  {$status} {$cat->name}");
            }
            
            $this->newLine();
        }

        // Show download links
        $this->info('📥 Download high-quality logos from:');
        $this->line('  • https://brandfetch.com/');
        $this->line('  • https://seeklogo.com/');
        $this->line('  • Google Images: "[brand name] logo png transparent"');
        $this->newLine();

        $this->info('💡 After downloading, save to a folder and run:');
        $this->line('  php artisan categories:upload-from-folder /path/to/logos');

        return 0;
    }

    private function getImageStatus($category)
    {
        if (empty($category->image)) {
            return '❌ No image';
        }

        $imagePath = storage_path('app/public/' . $category->image);
        
        if (!file_exists($imagePath)) {
            return '❌ Missing';
        }

        $fileSize = filesize($imagePath);
        
        if ($fileSize < 5000) {
            return '⚠️  Low quality (< 5KB)';
        }

        if ($fileSize < 20000) {
            return '⚠️  Medium quality';
        }

        return '✅ Good quality';
    }
}