#!/usr/bin/env node
// verify.mjs — the check for The Law's Own Arithmetic.
//
//   node research/the-laws-own-arithmetic/verify.mjs
//
// What it proves, in order, and what each one is guarding against:
//
//  1. THE BYTES ARE THE BYTES WE DESCRIBED. Every title in sources.json is present and
//     hashes to its recorded sha256. Guards against a silently re-fetched source: the
//     eCFR is amended daily, so the same program run a week later describes a different
//     document, and the datasets must say which one.
//  2. THE EXTRACTOR STILL PASSES ITS OWN TESTS. verify-extractor.mjs, 120-plus assertions, every
//     one of them a real CFR sentence or a construction of a false-positive class the
//     prototype produced.
//  3. THE DATASETS ARE REPRODUCIBLE. Re-running the extractor over the same bytes gives
//     the same row counts and the same verdict census. Guards against a hand-edited file.
//  4. EVERY PUBLISHED QUOTE IS IN THE SOURCE. Each reviewed pair's verbatim span is
//     re-found in the title's raw XML, independently of the walker that produced it, by
//     stripping tags from a window around the block and normalising whitespace. This is
//     the one that matters: it is the difference between quoting a regulation and
//     asserting one.
//  5. THE ARITHMETIC HOLDS. Every reviewed contradiction is re-derived here from the two
//     written numbers and the exact conversion factor, without using the extractor.
//  6. THE REVIEW COVERS THE SET, EXACTLY. Every pair over the review threshold has a
//     hand-check entry and every entry matches a pair. If a later edition of the CFR
//     brings a new one in, this fails until a person reads it. That is the point: the
//     claim on the page is "these were read", and nothing else can keep that true.

import { readFileSync, existsSync } from 'node:fs';
import { createHash } from 'node:crypto';
import { execFileSync } from 'node:child_process';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Rat } from './lib/rational.mjs';
import { UNITS, convert } from './lib/units.mjs';

const HERE = dirname(fileURLToPath(import.meta.url));
const args = new Set(process.argv.slice(2));
const FAST = args.has('--fast');            // skip the re-extraction (step 3)

let failures = 0;
let checks = 0;
const ok = (name, cond, detail = '') => {
  checks++;
  if (cond) { console.log(`  ok   ${name}`); return true; }
  failures++;
  console.log(`  FAIL ${name}${detail ? `\n       ${detail}` : ''}`);
  return false;
};
const section = (s) => console.log(`\n${s}`);

const sources = JSON.parse(readFileSync(join(HERE, 'sources.json'), 'utf8'));
const review = JSON.parse(readFileSync(join(HERE, 'review.json'), 'utf8'));
const census = JSON.parse(readFileSync(join(HERE, 'out/census.json'), 'utf8'));
const pairs = readFileSync(join(HERE, 'out/pairs.jsonl'), 'utf8').trim().split('\n').map((l) => JSON.parse(l));

// ---------------------------------------------------------------------------
section('1. the source bytes');
const haveCache = sources.sources.every((s) => existsSync(join(HERE, s.path)));
if (!haveCache) {
  console.log('  --   cache/ is absent; run `node research/the-laws-own-arithmetic/fetch.mjs` first.');
  console.log('       (The cache is ~825 MB of public govinfo bulkdata and is not committed.)');
  process.exit(2);
}
let hashOk = 0;
for (const s of sources.sources) {
  const buf = readFileSync(join(HERE, s.path));
  if (buf.length === s.bytes && createHash('sha256').update(buf).digest('hex') === s.sha256) hashOk++;
}
ok(`all ${sources.sources.length} titles match their recorded sha256 and length`, hashOk === sources.sources.length,
   `${hashOk} of ${sources.sources.length} matched`);
ok('the census describes the same bytes', census.sourceDigest.every((d) => {
  const s = sources.sources.find((x) => x.title === d.title);
  return s && s.sha256 === d.sha256;
}));

// ---------------------------------------------------------------------------
section('2. the extractor passes its own tests');
try {
  const out = execFileSync(process.execPath, [join(HERE, 'verify-extractor.mjs')], { encoding: 'utf8' });
  const m = /ok — (\d+) of (\d+) assertions pass/.exec(out);
  ok(`extractor selftest: ${m ? m[0].replace('ok — ', '') : out.trim()}`, !!m && m[1] === m[2]);
} catch (err) {
  ok('the extractor selftest passes', false, String(err.stdout || err.message).trim().slice(0, 800));
}

