const fs = require('fs');
const path = require('path');
const sharp = require('sharp');
const pngToIcoModule = require('png-to-ico');
const pngToIco = pngToIcoModule.default || pngToIcoModule;

const root = 'f:/fizzabctoapp';
const inDir = path.join(root, 'branding-input');
const desktopDir = path.join(root, 'desktop-wrapper');
const androidRes = path.join(root, 'android-wrapper', 'android', 'app', 'src', 'main', 'res');

const iconSrc = path.join(inDir, 'icon-1024.png');
const splashPortrait = path.join(inDir, 'splash-portrait.png');
const splashLandscape = path.join(inDir, 'splash-landscape.png');

function ensureDir(p) {
  fs.mkdirSync(p, { recursive: true });
}

async function writePng(src, dest, w, h, fit = 'contain') {
  ensureDir(path.dirname(dest));
  const image = sharp(src).resize(w, h, {
    fit,
    background: { r: 0, g: 0, b: 0, alpha: 0 }
  });
  await image.png({ compressionLevel: 9 }).toFile(dest);
}

async function buildWindowsIco() {
  const buildDir = path.join(desktopDir, 'build');
  ensureDir(buildDir);
  const sizes = [16, 24, 32, 48, 64, 128, 256];
  const pngBuffers = [];
  for (const size of sizes) {
    const buf = await sharp(iconSrc)
      .resize(size, size, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })
      .png()
      .toBuffer();
    pngBuffers.push(buf);
  }
  const ico = await pngToIco(pngBuffers);
  fs.writeFileSync(path.join(buildDir, 'app.ico'), ico);
  fs.writeFileSync(path.join(buildDir, 'setup.ico'), ico);
}

async function buildAndroidIcons() {
  const launcher = [
    ['mipmap-mdpi', 48],
    ['mipmap-hdpi', 72],
    ['mipmap-xhdpi', 96],
    ['mipmap-xxhdpi', 144],
    ['mipmap-xxxhdpi', 192]
  ];

  for (const [dir, size] of launcher) {
    await writePng(iconSrc, path.join(androidRes, dir, 'ic_launcher.png'), size, size);
    await writePng(iconSrc, path.join(androidRes, dir, 'ic_launcher_round.png'), size, size);
  }

  // Adaptive icon layers
  await writePng(iconSrc, path.join(androidRes, 'drawable', 'ic_launcher_foreground.png'), 432, 432);
  await writePng(iconSrc, path.join(androidRes, 'drawable', 'ic_launcher_monochrome.png'), 432, 432);
}

async function buildAndroidSplash() {
  const portSizes = [
    ['drawable-port-mdpi', 320, 480],
    ['drawable-port-hdpi', 480, 800],
    ['drawable-port-xhdpi', 720, 1280],
    ['drawable-port-xxhdpi', 960, 1600],
    ['drawable-port-xxxhdpi', 1280, 1920]
  ];

  const landSizes = [
    ['drawable-land-mdpi', 480, 320],
    ['drawable-land-hdpi', 800, 480],
    ['drawable-land-xhdpi', 1280, 720],
    ['drawable-land-xxhdpi', 1600, 960],
    ['drawable-land-xxxhdpi', 1920, 1280]
  ];

  for (const [dir, w, h] of portSizes) {
    await writePng(splashPortrait, path.join(androidRes, dir, 'splash.png'), w, h, 'cover');
  }

  for (const [dir, w, h] of landSizes) {
    await writePng(splashLandscape, path.join(androidRes, dir, 'splash.png'), w, h, 'cover');
  }

  await writePng(splashPortrait, path.join(androidRes, 'drawable', 'splash.png'), 720, 1280, 'cover');
}

async function verifyInputs() {
  for (const p of [iconSrc, splashPortrait, splashLandscape]) {
    if (!fs.existsSync(p)) {
      throw new Error(`Missing branding input: ${p}`);
    }
  }

  const m1 = await sharp(iconSrc).metadata();
  const m2 = await sharp(splashPortrait).metadata();
  const m3 = await sharp(splashLandscape).metadata();

  console.log(`icon-1024.png: ${m1.width}x${m1.height}`);
  console.log(`splash-portrait.png: ${m2.width}x${m2.height}`);
  console.log(`splash-landscape.png: ${m3.width}x${m3.height}`);
}

(async () => {
  await verifyInputs();
  await buildWindowsIco();
  await buildAndroidIcons();
  await buildAndroidSplash();
  console.log('BRANDING_ASSETS_GENERATED');
})();
