#!/usr/bin/env node
// verify-does-thinking-hard-burn-calories.mjs
//
// Offline gate for /strata/does-thinking-hard-burn-calories/.
// Recomputes EVERY number shown on the page from first principles / published constants,
// prints one line per check, exits 0 iff all pass. Where possible each fact is checked
// TWO independent ways.
//
// Sources (see README.md):
//   - Brain = 2% mass, ~20% resting energy; near-constant baseline: SciAm (Jabr 2012);
//     Raichle & Mintun 2006 ("<5% task-evoked relative to basal", 60-80% baseline).
//   - RMR: Mifflin-St Jeor (1990). Cross-checked vs revised Harris-Benedict (Roza 1984).
//   - Banana ~105 kcal (USDA). 1 kcal = 4184 J; 1 day = 86400 s.

let PASS = true;
const results = [];
function check(name, got, want, tol, unit = '') {
  const ok = Math.abs(got - want) <= tol;
  if (!ok) PASS = false;
  results.push(
    `${ok ? 'PASS' : 'FAIL'}  ${name}: got ${round(got)}${unit}  want ${round(want)}±${tol}${unit}`
  );
  return ok;
}
function assert(name, cond, detail = '') {
  if (!cond) PASS = false;
  results.push(`${cond ? 'PASS' : 'FAIL'}  ${name}${detail ? '  (' + detail + ')' : ''}`);
  return cond;
}
function round(x) { return Math.abs(x) >= 100 ? Math.round(x) : Math.round(x * 100) / 100; }

// ── the exact model the page runs ───────────────────────────────────────────
const BRAIN_SHARE = 0.20;         // brain = ~20% of RMR
const TASK_BUMP   = 0.05;         // <5% task-evoked increase (Raichle & Mintun), applied whole-brain
const KCAL_TO_W   = 4184 / 86400; // kcal/day -> watts
const BANANA_KCAL = 105;

// Mifflin-St Jeor, kcal/day (w kg, h cm, a yr)
const mifflin = (w, h, a, male) => 10 * w + 6.25 * h - 5 * a + (male ? 5 : -161);
// Revised Harris-Benedict (Roza & Shizgal 1984) — INDEPENDENT cross-check formula
const harris = (w, h, a, male) => male
  ? 88.362 + 13.397 * w + 4.799 * h - 5.677 * a
  : 447.593 + 9.247 * w + 3.098 * h - 4.330 * a;

const brainBaseline = (rmr) => BRAIN_SHARE * rmr;
const brainWatts    = (bk)  => bk * KCAL_TO_W;
const thinkExtra    = (bk, effort, hours) => TASK_BUMP * effort * (bk / 24) * hours;

console.log('=== does-thinking-hard-burn-calories · verifier ===\n');

// ── 1. Canonical example shown on the page: 70 kg / 170 cm / 30 yr male ──────
const R  = mifflin(70, 170, 30, true);
const Bk = brainBaseline(R);
const Bw = brainWatts(Bk);
const extra8 = thinkExtra(Bk, 1.0, 8);

check('RMR (70kg/170cm/30yr male), Mifflin-St Jeor', R, 1617.5, 0.5, ' kcal/day');
check('brain baseline = 20% of RMR',                 Bk, 323.5, 0.5, ' kcal/day');
check('brain power in watts',                        Bw, 15.67, 0.1, ' W');
check('extra from 8 h @100% effort',                 extra8, 5.39, 0.05, ' kcal');
check('extra as % of a 105-kcal banana',             extra8 / BANANA_KCAL * 100, 5.13, 0.05, ' %');

// page rounds these into the static readouts / table:
check('page readout: RMR rounds to 1,618',   Math.round(R), 1618, 0, '');
check('page readout: brain rounds to 324',   Math.round(Bk), 324, 0, '');
check('page readout: watts rounds to 16',    Math.round(Bw), 16, 0, ' W');
check('page readout: extra rounds to 5.4',   Math.round(extra8 * 10) / 10, 5.4, 0, ' kcal');

// ── 2. INDEPENDENT cross-check of RMR via a different published equation ─────
const Rh = harris(70, 170, 30, true);
assert('RMR agrees across two equations (Mifflin vs Harris-Benedict) within 10%',
  Math.abs(R - Rh) / R < 0.10, `Mifflin ${round(R)} vs Harris ${round(Rh)}`);

