import sharp from "sharp";
import { mkdirSync } from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const publicDir = join(__dirname, "..", "public");

const ACCENT = "#10b981";
const BG_DARK = "#030712";

function buildSvg(size) {
  const fontSize = Math.round(size * 0.32);
  const radius = Math.round(size * 0.18);
  return `<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 ${size} ${size}">
    <rect width="${size}" height="${size}" rx="${radius}" fill="${BG_DARK}"/>
    <text x="50%" y="54%" text-anchor="middle" dominant-baseline="central"
          font-family="system-ui,-apple-system,sans-serif" font-weight="700"
          font-size="${fontSize}" fill="${ACCENT}">NW</text>
  </svg>`;
}

const sizes = [192, 512];

for (const size of sizes) {
  const svg = Buffer.from(buildSvg(size));
  const out = join(publicDir, `icon-${size}x${size}.png`);
  await sharp(svg).resize(size, size).png().toFile(out);
  console.log(`✓ ${out}`);
}

const appleSvg = Buffer.from(buildSvg(180));
const appleOut = join(publicDir, "apple-touch-icon.png");
await sharp(appleSvg).resize(180, 180).png().toFile(appleOut);
console.log(`✓ ${appleOut}`);

console.log("\nPWA icons generated.");
