<?php
/**
 * PWA Icon Generator for LandSalesPro
 * Generates all required icon sizes for PWA
 *
 * Run: php generate-icons.php
 * Or access: http://localhost/landsales/assets/icons/generate-icons.php
 */

header('Content-Type: text/html; charset=UTF-8');

$sizes = [72, 96, 128, 144, 152, 192, 384, 512];
$badgeSizes = [72, 96];
$outputDir = __DIR__;

echo "<!DOCTYPE html><html><head><title>PWA Icon Generator</title>";
echo "<style>body{font-family:system-ui;padding:20px;max-width:800px;margin:0 auto}";
echo "h1{color:#667eea}li{padding:5px 0}img{margin:5px;border:1px solid #ddd;border-radius:8px}";
echo ".success{color:green}.error{color:red}</style></head><body>";
echo "<h1>LandSalesPro PWA Icon Generator</h1>";

// Check GD
if (!extension_loaded('gd')) {
    echo "<p class='error'>Error: GD library is not available!</p>";
    echo "<p>To enable GD in XAMPP:</p>";
    echo "<ol><li>Open php.ini</li><li>Uncomment: extension=gd</li><li>Restart Apache</li></ol>";
    exit;
}

echo "<h2>Generating Icons...</h2><ul>";

foreach ($sizes as $size) {
    $filename = "icon-{$size}x{$size}.png";
    $filepath = $outputDir . '/' . $filename;

    // Create image
    $img = imagecreatetruecolor($size, $size);

    // Enable alpha
    imagealphablending($img, false);
    imagesavealpha($img, true);

    // Create gradient background
    for ($y = 0; $y < $size; $y++) {
        $ratio = $y / $size;
        // Gradient from #667eea to #764ba2
        $r = (int)(102 + $ratio * (118 - 102));
        $g = (int)(126 + $ratio * (75 - 126));
        $b = (int)(234 + $ratio * (162 - 234));
        $color = imagecolorallocate($img, $r, $g, $b);
        imageline($img, 0, $y, $size, $y, $color);
    }

    // Draw white land/plot icon
    $white = imagecolorallocate($img, 255, 255, 255);

    $margin = $size * 0.18;
    $centerX = $size / 2;

    // House roof (triangle)
    $roofPeak = $margin;
    $roofBase = $size * 0.42;
    $roofPoints = [
        $centerX, $roofPeak,
        $margin, $roofBase,
        $size - $margin, $roofBase
    ];
    imagefilledpolygon($img, $roofPoints, 3, $white);

    // House body
    $bodyMargin = $margin * 1.3;
    $bodyTop = $roofBase - ($size * 0.03);
    $bodyBottom = $size - $margin;
    imagefilledrectangle($img, $bodyMargin, $bodyTop, $size - $bodyMargin, $bodyBottom, $white);

    // Door (purple rectangle)
    $doorWidth = $size * 0.15;
    $doorHeight = $size * 0.22;
    $doorX = $centerX - ($doorWidth / 2);
    $doorY = $bodyBottom - $doorHeight;

    // Get purple color from gradient
    $purple = imagecolorallocate($img, 102, 126, 234);
    imagefilledrectangle($img, $doorX, $doorY, $doorX + $doorWidth, $bodyBottom, $purple);

    // Windows (purple squares)
    $windowSize = $size * 0.1;
    $windowY = $bodyTop + ($size * 0.08);

    // Left window
    $leftWindowX = $bodyMargin + ($size * 0.06);
    imagefilledrectangle($img, $leftWindowX, $windowY, $leftWindowX + $windowSize, $windowY + $windowSize, $purple);

    // Right window
    $rightWindowX = $size - $bodyMargin - ($size * 0.06) - $windowSize;
    imagefilledrectangle($img, $rightWindowX, $windowY, $rightWindowX + $windowSize, $windowY + $windowSize, $purple);

    // Save
    if (imagepng($img, $filepath)) {
        echo "<li class='success'>✓ Created: {$filename}</li>";
    } else {
        echo "<li class='error'>✗ Failed: {$filename}</li>";
    }

    imagedestroy($img);
}

// Generate badge icons
echo "</ul><h2>Generating Badge Icons...</h2><ul>";

foreach ($badgeSizes as $size) {
    $filename = "badge-{$size}x{$size}.png";
    $filepath = $outputDir . '/' . $filename;

    $img = imagecreatetruecolor($size, $size);
    imagealphablending($img, false);
    imagesavealpha($img, true);

    // Transparent background
    $transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
    imagefill($img, 0, 0, $transparent);

    // Purple circle
    $purple = imagecolorallocate($img, 102, 126, 234);
    imagefilledellipse($img, $size/2, $size/2, $size - 4, $size - 4, $purple);

    // White checkmark
    $white = imagecolorallocate($img, 255, 255, 255);
    imagesetthickness($img, max(2, $size / 10));

    $cx = $size / 2;
    $cy = $size / 2;
    $scale = $size / 72;

    imageline($img, $cx - 12*$scale, $cy, $cx - 2*$scale, $cy + 10*$scale, $white);
    imageline($img, $cx - 2*$scale, $cy + 10*$scale, $cx + 15*$scale, $cy - 8*$scale, $white);

    if (imagepng($img, $filepath)) {
        echo "<li class='success'>✓ Created: {$filename}</li>";
    } else {
        echo "<li class='error'>✗ Failed: {$filename}</li>";
    }

    imagedestroy($img);
}

echo "</ul>";

// Show preview
echo "<h2>Preview</h2><div>";
foreach (array_merge($sizes, ['badge-72', 'badge-96']) as $s) {
    $file = is_numeric($s) ? "icon-{$s}x{$s}.png" : "{$s}x" . explode('-', $s)[1] . ".png";
    $displaySize = is_numeric($s) ? min($s, 96) : 72;
    if (file_exists($outputDir . '/' . $file)) {
        echo "<img src='{$file}?t=" . time() . "' width='{$displaySize}' height='{$displaySize}' title='{$file}'>";
    }
}
echo "</div>";

echo "<h2 style='color:green'>✓ All icons generated!</h2>";
echo "<p>You can now close this page. The icons are ready for PWA use.</p>";
echo "</body></html>";
?>