// ── 3. INDEPENDENT cross-check of brain power vs the literature ~20 W anchor ─
// Raichle: brain ~20 W. Our computed watts for a normal RMR must sit in a plausible band.
assert('brain power lands in the literature band (~12-25 W)',
  Bw > 12 && Bw < 25, `${round(Bw)} W`);
// second route: a commonly-quoted 2000 kcal RMR -> 20% -> 400 kcal/day -> watts
const wFrom2000 = brainWatts(0.20 * 2000);
check('sanity: 2000 kcal RMR -> brain ~400 kcal -> ~19.4 W', wFrom2000, 19.37, 0.1, ' W');

// ── 4. The headline verdict: even the MOST generous inputs stay below a banana ─
let worst = 0;
for (const w of [40, 70, 100, 140]) {
  for (const male of [true, false]) {
    const bk = brainBaseline(mifflin(w, 210, 14, male)); // tall, young -> highest RMR
    worst = Math.max(worst, thinkExtra(bk, 1.0, 16));    // max effort, 16 h
  }
}
assert('EVEN the max-everything case stays under one banana (105 kcal)',
  worst < BANANA_KCAL, `worst = ${round(worst)} kcal`);
assert('max case is still a "snack" (< 20 kcal)',
  worst < 20, `worst = ${round(worst)} kcal`);

// ── 5. Comparison-table multipliers (vs the ~5.4 kcal example) ──────────────
const mult = (k) => k / extra8;
check('banana is ~19x the thinking extra',            mult(105), 19.5, 0.6, 'x');
check('one almond (7 kcal) is ~1.3x',                 mult(7),   1.30, 0.1, 'x');
check('standing 8 h (~170 kcal) is ~31x',             mult(170), 31.5, 1.0, 'x');
check('20-min walk (~80 kcal) is ~15x',               mult(80),  14.8, 0.6, 'x');
check('brain baseline over same 8 h (~108 kcal) ~20x', mult(Bk / 24 * 8), 20.0, 0.6, 'x');

// ── 6. Published anchors the prose asserts (documented facts) ───────────────
assert('brain mass fraction ~2% and energy share ~20% are the stated constants',
  BRAIN_SHARE === 0.20, 'BRAIN_SHARE = 0.20');
assert('task-evoked bump constant is Raichle & Mintun\'s <5% upper figure',
  TASK_BUMP <= 0.05, `TASK_BUMP = ${TASK_BUMP}`);
// Raichle: 60-80% of budget is baseline signalling -> task-evoked is the small remainder.
assert('applying 5% whole-brain is generous vs the ~0.5-1% whole-brain reality',
  TASK_BUMP > 0.01, 'the page overstates the bump on purpose; truth is smaller');

// ── 7. Monotonicity claims the page draws ───────────────────────────────────
// Human curve: energy rises only weakly with effort (slope = 5% of baseline, tiny vs baseline).
const humanLo = Bk + thinkExtra(Bk, 0.0, 24);   // effort 0
const humanHi = Bk + thinkExtra(Bk, 1.0, 24);   // effort 1
assert('human: total energy rises with effort but by <6% (near-flat curve)',
  humanHi > humanLo && (humanHi - humanLo) / humanLo < 0.06,
  `+${round((humanHi - humanLo) / humanLo * 100)}%`);
// AI side is DIRECTION ONLY: assert only monotonic-increasing, no magnitude claimed.
const aiCurve = (t) => 0.06 + 0.9 * t;          // the schematic the page plots (arbitrary units)
let aiMono = true;
for (let t = 0; t < 1; t += 0.05) if (aiCurve(t + 0.05) <= aiCurve(t)) aiMono = false;
assert('AI: curve is strictly monotonic in tokens (direction only, no magnitude asserted)', aiMono);

// ── report ──────────────────────────────────────────────────────────────────
console.log(results.join('\n'));
console.log(`\n${PASS ? 'ALL CHECKS PASSED' : 'SOME CHECKS FAILED'}`);
process.exit(PASS ? 0 : 1);
