import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import sharp from 'sharp';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

// Icon sizes needed for PWA
const sizes = [72, 96, 128, 144, 152, 192, 384, 512];

// A/H 几何图形路径（坐标基于 512x512 viewBox，75% 缩放居中，padding ~22%）
// 精简为：A 外框 + 中心圆圈 + H 横杠，去掉重叠的辐射线
const LOGO_PATHS = `
    <path d="M 112 400 L 256 112 L 400 400"/>
    <circle cx="256" cy="256" r="36"/>
    <path d="M 190 330 L 322 330"/>`;

// PWA 图标：白色背景 + 深蓝 logo
function createIconSVG(size) {
  return `<svg width="${size}" height="${size}" viewBox="0 0 512 512" fill="none" xmlns="http://www.w3.org/2000/svg">
  <rect width="512" height="512" rx="113" fill="#FFFFFF"/>
  <g fill="none" stroke="#0F172A" stroke-width="30" stroke-linecap="round" stroke-linejoin="round">${LOGO_PATHS}
  </g>
</svg>`;
}

// 应用 logo：蓝色背景 + 白色 logo（与现有配色一致）
function createLogoSVG(size) {
  return `<svg width="${size}" height="${size}" viewBox="0 0 512 512" fill="none" xmlns="http://www.w3.org/2000/svg">
  <rect width="512" height="512" rx="113" fill="hsl(221.2 83.2% 53.3%)"/>
  <g fill="none" stroke="#FFFFFF" stroke-width="30" stroke-linecap="round" stroke-linejoin="round">${LOGO_PATHS}
  </g>
</svg>`;
}

async function generateIcons() {
  const iconsDir = path.join(__dirname, 'icons');

  for (const size of sizes) {
    const svgContent = createIconSVG(size);
    const svgPath = path.join(iconsDir, `icon-${size}x${size}.svg`);
    const pngPath = path.join(iconsDir, `icon-${size}x${size}.png`);

    fs.writeFileSync(svgPath, svgContent);
    console.log(`Created icon-${size}x${size}.svg`);

    await sharp(Buffer.from(svgContent))
      .resize(size, size)
      .png()
      .toFile(pngPath);
    console.log(`Created icon-${size}x${size}.png`);
  }

  // favicon.svg / favicon.png (白色背景 + 深蓝)
  const faviconSvg = createIconSVG(32);
  fs.writeFileSync(path.join(__dirname, 'favicon.svg'), faviconSvg);
  console.log('Created favicon.svg');

  await sharp(Buffer.from(faviconSvg))
    .resize(32, 32)
    .png()
    .toFile(path.join(__dirname, 'favicon.png'));
  console.log('Created favicon.png');

  // logo.svg + logo-{size}.png（蓝色背景 + 白色，与应用配色一致）
  const logoSizes = [32, 64, 128, 256, 512];
  const logoSvg = createLogoSVG(512);
  fs.writeFileSync(path.join(__dirname, 'logo.svg'), logoSvg);
  console.log('Created logo.svg');

  for (const size of logoSizes) {
    await sharp(Buffer.from(logoSvg))
      .resize(size, size)
      .png()
      .toFile(path.join(__dirname, `logo-${size}.png`));
    console.log(`Created logo-${size}.png`);
  }

  console.log('\nAll icons generated successfully!');
}

generateIcons().catch(console.error);
