import { readFileSync, writeFileSync } from 'fs';

const content = readFileSync('src/data/seoData.ts', 'utf8');

// These are the mojibake sequences (UTF-8 bytes misread as Latin-1)
// and their correct Unicode replacements
const replacements = [
  ['â€"', '—'],   // em dash
  ['â€"', '–'],   // en dash (different byte sequence)
  ['â€™', '\u2019'], // right single quote / apostrophe
  ['â€˜', '\u2018'], // left single quote
  ['â€œ', '\u201c'], // left double quote
  ['â€', '\u201d'],  // right double quote
  ['â€¦', '…'],   // ellipsis
  ['â€¢', '•'],   // bullet
];

let fixed = content;
for (const [bad, good] of replacements) {
  const count = fixed.split(bad).length - 1;
  if (count > 0) console.log(`Replacing ${count}x: "${bad}" → "${good}"`);
  fixed = fixed.split(bad).join(good);
}

// Check for any remaining â sequences
const remaining = fixed.match(/â[^\w\s'"]{1,3}/g);
if (remaining) {
  console.log('Remaining unhandled sequences:', [...new Set(remaining)]);
} else {
  console.log('No remaining mojibake found.');
}

if (content !== fixed) {
  writeFileSync('src/data/seoData.ts', fixed, 'utf8');
  console.log('File saved successfully.');
} else {
  console.log('No changes needed.');
}
