#!/usr/bin/env node
import { readFileSync } from 'fs';
import path from 'path';

const file = path.resolve('src/lib/data/calculators.ts');
const text = readFileSync(file, 'utf8');
const start = text.indexOf('[');
const end = text.indexOf('];', start);
if (start === -1 || end === -1) {
  console.error('Unable to locate calculators array in calculators.ts');
  process.exit(1);
}
let arr = text.slice(start, end + 1).trim();
arr = arr.replace(/,\s*\]$/, ']');
let calculators;
try {
  calculators = JSON.parse(arr);
} catch (err) {
  console.error('Failed to parse calculators.ts as JSON:', err.message);
  process.exit(1);
}

const offenders = calculators.filter(c =>
  ['3col', '3col-mul'].includes(c.type) &&
  c.name.toLowerCase().includes(' to ') &&
  (!c.labels?.in3 || c.labels.in3.toLowerCase() === 'result')
);

if (offenders.length) {
  console.error(`3-col calculators with vague or missing output labels: ${offenders.length}`);
  offenders.slice(0, 20).forEach(c => console.error(`- ${c.slug}`));
  if (offenders.length > 20) console.error(`…and ${offenders.length - 20} more`);
  process.exit(1);
}

console.log('3-col label lint passed.');
