<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\PosMenuItem;
use Illuminate\Support\Facades\Storage;

class GeneratePlaceholderImages extends Command
{
    protected $signature = 'images:generate-placeholders';
    protected $description = 'Generate placeholder images for all menu items';

    public function handle()
    {
        $items = PosMenuItem::select('id', 'name', 'image_url', 'image')
            ->get();

        $this->info('Generating placeholder images...');
        $this->line('');

        $foodImagesPath = public_path('FoodImages');
        
        // Ensure directory exists
        if (!file_exists($foodImagesPath)) {
            mkdir($foodImagesPath, 0755, true);
        }

        $imageNames = [];
        
        foreach ($items as $item) {
            $imageName = null;
            
            if ($item->image_url) {
                $imageName = basename($item->image_url);
            } elseif ($item->image) {
                if (strpos($item->image, 'FoodImages') !== false) {
                    $imageName = basename($item->image);
                } else {
                    $imageName = basename($item->image);
                }
            }

            if ($imageName && !in_array($imageName, $imageNames)) {
                $imageNames[] = $imageName;
                $imagePath = $foodImagesPath . '/' . $imageName;
                
                if (!file_exists($imagePath)) {
                    $this->createPlaceholderImage($imagePath, $item->name);
                    $this->info("Created: {$imageName}");
                } else {
                    $this->line("Exists: {$imageName}");
                }
            }
        }

        // Create default image
        $defaultImage = $foodImagesPath . '/default-menu-item.jpg';
        if (!file_exists($defaultImage)) {
            $this->createPlaceholderImage($defaultImage, 'Menu Item');
            $this->info("Created: default-menu-item.jpg");
        }

        $this->line('');
        $this->info('Total placeholder images created: ' . count($imageNames));
        $this->warn('Note: Replace these placeholder images with actual Pakistani food images when available.');
        
        return 0;
    }

    private function createPlaceholderImage($filePath, $text)
    {
        // Create a simple placeholder image using GD
        if (!extension_loaded('gd')) {
            $this->error('GD extension is not loaded. Cannot generate images.');
            return false;
        }

        $width = 400;
        $height = 300;
        
        $image = imagecreatetruecolor($width, $height);
        
        // Background color (light gray)
        $bgColor = imagecolorallocate($image, 240, 240, 240);
        imagefill($image, 0, 0, $bgColor);
        
        // Border
        $borderColor = imagecolorallocate($image, 200, 200, 200);
        imagerectangle($image, 0, 0, $width - 1, $height - 1, $borderColor);
        
        // Text color
        $textColor = imagecolorallocate($image, 100, 100, 100);
        
        // Add text (truncate if too long)
        $displayText = strlen($text) > 30 ? substr($text, 0, 27) . '...' : $text;
        $fontSize = 5;
        $textX = ($width - (strlen($displayText) * imagefontwidth($fontSize))) / 2;
        $textY = ($height - imagefontheight($fontSize)) / 2;
        
        imagestring($image, $fontSize, $textX, $textY, $displayText, $textColor);
        
        // Save as JPEG
        imagejpeg($image, $filePath, 85);
        imagedestroy($image);
        
        return true;
    }
}

