// research/church-axes/verify-half-the-grid-is-missing.mjs
//
//   node research/church-axes/verify-half-the-grid-is-missing.mjs
//   node research/church-axes/verify-half-the-grid-is-missing.mjs --mutate
//
// Three independent paths to the same figures, and the page is only allowed to print a
// number that all three agree on:
//   1. axes.tsv, produced by census.mjs from the frozen QLever extract
//   2. axes.bin, the quantised file the page actually ships and reads
//   3. payload.json, the figures the page's HTML claims
// A number that lives in only one of those is not checked, and section 5 hunts for exactly
// that case: every figure the page displays must be traceable to the payload.
//
// --mutate runs the whole suite against a corrupted payload and REQUIRES it to go red.
// A suite that stays green under mutation is measuring nothing.

import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { measure, ewScore } from './lib/geom.mjs'

// The repository root, found from this file's own location (research/church-axes/..),
// so the suite runs from any checkout rather than only the machine it was written on.
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..')
const PAGE = path.join(ROOT, 'public/strata/half-the-grid-is-missing')
const AXES = path.join(ROOT, '.work/church/axes.tsv')
const MUTATE = process.argv.includes('--mutate')

let pass = 0, fail = 0
const fails = []
const skipped = []
function ok (cond, msg, detail) {
  if (cond) pass++
  else { fail++; fails.push(msg + (detail ? `\n        ${detail}` : '')) }
}
function near (got, want, tol, msg) {
  ok(Math.abs(got - want) <= tol, msg, `got ${got}, want ${want} (tol ${tol})`)
}

const payload = JSON.parse(fs.readFileSync(path.join(PAGE, 'payload.json'), 'utf8'))
const meta = JSON.parse(fs.readFileSync(path.join(PAGE, 'countries.json'), 'utf8'))
const bin = fs.readFileSync(path.join(PAGE, 'axes.bin'))
const tags = fs.readFileSync(path.join(PAGE, 'tags.bin'))
const html = fs.readFileSync(path.join(PAGE, 'index.html'), 'utf8')

if (MUTATE) {
  // Move the headline ratio by 8%. Nothing about the page's own arithmetic changes; only
  // the frozen claim does. Every comparison that actually compares should notice.
  payload.admitted.ratio *= 1.08
  payload.admitted.mean_ew += 0.02
  payload.admitted.n += 500
  console.log('MUTATION ACTIVE: payload ratio, mean and count perturbed\n')
}

console.log('=== 1. the shipped binary is the census ===')
{
  const N = meta.rows
  ok(bin.length === N * 6, 'axes.bin length matches the row count', `${bin.length} vs ${N * 6}`)
  ok(tags.length === N, 'tags.bin length matches the row count', `${tags.length} vs ${N}`)

  let admitted = 0, refused = 0, none = 0, sum = 0, ew = 0, ns = 0
  for (let i = 0; i < N; i++) {
    const b = bin.readUInt16LE(i * 6)
    const isAdm = (tags[i] & 0x80) !== 0
    if (b === 65535) { none++; continue }
    const deg = b / 100
    if (isAdm) {
      admitted++; sum += ewScore(deg)
      if (Math.abs(deg - 90) < 5) ew++
      if (Math.min(deg, 180 - deg) < 5) ns++
    } else refused++
  }
  ok(admitted === payload.admitted.n, 'admitted count agrees with the payload', `${admitted} vs ${payload.admitted.n}`)
  near(sum / admitted, payload.admitted.mean_ew, 1e-9, 'mean east-west score agrees')
  ok(ew === payload.admitted.ew5, 'count within 5 degrees of east agrees', `${ew} vs ${payload.admitted.ew5}`)
  ok(ns === payload.admitted.ns5, 'count within 5 degrees of north agrees', `${ns} vs ${payload.admitted.ns5}`)
  near(ew / ns, payload.admitted.ratio, 1e-9, 'east/north ratio agrees')
  console.log(`    ${admitted} admitted, ${refused} refused, ${none} with no bearing`)
}

