#!/usr/bin/env node
// verify-scan.mjs — the scan index is a summary, and a summary can lie two ways.
//
//   node research/au-coins/verify-scan.mjs
//
// A cheap index is only worth having if it agrees with the expensive thing it summarises. The
// two failures that matter are opposite: it can go STALE, still describing coins as they were
// several builds ago, or it can be WRONG NOW, mis-ranking or mis-flagging what it just read.
// Both look identical from outside, which is a small green file either way.
//
// So every assertion here is a cross-check against api/catalogue.json, never against scan.mjs's
// own idea of what it wrote. Checking a file against the code that produced it proves only that
// the code ran twice.

import { readFile, readdir } from 'node:fs/promises'

const OUT = 'public/tools/australian-coins'
const cat = JSON.parse(await readFile(`${OUT}/api/catalogue.json`, 'utf8'))
const app = JSON.parse(await readFile(`${OUT}/api/app.json`, 'utf8'))
const scan = JSON.parse(await readFile(`${OUT}/api/scan.json`, 'utf8'))

let passed = 0, failed = 0, controls = 0, fired = 0
const ok = (m) => { passed++; console.log(`  ok   ${m}`) }
const bad = (m, d = '') => { failed++; console.log(`  FAIL ${m}${d ? `\n       ${d}` : ''}`) }
const control = async (m, f) => { controls++; (await f()) ? (fired++, console.log(`  red  ${m}`)) : (failed++, console.log(`  CONTROL DID NOT FIRE: ${m}`)) }

const C = scan.legend.cols
const ix = Object.fromEntries(C.map((c, n) => [c, n]))
const rows = Object.entries(scan.denominations).flatMap(([den, rs]) => rs.map((r) => ({ den, r })))
const fullId = (den, r) => `au-${den}-${r[ix.year]}${r[ix.id] ? `-${r[ix.id]}` : ''}`

// ── 1. every issue is present, exactly once, and identifiable ──────────────────────────
{
  const want = new Set(app.issues.map((i) => i.id))
  const got = rows.map(({ den, r }) => fullId(den, r))
  const gotSet = new Set(got)
  const missing = [...want].filter((id) => !gotSet.has(id))
  const extra = got.filter((id) => !want.has(id))
  missing.length || extra.length
    ? bad(`every one of the ${want.size} issues appears in the scan`,
          `${missing.length} missing (${missing.slice(0, 3).join(', ')}), ${extra.length} unknown`)
    : ok(`every one of the ${want.size} issues appears in the scan, and none that is not an issue`)
  got.length === gotSet.size
    ? ok(`all ${got.length} scan keys are unique, so a row identifies a coin`)
    : bad('all scan keys are unique', `${got.length - gotSet.size} collisions`)
}

// ── 2. the mintage in the cheap file is the mintage in the expensive one ───────────────
{
  const byId = new Map(app.issues.map((i) => [i.id, i]))
  const wrong = rows.filter(({ den, r }) => {
    const i = byId.get(fullId(den, r))
    const m = r[ix.mintage]
    return !i || (typeof i.mintage === 'number' ? m !== i.mintage : m !== null)
  })
  wrong.length
    ? bad('every mintage in the scan equals the catalogue', `${wrong.length} differ, first ${fullId(wrong[0].den, wrong[0].r)}`)
    : ok(`every one of the ${rows.length} mintages equals the catalogue's`)
}

// ── 3. the rank means what the legend says it means ────────────────────────────────────
{
  let problems = []
  for (const [den, rs] of Object.entries(scan.denominations)) {
    const ranked = rs.filter((r) => r[ix.rank] != null)
      .sort((a, b) => a[ix.rank] - b[ix.rank])
    for (let n = 0; n < ranked.length; n++) {
      if (ranked[n][ix.rank] !== n + 1) { problems.push(`${den} rank ${ranked[n][ix.rank]} at position ${n + 1}`); break }
      if (n && ranked[n][ix.mintage] < ranked[n - 1][ix.mintage]) { problems.push(`${den} rank ${n + 1} has a lower mintage than rank ${n}`); break }
    }
    // The trap this file exists to avoid: an evidenced zero ranked first would send a reader
    // hunting a coin that no document says exists.
    const zeroRanked = rs.filter((r) => r[ix.mintage] === 0 && r[ix.rank] != null)
    if (zeroRanked.length) problems.push(`${den} ranks ${zeroRanked.length} issue(s) whose mintage is zero`)
  }
  problems.length ? bad('rank is dense, ascending, and never given to an evidenced zero', problems.slice(0, 3).join('; '))
                  : ok('rank is dense, ascending by mintage, and never given to an evidenced zero')
}

// ── 4. the flags say what is true ──────────────────────────────────────────────────────
{
  const byId = new Map(app.issues.map((i) => [i.id, i]))
  const keyDates = new Set((cat.varieties ?? [])
    .filter((v) => /key date|key die/i.test(`${v.name} ${v.identified_by ?? ''}`)).map((v) => v.issue).filter(Boolean))
  const bad_ = []
  for (const { den, r } of rows) {
    const i = byId.get(fullId(den, r)); if (!i) continue
    const f = r[ix.flags] ?? ''
    if (!!i.varieties !== f.includes('v')) bad_.push(`${i.id} v`)
    if (keyDates.has(i.id) !== f.includes('k')) bad_.push(`${i.id} k`)
    if ((i.mintage === 0) !== f.includes('z')) bad_.push(`${i.id} z`)
    if ((typeof i.mintage !== 'number' && !i.bound) !== f.includes('n')) bad_.push(`${i.id} n`)
  }
  bad_.length ? bad('every flag matches the catalogue', `${bad_.length} wrong, first ${bad_[0]}`)
              : ok('every v, k, z and n flag matches the catalogue')
}

