// verify.mjs — the check behind "The Quote That Arrived Before She Did".
//
// Rousseau's Book VI predates Marie Antoinette's arrival in France. The six
// comparisons below check that chronology under the selected composition dates.
// They do not establish that she could never have repeated an older sentence.
// Historical rejection of the attribution rests on the sources in facts.md,
// including the absence of a known contemporary record of her saying it.
//
//   node research/let-them-eat-cake/verify.mjs
//
// Every date below carries a source. Where the historical record is itself
// uncertain (the exact composition year; who first pinned the line on the queen),
// the uncertainty is encoded as a RANGE and the page states it as a range too —
// never a false precision. The anachronism survives the worst case of every range.

const E = [];
function ev(o) { E.push(o); return o; }

// --- the woman -------------------------------------------------------------
const BORN = ev({
  key: 'born', year: 1755, label: 'Marie Antoinette born (Vienna)',
  src: 'Britannica; Château de Versailles — 2 November 1755',
});
const ARRIVE = ev({
  key: 'arrive', year: 1770, label: 'Arrives in France; marries the Dauphin',
  src: 'Britannica; en.chateauversailles.fr — May 1770, age 14',
});
const QUEEN = ev({
  key: 'queen', year: 1774, label: 'Becomes queen consort of France',
  src: 'Britannica — Louis XVI acceded 10 May 1774',
});
const DIED = ev({
  key: 'died', year: 1793, label: 'Executed',
  src: 'Britannica — 16 October 1793',
});

// --- the line --------------------------------------------------------------
// Rousseau composed Books I–VI of the Confessions between 1765 and 1767. We take
// the LATEST plausible year (1767) as the worst case for the anachronism: even if
// he wrote the brioche line as late as possible, she was still a child in Austria.
const WRITTEN = ev({
  key: 'written', yearRange: [1765, 1767], label: 'Rousseau writes the line (Confessions, Book VI)',
  src: 'Wikipedia (Confessions); Britannica — Books I–VI composed c.1765–1767',
});
// The episode Rousseau is NARRATING belongs to his own youth, the late 1730s — so
// the "great princess" he half-remembers predates even the writing. (Used on the
// page only as "his youth / late 1730s", never as a single asserted year.)
const EPISODE = ev({
  key: 'episode', yearRange: [1736, 1740], label: "Setting of Rousseau's anecdote (his youth)",
  src: 'Confessions, Book VI covers c.1737–1740 of his life',
});
const PUBLISHED = ev({
  key: 'published', year: 1782, label: 'Confessions Part I published (posthumously; Rousseau d. 1778)',
  src: 'Wikipedia (Confessions) — Part I, Books I–VI, published 1782',
});
// The line is first widely attached to Marie Antoinette only in the 19th century.
// Karr mentions the attribution in November 1841, leaving its truth open. He
// rejects it in April 1843. The range spans these two distinct printed passages.
const ATTRIB = ev({
  key: 'attrib', yearRange: [1841, 1843], label: 'Karr mentions the attribution (1841); rejects it (1843)',
  src: 'Alphonse Karr, Les Guêpes, November 1841 and April 1843; ' +
       'https://www.gutenberg.org/files/42798/42798-h/42798-h.htm',
});

// --- helpers ---------------------------------------------------------------
const lo = (e) => e.year ?? e.yearRange[0];
const hi = (e) => e.year ?? e.yearRange[1];
const ageAt = (year) => year - BORN.year;

// --- the checks ------------------------------------------------------------
const checks = [];
function assert(name, cond, detail) {
  checks.push({ name, pass: !!cond, detail });
}

// 1. The line was written before she ever reached France — even in the worst case
//    (latest possible writing year vs. her arrival).
assert(
  'written-before-she-arrived-in-France',
  hi(WRITTEN) < ARRIVE.year,
  `latest plausible writing year ${hi(WRITTEN)} < arrival ${ARRIVE.year} ` +
  `(gap ≥ ${ARRIVE.year - hi(WRITTEN)} years)`
);

// 2. She was a CHILD when the line was written — give her age at both ends of the
//    composition window.
assert(
  'she-was-a-child-when-it-was-written',
  ageAt(hi(WRITTEN)) < 13,
  `aged ${ageAt(lo(WRITTEN))}–${ageAt(hi(WRITTEN))} during composition ${lo(WRITTEN)}–${hi(WRITTEN)}`
);

// 3. The line was written before she became a public figure anyone would quote
//    (queenship).
assert(
  'written-before-she-was-queen',
  hi(WRITTEN) < QUEEN.year,
  `latest writing ${hi(WRITTEN)} < queen ${QUEEN.year} (gap ≥ ${QUEEN.year - hi(WRITTEN)} years)`
);

// 4. The attribution to her postdates her DEATH — it is retrospective, not
//    contemporary revolutionary propaganda.
assert(
  'attribution-postdates-her-death',
  lo(ATTRIB) > DIED.year,
  `earliest attribution ${lo(ATTRIB)} > execution ${DIED.year} ` +
  `(at least ${lo(ATTRIB) - DIED.year} years after she died)`
);

// 5. Internal consistency of the printed record: written < published, and the
//    narrated episode predates the writing.
assert('written-before-published', hi(WRITTEN) <= PUBLISHED.year, `${hi(WRITTEN)} ≤ ${PUBLISHED.year}`);
assert('episode-predates-writing', hi(EPISODE) < lo(WRITTEN), `episode ≤ ${hi(EPISODE)} < writing ≥ ${lo(WRITTEN)}`);

// --- report ----------------------------------------------------------------
const fmt = (e) => e.year != null ? String(e.year) : `${e.yearRange[0]}–${e.yearRange[1]}`;
console.log('\nThe Quote That Arrived Before She Did — chronology check\n');
console.log('Timeline (each dated event is sourced; see facts.md):');
for (const e of [EPISODE, BORN, WRITTEN, ARRIVE, QUEEN, PUBLISHED, DIED, ATTRIB]) {
  const age = e.year != null
    ? (e.year >= BORN.year ? `  [she is ${ageAt(e.year)}]` : '  [before her birth]')
    : (e.yearRange[1] >= BORN.year ? `  [she is ${ageAt(e.yearRange[0])}–${ageAt(e.yearRange[1])}]` : '  [before her birth]');
  console.log(`  ${fmt(e).padEnd(11)} ${e.label}${e.key === 'born' ? '' : age}`);
  console.log(`              └─ ${e.src}`);
}

console.log('\nChecks:');
let ok = true;
for (const c of checks) {
  console.log(`  ${c.pass ? 'PASS' : 'FAIL'}  ${c.name}\n        ${c.detail}`);
  ok = ok && c.pass;
}
console.log(`\n${ok ? 'ALL CHECKS PASS' : 'SOME CHECKS FAILED'} — ${checks.length} assertions, ${E.length} dated events.\n`);

// Emit the same data as JSON for the page to import, so page and verifier never
// drift: the numbers on screen are the numbers checked here.
if (process.argv.includes('--json')) {
  const out = { events: E.map((e) => ({ key: e.key, year: e.year ?? null, yearRange: e.yearRange ?? null, label: e.label, src: e.src })), born: BORN.year };
  console.log(JSON.stringify(out, null, 2));
}

process.exit(ok ? 0 : 1);