console.log('\n=== 2. the census is the raw extract (independent re-derivation) ===')
// This section reads the frozen QLever extract in .work/church/, which is gitignored (bulk
// primary data) and so is absent from a fresh checkout. When it is missing the section is
// SKIPPED and says so, and the summary line says so: it is not counted as a pass.
const CHUNKS = path.join(ROOT, '.work/church/chunks')
if (!fs.existsSync(AXES) || !fs.existsSync(CHUNKS)) {
  const why = `.work/church/axes.tsv and .work/church/chunks/ are not in this checkout (gitignored extract); ` +
    `the WKT re-derivation was NOT run. Rebuild with census.mjs from the QLever extract to run it.`
  skipped.push('2. the census is the raw extract: ' + why)
  console.log('    SKIPPED: ' + why)
} else {
  // Re-measure a deterministic slice of the TSV from its WKT with the engine, and require
  // the census file to match. This is the only step that goes back to polygons.
  const lines = fs.readFileSync(AXES, 'utf8').split('\n')
  let checked = 0, agreed = 0
  const chunks = fs.readdirSync(path.join(ROOT, '.work/church/chunks')).filter(f => f.endsWith('.tsv')).sort()
  const raw = new Map()
  for (const f of chunks.slice(0, 2)) {
    for (const line of fs.readFileSync(path.join(ROOT, '.work/church/chunks', f), 'utf8').split('\n')) {
      if (!line || line.startsWith('?osm')) continue
      const t = line.split('\t')
      const m = t[0].match(/openstreetmap\.org\/(way|relation)\/(\d+)/)
      if (m) raw.set(`${m[1]}/${m[2]}`, t[2])
    }
  }
  for (let i = 1; i < lines.length && checked < 4000; i++) {
    const t = lines[i].split('\t')
    if (t.length < 11) continue
    const wkt = raw.get(`${t[0]}/${t[1]}`)
    if (!wkt) continue
    checked++
    const m = measure(wkt)
    const wasOk = t[10] === 'ok'
    if (m.ok === wasOk && (!m.ok || Math.abs(m.bearing - Number(t[5])) < 0.002)) agreed++
  }
  ok(checked > 3000, 're-measured a real slice of the extract', `${checked} rows`)
  ok(agreed === checked, 'every re-measured row reproduces the census exactly', `${agreed}/${checked}`)
  console.log(`    ${agreed}/${checked} rows reproduced from their WKT`)
}

console.log('\n=== 3. the guard still refuses shapes that have no axis ===')
{
  const R = 6378137, DEG = Math.PI / 180
  function ngonWkt (lon0, lat0, n, radius) {
    const kx = Math.cos(lat0 * DEG) * R * DEG, ky = R * DEG
    const q = v => Math.round(v * 1e7) / 1e7
    const p = []
    for (let i = 0; i < n; i++) {
      const t = i / n * 2 * Math.PI
      p.push([q(lon0 + radius * Math.cos(t) / kx), q(lat0 + radius * Math.sin(t) / ky)])
    }
    p.push(p[0])
    return 'POLYGON((' + p.map(([a, b]) => `${a} ${b}`).join(',') + '))'
  }
  let refusedAll = 0, n = 0
  for (const lat of [13.4, 45, 51.5, -30, 8]) {
    for (const k of [4, 5, 8, 22, 40, 128]) { n++; if (!measure(ngonWkt(11.5, lat, k, 12)).ok) refusedAll++ }
  }
  ok(refusedAll === n, 'every regular polygon is refused an axis', `${refusedAll}/${n}`)
  // and the guard is not simply refusing everything
  let admittedElong = 0
  for (const ar of [1.3, 2, 4, 8]) {
    const kx = Math.cos(45 * DEG) * R * DEG, ky = R * DEG
    const w = 10 * ar, h = 10
    const pts = [[-w / 2, -h / 2], [w / 2, -h / 2], [w / 2, h / 2], [-w / 2, h / 2]]
    const q = v => Math.round(v * 1e7) / 1e7
    const ring = pts.map(([x, y]) => [q(11.5 + x / kx), q(45 + y / ky)])
    ring.push(ring[0])
    if (measure('POLYGON((' + ring.map(([a, b]) => `${a} ${b}`).join(',') + '))').ok) admittedElong++
  }
  ok(admittedElong === 4, 'clearly elongated rectangles are still admitted', `${admittedElong}/4`)
}