// The two predicates below are named rather than inlined for one reason: their negative
// controls call THEM. A control that re-implements the test it is checking proves that the
// control's copy works, which is not a fact anybody needs.
const selfDescribing = (slice) => !!(slice?.legend?.cols && slice?.legend?.flags && slice?.legend?.basis)
const cheapEnough = (fullBytes, sliceBytes) => fullBytes / sliceBytes >= 50

// ── 5. it is actually cheap, which is the only reason it exists ────────────────────────
{
  const appSize = (await readFile(`${OUT}/api/app.json`, 'utf8')).length
  const slice = (await readFile(`${OUT}/api/d/two-dollars.json`, 'utf8')).length
  const factor = appSize / slice
  cheapEnough(appSize, slice)
    ? ok(`a one-denomination question costs ${factor.toFixed(0)}x less than app.json (${Math.round(slice / 4)} tokens vs ${Math.round(appSize / 4)})`)
    : bad('a one-denomination question is at least 50x cheaper than app.json', `only ${factor.toFixed(1)}x`)
  // Self-describing: a slice a reader cannot interpret without a second fetch is not cheap.
  const d = JSON.parse(await readFile(`${OUT}/api/d/penny.json`, 'utf8'))
  selfDescribing(d)
    ? ok('each slice repeats its own legend, so one fetch is enough to read it')
    : bad('each slice repeats its own legend')
}

// ── 6. every denomination has a slice, and llms.txt names the cheap path ───────────────
{
  const files = (await readdir(`${OUT}/api/d`)).filter((f) => f.endsWith('.json')).map((f) => f.replace(/\.json$/, ''))
  const dens = Object.keys(scan.denominations)
  const missing = dens.filter((d) => !files.includes(d))
  missing.length ? bad('every denomination has a slice', missing.join(', ')) : ok(`all ${dens.length} denominations have a slice`)

  const llms = await readFile(`${OUT}/llms.txt`, 'utf8')
  const head = llms.slice(0, llms.indexOf('## The one thing'))
  head.includes('api/d/<denomination>.json') && head.includes('api/scan.json')
    ? ok('llms.txt names the cheap path before it names the expensive one')
    : bad('llms.txt names the cheap path first', 'an agent that reads top-down must meet the slices before catalogue.json')
  // The token figures in llms.txt are claims about THIS build, so they must still be true of
  // it. A saving quoted in a file that nobody re-derives is the shape of claim that rots first.
  const appTok = Math.round((await readFile(`${OUT}/api/app.json`, 'utf8')).length / 4)
  const sliceTok = Math.round((await readFile(`${OUT}/api/d/two-dollars.json`, 'utf8')).length / 4)
  const quoted = /costs about ([\d,]+) tokens[\s\S]{0,80}?costs about ([\d,]+)/.exec(head)
  if (!quoted) {
    bad('llms.txt quotes both token figures', 'the measured sentence is gone')
  } else {
    const [qApp, qSlice] = [quoted[1], quoted[2]].map((s) => Number(s.replace(/,/g, '')))
    const near = (a, b) => Math.abs(a - b) / b < 0.15
    near(qApp, appTok) && near(qSlice, sliceTok)
      ? ok(`llms.txt's quoted savings still match the files (${qApp} vs ${appTok}, ${qSlice} vs ${sliceTok})`)
      : bad("llms.txt's quoted savings still match the files",
            `it says ${qApp} and ${qSlice}; the files are ~${appTok} and ~${sliceTok}`)
  }
}

// ── the controls ───────────────────────────────────────────────────────────────────────
console.log('')
await control('a mintage that disagrees with the catalogue is caught', () => {
  const byId = new Map(app.issues.map((i) => [i.id, i]))
  const { den, r } = rows.find(({ den, r }) => typeof r[ix.mintage] === 'number')
  const faked = [...r]; faked[ix.mintage] += 1
  const i = byId.get(fullId(den, faked))
  return i && faked[ix.mintage] !== i.mintage
})
await control('a duplicate key is caught', () => {
  const got = rows.map(({ den, r }) => fullId(den, r))
  return new Set([...got, got[0]]).size !== got.length + 1
})
await control('a zero given a rank is caught', () => {
  const zeros = rows.filter(({ r }) => r[ix.mintage] === 0)
  if (!zeros.length) return false          // nothing to protect against, so the check is idle
  return zeros.every(({ r }) => r[ix.rank] == null)   // and today they are all correctly unranked
})
await control('a slice that dropped its legend is caught', async () => {
  // The real slice, with the real predicate, minus the legend a real bug would drop.
  const real = JSON.parse(await readFile(`${OUT}/api/d/penny.json`, 'utf8'))
  if (!selfDescribing(real)) return false           // the check must pass on the real file first
  const { legend, ...withoutLegend } = real
  return !selfDescribing(withoutLegend)
})
await control('an index that is not cheap is caught', async () => {
  const appSize = (await readFile(`${OUT}/api/app.json`, 'utf8')).length
  const slice = (await readFile(`${OUT}/api/d/two-dollars.json`, 'utf8')).length
  if (!cheapEnough(appSize, slice)) return false     // must be true of the real files first
  // A slice that saved nothing: the same file served whole under a slice's name.
  return !cheapEnough(appSize, appSize)
})

console.log(`\n${passed} passed, ${failed} failed`)
console.log(`${fired}/${controls} negative controls fired`)
process.exit(failed === 0 && fired === controls ? 0 : 1)
