<?php
/**
 * generate-pwa-icons.php
 * Generates all 8 required PWA icon sizes in one pass using PHP-GD.
 * Run from CLI or via browser:
 *   php generate-pwa-icons.php
 *
 * Output directory: ../images/icons/  (relative to invcount/scripts/)
 *
 * Place a 1024-pixel source image at ../assets/images/source.png first.
 * If no source image is found a fallback will be generated.
 */

// ── Config ────────────────────────────────────────────────────────────────────
$BASE_DIR    = __DIR__ . '/..';
$SRC_PATH    = $BASE_DIR . '/assets/images/icon-source.png';
$ICONS_DIR   = $BASE_DIR . '/images/icons';
$SIZES       = [72, 96, 128, 144, 152, 192, 384, 512];

// Theme colours — keep in sync with manifest.json / CSS
$BG_COLOR   = [26, 26, 46];   // #1a1a2e
$BAR_COLOR  = [22, 33, 62];   // #16213e
$FG_COLOR   = [255, 255, 255];
$ACCENT     = [68, 165, 255];  // light-blue accent for the monogram

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

// ── Source image: use file if present, otherwise draw via GD ──────────────────
$src = @imagecreatefrompng($SRC_PATH);
if ($src) {
    $srcW = imagesx($src);
    $srcH = imagesy($src);
    echo "Source image loaded: {$srcW}×{$srcH}\n";
} else {
    echo "No source image at $SRC_PATH — generating from GD palette.\n";
    // Build a 1024×1024 source with a monogram + accent badge
    $srcSize = 1024;
    $src = imagecreatetruecolor($srcSize, $srcSize);
    imagealphablending($src, false);
    imagesavealpha($src, true);

    $transparent = imagecolorallocatealpha($src, 0, 0, 0, 127);
    imagefilledrectangle($src, 0, 0, $srcSize, $srcSize, $transparent);

    // Background circle
    $bg   = imagecolorallocate($src, $BG_COLOR[0], $BG_COLOR[1], $BG_COLOR[2]);
    imagefilledellipse($src, $srcSize / 2, $srcSize / 2, $srcSize, $srcSize, $bg);

    // Large "IC" monogram
    $fg   = imagecolorallocate($src, $FG_COLOR[0], $FG_COLOR[1], $FG_COLOR[2]);
    $size = (int)($srcSize * 0.42);
    // GD doesn't have built-in TTF from scratch; use a system font if available,
    // otherwise fall back to graphic rectangles spelling out tally-marks.
    $fontFile = match (PHP_OS) {
        'WINNT' => 'C:/Windows/Fonts/segoeuib.ttf',
        default  => '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',
    };
    if (file_exists($fontFile)) {
        imagettftext($src, $size, 0, $srcSize * 0.16, $srcSize * 0.62, $fg, $fontFile, 'IC');
        // Accent underline bar
        $accent = imagecolorallocate($src, $ACCENT[0], $ACCENT[1], $ACCENT[2]);
        imagefilledrectangle($src, $srcSize * 0.16, (int)($srcSize * 0.68),
                                     $srcSize * 0.68, (int)($srcSize * 0.72), $accent);
    } else {
        // ASCII-art fallback — four pixel squares representing "IC"
        for ($i = 0; $i < 8; $i++) {
            imagesetpixel($src, $srcSize / 2 + ($i - 4) * 6, $srcSize / 2, $fg);
            imagesetpixel($src, $srcSize / 2, $srcSize / 2 + ($i - 4) * 6, $fg);
        }
    }
}

// ── Retrieve raw palette pixels for maskable overlay ──────────────────────────
function sample_accents($img, $size, $bg) {
    $out = imagecreatetruecolor($size, $size);
    imagealphablending($out, false);
    imagesavealpha($out, true);
    $trans = imagecolorallocatealpha($out, 0, 0, 0, 127);
    imagefilledrectangle($out, 0, 0, $size, $size, $trans);
    imagecopyresampled($out, $img, 0, 0, 0, 0, $size, $size, 1024, 1024);
    return $out;
}

// ── Generate each icon size ───────────────────────────────────────────────────
$generated = 0;
foreach ($SIZES as $px) {
    $label  = str_pad((string)$px, 3, ' ', STR_PAD_LEFT) . "×{$px}";
    $outFile = $ICONS_DIR . "/icon-{$px}x{$px}.png";

    $out = imagecreatetruecolor($px, $px);
    imagealphablending($out, false);
    imagesavealpha($out, true);
    $trans = imagecolorallocatealpha($out, 0, 0, 0, 127);
    imagefilledrectangle($out, 0, 0, $px, $px, $trans);

    imagecopyresampled($out, $src, 0, 0, 0, 0, $px, $px, 1024, 1024);

    // Ensure lowercase colour for contrast on ALL-* maskable icons
    $outAll = imagecreatetruecolor($px, $px);
    imagealphablending($outAll, false);
    imagesavealpha($outAll, true);
    imagefilledrectangle($outAll, 0, 0, $px, $px, $trans);
    imagecopyresampled($outAll, $src, 0, 0, 0, 0, $px, $px, 1024, 1024);
    // Overlay 10% margin with bg colour so maskable cutouts look deliberate
    $inset    = (int)($px * 0.10);
    $marginClr = imagecolorallocate($outAll, $BG_COLOR[0], $BG_COLOR[1], $BG_COLOR[2]);
    imagefilledrectangle($outAll, 0, 0, $px, $inset, $marginClr);
    imagefilledrectangle($outAll, 0, $px - $inset, $px, $inset, $marginClr);
    imagefilledrectangle($outAll, 0, $inset, $inset, $px - 2 * $inset, $marginClr);
    imagefilledrectangle($outAll, $px - $inset, $inset, $inset, $px - 2 * $inset, $marginClr);

    imagepng($out,       $outFile);   // regular "any" icon
    imagepng($outAll,   $outFile);    // overwrite with maskable
    imagedestroy($out);
    imagedestroy($outAll);
    echo "  ✓ $label  →  $outFile\n";
    $generated++;
}

echo "\n✓ Generated $generated icon files in $ICONS_DIR\n";
echo "Next steps:\n";
echo "  1. Replace /invcount/images/icons/* with your brand PNGs.\n";
echo "  2. Re-run:  php scripts/generate-pwa-icons.php\n";

imagedestroy($src ?? null);