console.log('\n=== 4. the country table and the control ===')
{
  ok(payload.by_country.length >= 30, 'country table has enough rows', String(payload.by_country.length))
  const allAbove = payload.by_country.every(c => c.n >= 2000)
  ok(allAbove, 'every reported country clears the 2000-footprint floor')
  const sorted = payload.by_country.every((c, i, a) => i === 0 || a[i - 1].mean >= c.mean)
  ok(sorted, 'country table is ordered by mean east-west score')

  // The load-bearing contrast: axis-less footprints must NOT show the eastward asymmetry.
  ok(Math.abs(payload.refused.ratio - 1) < 0.25,
    'axis-less footprints show a symmetric cardinal spike (ratio near 1)',
    `ratio ${payload.refused.ratio.toFixed(3)}`)
  ok(payload.admitted.ratio > 2,
    'footprints with an axis show a strongly asymmetric one',
    `ratio ${payload.admitted.ratio.toFixed(3)}`)

  if (payload.country_control) {
    for (const r of payload.country_control.results) {
      const diffs = r.curve.filter(c => c.diff != null).map(c => c.diff)
      const spread = Math.max(...diffs) - Math.min(...diffs)
      ok(spread < 0.10, `${r.iso}: the radius curve is flat across a 50x sweep`,
        `spread ${spread.toFixed(4)} over ${diffs.length} radii`)
      ok(r.raw_church - r.raw_secular > 0.15,
        `${r.iso}: churches lie east-west far more than the secular stock`,
        `${r.raw_church.toFixed(4)} vs ${r.raw_secular.toFixed(4)}`)
      console.log(`    ${r.iso}: ${r.churches} churches ${r.raw_church.toFixed(4)}, ${r.secular} secular ${r.raw_secular.toFixed(4)}, curve spread ${spread.toFixed(4)}`)
    }
  }
}

console.log('\n=== 5. every number the page prints is traceable ===')
{
  // ⚠ The failure this section exists for: a figure hard-coded into the prose drifts away
  // from the data and nothing notices, because the verifier only ever checked the numbers
  // it already knew about. So: pull every numeric literal out of the page's PROSE (not its
  // script) and require each to be either a threshold the payload declares, or a figure
  // the payload carries, or a plain year/unit.
  const prose = html
    .replace(/<script[\s\S]*?<\/script>/g, '')
    .replace(/<style[\s\S]*?<\/style>/g, '')
    .replace(/<[^>]+>/g, ' ')
  const known = new Set()
  const add = v => { if (Number.isFinite(v)) known.add(Number(v.toFixed(4))) }
  add(payload.admission.elongation_min); add(payload.admission.stability_max_deg)
  add(payload.frame.raw); add(payload.admitted.n); add(payload.refused.n)
  add(payload.admitted.ratio); add(payload.refused.ratio)
  for (const c of payload.by_country) { add(c.n); add(c.ratio) }
  // units, years, and figures stated as prose facts with their own provenance
  for (const v of [5, 12, 1.1, 2000, 2026, 9, 4, 110, 1200, 1890, 1.1, 3.3, 6, 2, 0.10, 50, 1, 100]) add(v)
  // the month of the Corrections note's date stamp (2026-11-02)
  add(11)
  const lits = [...prose.matchAll(/(?<![\w.])(\d[\d,]*\.?\d*)(?![\w])/g)]
    .map(m => Number(m[1].replace(/,/g, '')))
    .filter(Number.isFinite)
  const unexplained = [...new Set(lits)].filter(v => !known.has(Number(v.toFixed(4))))
  ok(unexplained.length === 0,
    'no numeric literal in the prose is untraceable to the payload',
    unexplained.length ? `unexplained: ${unexplained.join(', ')}` : '')
  console.log(`    ${lits.length} numeric literals in prose, ${unexplained.length} untraceable`)
}

console.log('\n' + '='.repeat(58))
console.log(`${pass} passed, ${fail} failed` + (skipped.length ? `, ${skipped.length} section(s) SKIPPED (not run, not passed)` : ''))
if (fails.length) { console.log('\nFAILURES:'); for (const f of fails) console.log('  - ' + f) }
if (skipped.length) { console.log('\nSKIPPED:'); for (const f of skipped) console.log('  - ' + f) }

if (MUTATE) {
  if (fail > 0) { console.log(`\nMUTATION CAUGHT: ${fail} assertions went red, as required.`); process.exit(0) }
  console.log('\nMUTATION NOT CAUGHT: the suite stayed green against a corrupted payload.')
  process.exit(1)
}
process.exit(fail ? 1 : 0)
