#!/usr/bin/env node
// verify.mjs: "Not an Acronym" (the backronym myth)
//
// Checks the internal consistency of this embedded source summary. It does not
// read the page or independently establish any etymology. Read sources.md for the
// evidence, including earlier RADAR and LASER records than the dictionary dates.
//
// Dates give context. There is no first-possible-year rule for acronyms here.
// The classification follows the recorded origin evidence for each word:
//   special === 'morse' -> 'not-an-abbreviation'
//   evidence === true   -> 'genuine'
//   rejected === true   -> 'myth' (source rejects initials and gives another route)
//   otherwise           -> 'unproven' (POSH's proposed acronym lacks evidence)
//
// Run: node research/backronym-myth/verify.mjs

// year = a date supported by the record, not a guarantee of first use.
// NEWS uses 1390 only as a plotting position for the late 1300s.
// originUncertain flags the three deeper origins discussed as uncertain here.
const WORDS = [
  { id: 'golf', word: 'GOLF', year: 1457, claim: 'Gentlemen Only, Ladies Forbidden',
    evidence: false, rejected: true, label: 'myth', originUncertain: false,
    source: 'https://www.etymonline.com/word/golf',
    note: 'Scots gouf, usually linked to Middle Dutch colf. The 1457 statute quotation is sourced in sources.md.' },
  { id: 'news', word: 'NEWS', year: 1390, claim: 'North, East, West, South',
    evidence: false, rejected: true, label: 'myth', originUncertain: false,
    source: 'https://www.etymonline.com/word/news',
    note: 'Late 1300s, plotted approximately; plural of new; compass story a 1640 jest.' },
  { id: 'tip', word: 'TIP', year: 1706, claim: 'To Insure Promptness',
    evidence: false, rejected: true, label: 'myth', originUncertain: true,
    source: 'https://www.etymonline.com/word/tip',
    note: 'Gratuity verb by 1706; promptitude story by 1909, promptness variant from 1946. Deeper origin uncertain.' },
  { id: 'cop', word: 'COP', year: 1859, claim: 'Constable On Patrol',
    evidence: false, rejected: true, label: 'myth', originUncertain: true,
    source: 'https://www.etymonline.com/word/cop',
    note: 'Noun 1859, clipping of copper 1846, from verb cop 1704. Deeper root uncertain: perhaps French/Latin or Dutch/Frisian.' },
  { id: 'fuck', word: 'FUCK', year: 1475, claim: 'Fornication Under Consent of the King',
    evidence: false, rejected: true, label: 'myth', originUncertain: false,
    source: 'https://www.etymonline.com/word/fuck',
    note: 'Germanic; Flen flyys c. 1475 (an earlier 1310 nickname is contested); proposed cognates are uncertain.' },
  { id: 'posh', word: 'POSH', year: 1914, claim: 'Port Out, Starboard Home',
    evidence: false, rejected: false, label: 'unproven', originUncertain: true,
    source: 'https://www.etymonline.com/word/posh',
    note: 'By 1914; proposed ship-cabin acronym lacks evidence; true origin uncertain.' },
  { id: 'sos', word: 'SOS', year: 1905, claim: 'Save Our Souls',
    evidence: false, special: 'morse', label: 'not-an-abbreviation', originUncertain: false,
    source: 'https://en.wikipedia.org/wiki/SOS',
    note: 'Morse pattern adopted in German rules in 1905; expansion added later.' },
  { id: 'radar', word: 'RADAR', year: 1940, claim: 'Radio Detecting And Ranging',
    evidence: true, label: 'genuine', originUncertain: false,
    source: 'https://www.navy-radio.com/manuals/nrl-history.pdf#page=186',
    note: 'In official use by 19 November 1940. NRL history p. 170 reproduces the directive; it uses detection, while etymonline uses detecting.' },
  { id: 'scuba', word: 'SCUBA', year: 1952, claim: 'Self-Contained Underwater Breathing Apparatus',
    evidence: true, label: 'genuine', originUncertain: false,
    source: 'https://www.etymonline.com/word/scuba', note: 'Genuine acronym, 1952.' },
  { id: 'laser', word: 'LASER', year: 1957, claim: 'Light Amplification by Stimulated Emission of Radiation',
    evidence: true, label: 'genuine', originUncertain: false,
    source: 'https://history.aip.org/exhibits/laser/index.html#section4',
    note: 'Named in a 1957 notebook; the AIP account antedates etymonline 1960.' },
  { id: 'snafu', word: 'SNAFU', year: 1941, claim: 'Situation Normal, All Fouled Up',
    evidence: true, label: 'genuine', originUncertain: false,
    source: 'https://www.etymonline.com/word/snafu', note: 'Expression by 1941; noun sense 1943. Fouled is a euphemism.' },
];

function derive(w) {
  if (w.special === 'morse') return 'not-an-abbreviation';
  if (w.evidence) return 'genuine';
  return w.rejected ? 'myth' : 'unproven';
}

let pass = 0, fail = 0;
const log = (ok, msg) => { ok ? pass++ : fail++; console.log(`  ${ok ? 'PASS' : 'FAIL'}  ${msg}`); };
console.log('\nNot an Acronym: embedded-table consistency\n');

// 1. Each embedded label agrees with its recorded evidence flags.
for (const w of WORDS) {
  const d = derive(w);
  log(d === w.label, `${w.word.padEnd(6)} -> ${d}`);
}

// 2. These four selected genuine examples are twentieth-century and cite evidence.
// This is a property of the selection, not a rule about all acronyms.
for (const w of WORDS.filter(w => w.label === 'genuine')) {
  log(w.year >= 1901 && w.year <= 2000 && w.evidence === true && /^https:\/\//.test(w.source),
    `${w.word}: twentieth-century dated evidence (${w.year}) with a source reference`);
}

// 3. Each myth entry records a source rejection and an alternative origin account.
// The script checks the record's fields, not whether the external source is right.
for (const w of WORDS.filter(w => w.label === 'myth')) {
  log(w.rejected === true && w.evidence === false && /^https:\/\//.test(w.source) && w.note.length > 20,
    `${w.word}: source rejection and origin account recorded`);
}

// 4. The unresolved-origin case preserves both kinds of uncertainty.
for (const w of WORDS.filter(w => w.label === 'unproven')) {
  log(w.evidence === false && w.originUncertain === true,
    `${w.word}: proposed acronym lacks evidence; true origin flagged uncertain`);
}

// 5. Check the three deeper origins singled out in the page's uncertainty paragraph.
const uncertainIds = WORDS.filter(w => w.originUncertain).map(w => w.id).sort().join(',');
log(uncertainIds === 'cop,posh,tip',
  `discussed uncertain-origin set: {${uncertainIds}}`);

// 6. The categories account for all eleven cards.
const tally = WORDS.reduce((m, w) => (m[w.label] = (m[w.label] || 0) + 1, m), {});
log(tally.genuine === 4 && tally.myth === 5 && tally.unproven === 1 && tally['not-an-abbreviation'] === 1,
  `card tally: ${JSON.stringify(tally)} (expect 5 myth, 1 unproven, 1 Morse, 4 genuine)`);

console.log(`\n${pass} passed, ${fail} failed.\n`);
process.exit(fail ? 1 : 0);
