<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class GeneratePlaceholderImages extends Command
{
    protected $signature = 'images:placeholders {--force : Overwrite existing files}';

    protected $description = 'Generate placeholder JPG assets in public/images/';

    public function handle(): int
    {
        if (! extension_loaded('gd')) {
            $this->error('The GD extension is required to generate placeholder images.');

            return self::FAILURE;
        }

        $directory = public_path('images');

        if (! is_dir($directory) && ! mkdir($directory, 0755, true) && ! is_dir($directory)) {
            $this->error("Could not create directory: {$directory}");

            return self::FAILURE;
        }

        $files = [
            'hero.jpg' => [1920, 1080, 'HERO'],
            'action.jpg' => [1920, 1080, 'ACTION'],
            'tex-a.jpg' => [900, 1100, 'TEX A'],
            'tex-b.jpg' => [900, 1100, 'TEX B'],
            'tex-c.jpg' => [900, 1100, 'TEX C'],
            'tex-d.jpg' => [900, 1100, 'TEX D'],
            'tex-e.jpg' => [900, 1100, 'TEX E'],
            'tex-f.jpg' => [900, 1100, 'TEX F'],
        ];

        foreach ($files as $filename => [$width, $height, $label]) {
            $path = $directory.DIRECTORY_SEPARATOR.$filename;

            if (is_file($path) && ! $this->option('force')) {
                $this->line("Skipped {$filename} (already exists; use --force to overwrite)");

                continue;
            }

            $this->writePlaceholder($path, $width, $height, $label);
            $this->info("Created {$filename}");
        }

        $this->newLine();
        $this->comment('Replace these placeholders with production photography when ready.');

        return self::SUCCESS;
    }

    private function writePlaceholder(string $path, int $width, int $height, string $label): void
    {
        $image = imagecreatetruecolor($width, $height);

        $ink = imagecolorallocate($image, 10, 10, 11);
        $ink2 = imagecolorallocate($image, 22, 11, 13);
        $brass = imagecolorallocate($image, 224, 35, 47);
        $bone = imagecolorallocate($image, 243, 239, 231);
        $muted = imagecolorallocate($image, 110, 105, 98);

        imagefill($image, 0, 0, $ink);

        for ($y = 0; $y < $height; $y++) {
            $ratio = $y / max($height, 1);
            $red = (int) (10 + (22 - 10) * $ratio);
            $green = (int) (10 + (11 - 10) * $ratio);
            $blue = (int) (11 + (13 - 11) * $ratio);
            $line = imagecolorallocate($image, $red, $green, $blue);
            imageline($image, 0, $y, $width, $y, $line);
        }

        imagefilledrectangle($image, 0, (int) ($height * 0.72), $width, $height, $ink2);
        imagefilledrectangle($image, 0, $height - 6, $width, $height, $brass);

        $stripeX = (int) ($width * 0.08);
        imagefilledrectangle($image, $stripeX, (int) ($height * 0.18), $stripeX + 4, (int) ($height * 0.62), $brass);

        imagestring($image, 5, 24, 24, 'HEAD BANGERS', $bone);
        imagestring($image, 3, 24, 48, 'PLACEHOLDER', $muted);
        imagestring($image, 5, 24, $height - 52, $label, $bone);

        imagejpeg($image, $path, 88);
        imagedestroy($image);
    }
}
