#!/usr/bin/env php
<?php
/**
 * TBW PWA Icon Generator
 * Run: php generate-icons.php
 * Requires GD extension
 */

$sourceImage = __DIR__ . '/assets/images/1000105812.png';
$iconsDir = __DIR__ . '/icons';

if (!is_dir($iconsDir)) {
    mkdir($iconsDir, 0755, true);
}

$sizes = [72, 96, 128, 192, 512];

if (!extension_loaded('gd')) {
    echo "GD extension not available. Creating placeholder icon files.\n";
    // Create minimal 1x1 transparent PNGs as placeholders
    $placeholder = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==');
    foreach ($sizes as $size) {
        file_put_contents("$iconsDir/icon-$size.png", $placeholder);
        echo "Created placeholder: icon-$size.png\n";
    }
    exit(0);
}

if (!file_exists($sourceImage)) {
    echo "Source image not found: $sourceImage\n";
    exit(1);
}

$src = imagecreatefrompng($sourceImage);
if (!$src) {
    echo "Failed to load source image.\n";
    exit(1);
}

$srcW = imagesx($src);
$srcH = imagesy($src);

foreach ($sizes as $size) {
    $dst = imagecreatetruecolor($size, $size);
    
    // Fill with brand red background
    $bg = imagecolorallocate($dst, 139, 0, 0);
    imagefill($dst, 0, 0, $bg);
    
    // Enable alpha
    imagealphablending($dst, true);
    imagesavealpha($dst, true);
    
    // Scale and center the logo
    $padding = (int)($size * 0.1);
    $logoSize = $size - ($padding * 2);
    
    imagecopyresampled($dst, $src, $padding, $padding, 0, 0, $logoSize, $logoSize, $srcW, $srcH);
    
    $outPath = "$iconsDir/icon-$size.png";
    imagepng($dst, $outPath);
    imagedestroy($dst);
    echo "Created: icon-$size.png ($size x $size)\n";
}

imagedestroy($src);
echo "\nAll icons generated successfully!\n";