// ---------------------------------------------------------------------------
section('3. the datasets are reproducible from the bytes');
if (FAST) {
  console.log('  --   skipped (--fast)');
} else {
  const tmp = join(HERE, 'out');
  const before = { pairs: pairs.length, verdicts: JSON.stringify(census.verdicts), quantities: census.totals.quantities };
  execFileSync(process.execPath, ['--max-old-space-size=8192', join(HERE, 'extract.mjs')], { stdio: 'ignore' });
  const after = JSON.parse(readFileSync(join(tmp, 'census.json'), 'utf8'));
  ok('re-extraction reproduces the pair count', after.totals.pairs === before.pairs,
     `${after.totals.pairs} vs ${before.pairs}`);
  ok('re-extraction reproduces the quantity count', after.totals.quantities === before.quantities,
     `${after.totals.quantities} vs ${before.quantities}`);
  ok('re-extraction reproduces the verdict census', JSON.stringify(after.verdicts) === before.verdicts,
     `${JSON.stringify(after.verdicts)}\n       vs ${before.verdicts}`);
}

// ---------------------------------------------------------------------------
// The review set, recomputed here rather than read from a file.
const gap = (r) => { const x = Math.abs(r.ratioNumber); return 1 - Math.min(x, 1 / x); };
const inReview = (r) => r.verdict === 'discrepant' && (
  (!r.affine && r.ratioNumber != null && gap(r) >= 0.25) ||
  (r.affine && (r.lastPlaceError || 0) >= 10));
const reviewed = pairs.filter(inReview);
const keyOf = (r) => `${r.cite} :: ${r.verbatim}`;

section('4. every reviewed quote is in the source bytes');
const xmlCache = new Map();
const xmlOf = (t) => {
  if (!xmlCache.has(t)) xmlCache.set(t, readFileSync(join(HERE, `cache/ECFR-title${t}.xml`), 'utf8'));
  return xmlCache.get(t);
};
// Strip tags the way a reader would, independently of lib/xml.mjs: this check must not
// share a bug with the thing it checks.
const plain = (s) => s
  .replace(/<SU>|<sup>/g, '^(').replace(/<\/SU>|<\/sup>/g, ')')
  .replace(/<E T="51">/g, '^(').replace(/<E T="52">/g, '_(').replace(/<\/E>/g, ')')
  .replace(/<[^>]*>/g, ' ')
  .replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&')
  .replace(/\s+/g, ' ');
let found = 0;
const missing = [];
for (const r of reviewed) {
  const xml = xmlOf(r.title);
  const window = plain(xml.slice(Math.max(0, r.blockStart - 200), r.blockStart + 60000));
  if (window.includes(r.verbatim.replace(/\s+/g, ' '))) found++;
  else missing.push(`${r.id} ${r.cite} :: ${r.verbatim}`);
}
ok(`all ${reviewed.length} reviewed spans re-found in the raw XML`, missing.length === 0,
   missing.slice(0, 5).join('\n       '));

section('5. the arithmetic, re-derived without the extractor');
// A small independent table. If this disagrees with lib/units.mjs, one of them is wrong.
const FACTORS = {
  in: '0.0254', ft: '0.3048', yd: '0.9144', mi: '1609.344', mm: '0.001', cm: '0.01', m: '1', km: '1000',
  lb: '0.45359237', oz: '0.028349523125', kg: '1', g: '0.001', mg: '0.000001',
  L: '0.001', mL: '0.000001', gal: '0.003785411784', pt: '0.000473176473',
  nmi: '1852', hectare: '10000', acre: '4046.8564224',
};
let redone = 0;
const mismatches = [];
for (const r of reviewed) {
  const f = FACTORS[r.a.unit];
  const g = FACTORS[r.b.unit];
  if (!f || !g || r.reading !== 'direct') continue;
  const [an, ad] = r.a.value.split('/');
  const a = Rat.of(BigInt(an), BigInt(ad || '1'));
  const expect = a.mul(Rat.parse(f)).div(Rat.parse(g));
  const printed = Rat.parse(String(r.expectedNumber));
  const rel = Math.abs(expect.toNumber() - printed.toNumber()) / Math.abs(expect.toNumber() || 1);
  redone++;
  if (rel > 1e-9) mismatches.push(`${r.id}: independent ${expect.toPrecision(10)} vs published ${r.expectedNumber}`);
}
ok(`${redone} conversions re-derived from an independent factor table`, mismatches.length === 0,
   mismatches.slice(0, 5).join('\n       '));

