<?php
/**
 * PWA Icon Generator - White Padlock on Blue Background
 * Run once: visit in browser
 */

$sizes = [192, 512];
$outputDir = __DIR__ . '/assets/images/';

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

echo "<h2>PWA Icon Generator - White Padlock on Blue</h2>";

// Blue gradient background
$bgColor1 = [30, 87, 153];   // #1e5799
$bgColor2 = [30, 60, 114];   // #1e3c72

foreach ($sizes as $size) {
    $image = imagecreatetruecolor($size, $size);
    imagesavealpha($image, true);
    imagealphablending($image, true);

    // Create gradient background
    for ($y = 0; $y < $size; $y++) {
        for ($x = 0; $x < $size; $x++) {
            $ratio = ($x + $y) / ($size * 2);
            $r = (int)($bgColor1[0] + ($bgColor2[0] - $bgColor1[0]) * $ratio);
            $g = (int)($bgColor1[1] + ($bgColor2[1] - $bgColor1[1]) * $ratio);
            $b = (int)($bgColor1[2] + ($bgColor2[2] - $bgColor1[2]) * $ratio);
            $color = imagecolorallocate($image, $r, $g, $b);
            imagesetpixel($image, $x, $y, $color);
        }
    }

    // Colors
    $white = imagecolorallocate($image, 255, 255, 255);
    $darkBlue = imagecolorallocate($image, 30, 60, 114);

    $cx = (int)($size / 2);

    // Shackle
    $shackleWidth = (int)($size * 0.28);
    $shackleHeight = (int)($size * 0.22);
    $shackleTop = (int)($size * 0.12);
    $shackleThickness = max(8, (int)($size * 0.07));

    imagesetthickness($image, $shackleThickness);
    imagearc($image, $cx, $shackleTop + $shackleHeight, $shackleWidth * 2, $shackleHeight * 2, 180, 360, $white);

    $shackleLeft = $cx - $shackleWidth;
    $shackleRight = $cx + $shackleWidth;
    $shackleBottom = (int)($size * 0.42);

    imageline($image, $shackleLeft, $shackleTop + $shackleHeight, $shackleLeft, $shackleBottom, $white);
    imageline($image, $shackleRight, $shackleTop + $shackleHeight, $shackleRight, $shackleBottom, $white);

    // Lock body
    $bodyTop = (int)($size * 0.40);
    $bodyBottom = (int)($size * 0.88);
    $bodyLeft = (int)($size * 0.18);
    $bodyRight = (int)($size * 0.82);
    $bodyRadius = max(8, (int)($size * 0.05));

    imagefilledrectangle($image, $bodyLeft + $bodyRadius, $bodyTop, $bodyRight - $bodyRadius, $bodyBottom, $white);
    imagefilledrectangle($image, $bodyLeft, $bodyTop + $bodyRadius, $bodyRight, $bodyBottom - $bodyRadius, $white);
    imagefilledellipse($image, $bodyLeft + $bodyRadius, $bodyTop + $bodyRadius, $bodyRadius * 2, $bodyRadius * 2, $white);
    imagefilledellipse($image, $bodyRight - $bodyRadius, $bodyTop + $bodyRadius, $bodyRadius * 2, $bodyRadius * 2, $white);
    imagefilledellipse($image, $bodyLeft + $bodyRadius, $bodyBottom - $bodyRadius, $bodyRadius * 2, $bodyRadius * 2, $white);
    imagefilledellipse($image, $bodyRight - $bodyRadius, $bodyBottom - $bodyRadius, $bodyRadius * 2, $bodyRadius * 2, $white);

    // Keyhole
    $keyholeY = (int)($size * 0.58);
    $keyholeRadius = max(8, (int)($size * 0.065));
    imagefilledellipse($image, $cx, $keyholeY, $keyholeRadius * 2, $keyholeRadius * 2, $darkBlue);

    // Keyhole slot
    $keySlotTop = $keyholeY + $keyholeRadius - 3;
    $keySlotBottom = (int)($size * 0.78);
    $keySlotTopWidth = max(4, (int)($size * 0.03));
    $keySlotBottomWidth = max(6, (int)($size * 0.055));

    $points = [
        $cx - $keySlotTopWidth, $keySlotTop,
        $cx + $keySlotTopWidth, $keySlotTop,
        $cx + $keySlotBottomWidth, $keySlotBottom,
        $cx - $keySlotBottomWidth, $keySlotBottom
    ];
    imagefilledpolygon($image, $points, 4, $darkBlue);

    // Save
    $outputPath = $outputDir . "icon-{$size}.png";
    imagepng($image, $outputPath);
    imagedestroy($image);

    echo "<p style='color:green'>Created icon-{$size}.png</p>";
}

echo "<hr><p><strong>Done!</strong></p>";
echo "<h3>Preview:</h3>";
echo "<img src='assets/images/icon-512.png' width='128' height='128' style='border-radius:20px; box-shadow: 0 4px 15px rgba(0,0,0,0.3);'>";
echo "<p><a href='home'>Go to Home</a></p>";
?>
