/**
 * scripts/fix-remaining-logos.js
 *
 * The earlier replace-logo-assets.js caught files that already had a
 * logo-mark.png. The leftover files have the OLD shield SVG inline,
 * spread across multi-line markup. Regex sweep + replacement.
 *
 * Matches:
 *   <a href="..." class="brand" aria-label="takedowns home">
 *     <div class="brand-logo" aria-hidden="true">
 *       <svg viewBox="...">
 *         <path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"></path>
 *       </svg>
 *     </div>
 *     <span>takedowns</span>
 *   </a>
 *
 * Replaces the SVG with <img src="/public/og/logo-mark.png"> and
 * preserves the original href. 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'));

// Multi-line aware. Captures the original href so we keep the link target.
const re = /<a\s+href="([^"]+)"\s+class="brand"\s+aria-label="takedowns home">\s*<div class="brand-logo" aria-hidden="true">\s*<svg[^>]*>\s*<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"><\/path>\s*<\/svg>\s*<\/div>\s*<span>takedowns<\/span>\s*<\/a>/g;

let changed = 0;
let replacements = 0;
for (const f of files) {
  const fp = path.join(dir, f);
  let html = fs.readFileSync(fp, 'utf8');
  const before = html;
  html = html.replace(re, (match, href) => {
    replacements++;
    return `<a href="${href}" class="brand" aria-label="takedowns home">\n        <div class="brand-logo" aria-hidden="true">\n          <img src="/public/og/logo-mark.png" alt="" width="32" height="32" />\n        </div>\n        <span>takedowns</span>\n      </a>`;
  });
  if (html !== before) {
    fs.writeFileSync(fp, html);
    changed++;
  }
}
console.log('Updated', changed, 'files; replaced', replacements, 'shield SVGs with logo-mark.png.');
