#!/usr/bin/env node
// verify.mjs — the computed core for "The Word for Both" (חֶסֶד ḥesed).
// Runs offline from committed artifacts; no network.
//
// What this checks, none of it taken on trust:
//   1. ḥesed (Strong's H2617) occurs 251× in the tagged Masoretic text
//      (OSHB / morphhb, the Leningrad Codex), counted in occurrences.json,
//      which extract.mjs regenerates from the primary tagged XML.
//   2. The homonym split the lexica draw and Strong (1894) did not: OSHB
//      tags "2617 a" = ḥesed I (covenant kindness, 249×) vs "2617 b" =
//      ḥesed II (shame, disgrace, 2×). The SAME consonants חסד.
//   3. THE CENTREPIECE: the word for covenant love and the word for shame
//      are not just the same consonants — in their absolute form they are
//      the same POINTED word, חֶסֶד. Exod 34:6 (God "abounding in ḥesed")
//      and Lev 20:17 (incest, a "ḥesed") read identically.
//   4. Every cell of the KJV "spray" instrument — each English word the King
//      James used for the one Hebrew word — sits on a verse that genuinely
//      carries H2617 (renderings.json), with KJV numbering reconciled to the
//      Hebrew (Psalm superscriptions shift the count).
//
// Data provenance:
//   occurrences.json  — derived by extract.mjs from openscriptures/morphhb (WLC).
//   renderings.json   — the verified KJV spray; KJV text public domain (1611/1769),
//                       Strong's H2617 gloss from openscriptures/strongs (CC-BY-SA).

import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";

const HERE = dirname(fileURLToPath(import.meta.url));
const occ = JSON.parse(readFileSync(join(HERE, "occurrences.json"), "utf-8"));
const ren = JSON.parse(readFileSync(join(HERE, "renderings.json"), "utf-8"));

const checks = [];
function check(name, got, want) {
  const ok = String(got) === String(want);
  checks.push(ok);
  console.log(`  [${ok ? "PASS" : "FAIL"}] ${name}: ${got}${ok ? "" : ` (expected ${want})`}`);
}

const senseI = occ.filter((o) => o.sense === "I");
const senseII = occ.filter((o) => o.sense === "II");
const at = (ref) => occ.find((o) => o.ref === ref);

console.log("=== ḥesed (H2617) in the Masoretic text — OSHB / WLC ===\n");
console.log(`  Total occurrences of the lemma:        ${occ.length}`);
console.log(`  ḥesed I  (kindness / covenant love):   ${senseI.length}`);
console.log(`  ḥesed II (shame, disgrace — homonym):  ${senseII.length}\n`);

console.log("=== ḥesed II — every occurrence (the same חסד, opposite sense) ===");
for (const o of senseII) console.log(`  ${o.ref.padEnd(12)} ${o.pointed}  (${o.lemma})`);
console.log();

console.log("=== checks ===");
check("total H2617 occurrences", occ.length, 251);
check("ḥesed I count", senseI.length, 249);
check("ḥesed II count", senseII.length, 2);
check("I + II == total", senseI.length + senseII.length, occ.length);

// Psalm 136 — the great ḥesed I refrain, 26 verses ("his ḥesed endures forever").
const ps136 = senseI.filter((o) => o.ref.startsWith("Ps.136.")).length;
check("Ps.136 ḥesed I count (the 26-fold refrain)", ps136, 26);

// The two ḥesed II loci every Hebrew grammar cites.
check("Lev.20.17 is ḥesed II", at("Lev.20.17")?.sense, "II");
check("Prov.14.34 is ḥesed II", at("Prov.14.34")?.sense, "II");

// THE CENTREPIECE: identical pointed form, opposite sense.
const loveForm = at("Exod.34.6")?.pointed;   // ḥesed I, "abounding in ḥesed"
const shameForm = at("Lev.20.17")?.pointed;  // ḥesed II, the disgrace
console.log(`\n  Exod.34.6  (I, covenant love): ${loveForm}`);
console.log(`  Lev.20.17  (II, disgrace):     ${shameForm}`);
check("Exod.34.6 (I) and Lev.20.17 (II) are one pointed word",
      loveForm === shameForm ? loveForm : "differ", "חֶסֶד");

// Every spray cell sits on a verse that really carries H2617.
console.log("\n=== KJV spray — each rendering's verse carries H2617 ===");
for (const r of ren.renderings) {
  const o = at(r.hebRef);
  const ok = o && o.sense === r.sense;
  checks.push(!!ok);
  console.log(`  [${ok ? "PASS" : "FAIL"}] "${r.kjv}" — ${r.ref} (Heb ${r.hebRef}, ${o ? o.sense : "MISSING"})`);
}

const pass = checks.filter(Boolean).length;
console.log(`\n${pass === checks.length ? "ALL CHECKS PASS" : "SOME CHECKS FAILED"} — ${pass}/${checks.length}`);
process.exit(pass === checks.length ? 0 : 1);
