#!/usr/bin/env node
// verify.mjs — every numerical claim in the Field Note
// "A Banana Is Radioactive — and the Net Dose Is Zero" (Curiosities desk).
//
// Run:  node research/banana-radioactivity/verify.mjs
// No dependencies. The load-bearing physics figure — the specific activity of
// natural potassium and the activity of one banana — is COMPUTED from first
// principles (Avogadro, the K-40 isotopic abundance, and the K-40 half-life)
// and then required to agree with the figures quoted by Wikipedia / the
// radiation-protection literature (~31 Bq/g, ~15 Bq/banana). The dose figures
// are the published EPA and ICRP ingestion coefficients applied to that
// activity; we check that both land at ~0.1 µSv, the conventional "banana
// equivalent dose" (BED). Nothing about homeostasis is a number — it is a
// physiological fact, sourced in the note's "How we know" footer, not here.

let PASS = 0, FAIL = 0;
const log = (ok, msg) => { console.log(`  ${ok ? "✓" : "✗ FAIL"}  ${msg}`); ok ? PASS++ : FAIL++; };
const approx = (a, b, relTol) => Math.abs(a - b) <= relTol * Math.abs(b);

console.log("\nA Banana Is Radioactive — verifier\n");

// ── Physical constants (CODATA / IUPAC / standard nuclear data) ─────────────
const N_A      = 6.02214076e23;   // Avogadro's number, /mol (exact, SI 2019)
const M_K      = 39.0983;         // molar mass of natural potassium, g/mol (IUPAC)
const abund40  = 0.000117;        // K-40 atom fraction of natural K = 0.0117%
const t_half_y = 1.248e9;         // K-40 half-life, years (current evaluated value)
const SEC_PER_YEAR = 365.25 * 86400; // Julian year, s
const LN2      = Math.log(2);

// ── (1) Specific activity of natural potassium, from first principles ───────
// A = N·λ, where N = (atoms K per gram)·(K-40 fraction), λ = ln2 / half-life.
const atomsK_perGram = N_A / M_K;                 // atoms of K per gram of natural K
const atoms40_perGram = atomsK_perGram * abund40; // K-40 atoms per gram of natural K
const t_half_s = t_half_y * SEC_PER_YEAR;         // half-life in seconds
const lambda = LN2 / t_half_s;                    // decay constant, /s
const specificActivity = atoms40_perGram * lambda; // Bq per gram of natural K

console.log(`  computed: K-40 atoms per gram of natural K = ${atoms40_perGram.toExponential(4)}`);
console.log(`  computed: K-40 decay constant λ = ${lambda.toExponential(4)} /s`);
console.log(`  computed: specific activity of natural K = ${specificActivity.toFixed(2)} Bq/g\n`);

log(approx(specificActivity, 31, 0.05),
    `specific activity ≈ 31 Bq/g (literature) — computed ${specificActivity.toFixed(2)} Bq/g, within 5%`);

// ── (2) Activity of one banana ──────────────────────────────────────────────
// A "typical" banana is quoted at ~0.5 g of potassium (Wikipedia). USDA gives a
// medium banana ~422 mg. We check the round 0.5 g figure lands at ~15 Bq, and
// that the realistic 0.42–0.51 g range straddles it.
const K_per_banana = 0.5;                          // g, the figure the BED literature uses
const activity_banana = specificActivity * K_per_banana; // Bq
console.log(`  computed: activity of one ~0.5 g-K banana = ${activity_banana.toFixed(1)} Bq\n`);

log(approx(activity_banana, 15, 0.10),
    `one banana ≈ 15 Bq (literature) — computed ${activity_banana.toFixed(1)} Bq, within 10%`);
log(specificActivity * 0.422 > 12 && specificActivity * 0.510 < 17,
    `USDA range 422–510 mg K → ${(specificActivity*0.422).toFixed(1)}–${(specificActivity*0.510).toFixed(1)} Bq, brackets 15 Bq`);

// ── (3) The banana equivalent dose (BED), both published coefficients ───────
// Committed effective dose = activity (Bq) × ingestion dose coefficient (Sv/Bq).
const COEFF_EPA  = 5.02e-9;  // Sv/Bq — US EPA committed dose equiv. over 50 yr, K-40
const COEFF_ICRP = 6.2e-9;   // Sv/Bq — ICRP ingestion coefficient, K-40
const dose_EPA_uSv  = activity_banana * COEFF_EPA  * 1e6; // µSv
const dose_ICRP_uSv = activity_banana * COEFF_ICRP * 1e6; // µSv
console.log(`  computed: BED via EPA coeff  = ${dose_EPA_uSv.toFixed(3)} µSv`);
console.log(`  computed: BED via ICRP coeff = ${dose_ICRP_uSv.toFixed(3)} µSv\n`);

log(dose_EPA_uSv > 0.06 && dose_EPA_uSv < 0.10,
    `EPA → ${dose_EPA_uSv.toFixed(3)} µSv (rounds to the conventional 0.1 µSv)`);
log(dose_ICRP_uSv > 0.08 && dose_ICRP_uSv < 0.12,
    `ICRP → ${dose_ICRP_uSv.toFixed(3)} µSv (≈ the conventional 0.1 µSv)`);
log(approx(0.1, dose_EPA_uSv, 0.5) && approx(0.1, dose_ICRP_uSv, 0.5),
    `both coefficients agree with the stated "≈ 0.1 µSv per banana" to better than a factor of 2`);

// ── (4) Comparison figures quoted in the note (1 BED = 0.1 µSv) ─────────────
const BED_uSv = 0.1;
// "Average daily background ≈ 100 BED" → 10 µSv/day → ~3.65 mSv/yr.
const dailyBackground_uSv = 100 * BED_uSv;
const annualBackground_mSv = dailyBackground_uSv * 365.25 / 1000;
console.log(`  computed: 100 BED/day = ${dailyBackground_uSv} µSv/day → ${annualBackground_mSv.toFixed(2)} mSv/yr\n`);
log(annualBackground_mSv > 3 && annualBackground_mSv < 4,
    `100 BED/day ⇒ ${annualBackground_mSv.toFixed(2)} mSv/yr — in the cited ~3 mSv/yr natural-background range`);

// A chest CT scan, quoted at 7 mSv = 70,000 BED.
const CT_mSv = 7;
const CT_in_BED = CT_mSv * 1000 / BED_uSv;
log(CT_in_BED === 70000,
    `chest CT (7 mSv) = ${CT_in_BED.toLocaleString('en-US')} BED — matches the quoted 70,000 BED`);

// One banana is ~1 BED, i.e. ~1/100 of a day's background.
const banana_in_BED = ((dose_EPA_uSv + dose_ICRP_uSv) / 2) / BED_uSv;
log(banana_in_BED > 0.7 && banana_in_BED < 1.2,
    `one banana ≈ ${banana_in_BED.toFixed(2)} BED (≈ 1/100 of a day's background radiation)`);

// ── tally ───────────────────────────────────────────────────────────────────
console.log(`\n${FAIL === 0 ? "✓ ALL PASS" : "✗ SOME FAILED"} — ${PASS}/${PASS + FAIL}\n`);
process.exit(FAIL === 0 ? 0 : 1);
