/**
 * scripts/replace-logo-assets.js
 *
 * Update all HTML templates to use the new PNG favicon + brand mark
 * (built from logotakedowns.png by scripts/build-favicons.js).
 *
 * Replaces:
 *   - favicon links (favicon.svg, ico fallbacks)  →  favicon-32.png + favicon-16.png
 *   - <img src="/public/og/logo-mark.svg">       →  /public/og/logo-mark.png
 *   - <img src="/public/og/logo.svg"> (rare)     →  /public/og/logo.png
 *
 * Touches only public/views/*.html. Safe to re-run.
 */

'use strict';

const fs   = require('fs');
const path = require('path');
const dir  = path.join(__dirname, '..', 'public', 'views');
const files = fs.readdirSync(dir).filter(f => f.endsWith('.html'));

let changed = 0;
const oldFaviconLinkRe = /<link\s+rel="icon"[^>]*href="\/public\/og\/favicon\.[^"]+"[^>]*>/g;
const oldLogoMarkRe     = /\/public\/og\/logo-mark\.svg/g;
const oldLogoRe         = /\/public\/og\/logo\.svg/g;

// New favicon block (replaces the single old <link rel="icon" ...>)
const newFaviconBlock =
  '<link rel="icon" type="image/png" sizes="32x32" href="/public/og/favicon-32.png">\n  ' +
  '<link rel="icon" type="image/png" sizes="16x16" href="/public/og/favicon-16.png">\n  ' +
  '<link rel="apple-touch-icon" sizes="48x48" href="/public/og/favicon-48.png">';

for (const f of files) {
  const fp = path.join(dir, f);
  let html = fs.readFileSync(fp, 'utf8');
  const before = html;
  html = html.replace(oldFaviconLinkRe, newFaviconBlock);
  html = html.replace(oldLogoMarkRe, '/public/og/logo-mark.png');
  html = html.replace(oldLogoRe, '/public/og/logo.png');
  if (html !== before) {
    fs.writeFileSync(fp, html);
    changed++;
  }
}
console.log('Updated', changed, 'HTML files with new favicon + brand mark.');