section('6. the review covers the set exactly');
const setKeys = new Set(reviewed.map(keyOf));
const revKeys = new Set(Object.keys(review.verdicts));
const unreviewed = [...setKeys].filter((k) => !revKeys.has(k));
const orphaned = [...revKeys].filter((k) => !setKeys.has(k));
ok(`every one of the ${setKeys.size} sentences over the threshold has been read`, unreviewed.length === 0,
   unreviewed.slice(0, 8).join('\n       '));
ok('no review entry describes a pair that is no longer there', orphaned.length === 0,
   orphaned.slice(0, 8).join('\n       '));

// ---------------------------------------------------------------------------
section('7. the second census: the factors the CFR publishes itself');
const factors = JSON.parse(readFileSync(join(HERE, 'out/factors.json'), 'utf8'));
const badFactors = factors.statements.filter((r) => r.bothUnitsExactlyDefined && !r.agreesAtWrittenFigures);
const fKey = (r) => `${r.cite} :: ${r.statement}`;
const fReviewed = new Set(Object.keys(review.factors));
const fUnreviewed = badFactors.map(fKey).filter((k) => !fReviewed.has(k));
const fOrphaned = [...fReviewed].filter((k) => !badFactors.some((r) => fKey(r) === k));
ok(`every one of the ${new Set(badFactors.map(fKey)).size} factor statements that does not come out right has been read`,
   fUnreviewed.length === 0, fUnreviewed.slice(0, 8).join('\n       '));
ok('no factor review entry describes a statement that is no longer there', fOrphaned.length === 0,
   fOrphaned.slice(0, 8).join('\n       '));
const fMissing = badFactors.filter((r) => {
  const win = plain(xmlOf(r.title).slice(Math.max(0, r.blockStart - 200), r.blockStart + 60000));
  return !win.includes(r.statement.replace(/\s+/g, ' '));
});
ok('every published factor statement is in the source bytes', fMissing.length === 0,
   fMissing.slice(0, 5).map(fKey).join('\n       '));

// ---------------------------------------------------------------------------
section('the numbers this study publishes');
const byClass = {};
for (const k of setKeys) { const c = review.verdicts[k].class; byClass[c] = (byClass[c] || 0) + 1; }
const rowsByClass = {};
for (const r of reviewed) { const c = review.verdicts[keyOf(r)].class; rowsByClass[c] = (rowsByClass[c] || 0) + 1; }
const contradictionRows = pairs.filter((r) => inReview(r) && review.verdicts[keyOf(r)].class === 'contradiction');
const titlesHit = new Set(contradictionRows.map((r) => r.title));
const report = {
  titles: sources.sources.length,
  sourceBytes: sources.totalBytes,
  quantities: census.totals.quantities,
  pairs: census.totals.pairs,
  verdicts: census.verdicts,
  readings: census.readings,
  reviewed: { sentences: setKeys.size, rows: reviewed.length, byClass, rowsByClass },
  contradictions: { sentences: [...setKeys].filter((k) => review.verdicts[k].class === 'contradiction').length,
                    rows: contradictionRows.length, titles: titlesHit.size },
  factors: {
    statements: factors.found,
    betweenExactlyDefinedUnits: factors.betweenExactlyDefinedUnits,
    correctAtTheFiguresWritten: factors.correctAtTheFiguresWritten,
    notCorrect: factors.notCorrectAtTheFiguresWritten,
    byClass: Object.values(review.factors).reduce((a, v) => ({ ...a, [v.class]: (a[v.class] || 0) + 1 }), {}),
  },
};
console.log(JSON.stringify(report, null, 2));

console.log(`\n${failures ? `${failures} of ${checks} checks FAILED` : `all ${checks} checks pass`}`);
process.exit(failures ? 1 : 0);
