/**
 * Build assets/watermark.png for the default Songs2VID badge overlay.
 * Uses bundled Arimo + FFmpeg (same toolchain as video encode).
 *
 * Run: node scripts/generate-watermark-png.mjs
 */
import { spawnSync } from "child_process";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import ffmpegStatic from "ffmpeg-static";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const root = path.join(__dirname, "..");
const fontPath = path.join(root, "assets", "fonts", "Arimo-Regular.ttf");
const outPath = path.join(root, "assets", "watermark.png");

function ffmpegPath() {
  return process.env.FFMPEG_PATH || ffmpegStatic || "ffmpeg";
}

function escFont(p) {
  return p.replace(/\\/g, "/").replace(/:/g, "\\:");
}

if (!fs.existsSync(fontPath)) {
  console.error("Missing font:", fontPath, "— run node scripts/fetch-watermark-fonts.mjs first");
  process.exit(1);
}

const font = escFont(fontPath);
const w = 960;
const h = 88;
const filter = [
  `[0:v]format=rgba,drawbox=x=0:y=0:w=iw:h=ih:color=0x000000@0.45:t=fill`,
  `drawtext=fontfile='${font}':text='Uploaded through Songs2VID.com':fontsize=32:fontcolor=white@0.92:x=(w-text_w)/2:y=(h-th)/2`,
].join(",");

const args = [
  "-y",
  "-f",
  "lavfi",
  "-i",
  `color=c=black@0.0:s=${w}x${h}`,
  "-vf",
  filter,
  "-update",
  "1",
  outPath,
];

const res = spawnSync(ffmpegPath(), args, { stdio: "inherit" });
if (res.status !== 0) {
  process.exit(res.status ?? 1);
}

console.log("wrote", outPath, fs.statSync(outPath).size, "bytes");
