"""Orchestrator: builds all seven document-style leadership pages + evidence ledger page,
records reference usage, emits the decision record."""
import json
from pathlib import Path

import site_common
from site_common import CLAIMS, DATA, S, R, G, at, esc, write_page, flush_usage
import pages_brief_docs as pb
import pages_system_docs as ps
import pages_decision_docs as pd

GRADE_TITLE = {"A": "holds regardless of model progress",
               "B": "current-generation measurement",
               "C": "superseded-generation number; mechanism only"}
VERDICT_TXT = {"confirmed": "confirmed", "partially_confirmed": "confirmed with caveats",
               "mismatch": "contradicted", "not_retrievable": "source not retrievable"}

GAPS = [
    ("gap-01", "Praise and completeness verification", "Whether a model can tell warranted praise from empty praise, at usable precision, per praise type: the reviewed corpus identifies no published benchmark. Treated as unmeasured, not as proven absent. Settled by E2."),
    ("gap-02", "Transfer to human writeups", "Every published judge number grades model outputs; this program grades human writeups. No partial-transfer experiment exists. Settled by E3."),
    ("gap-03", "The both-wrong rate", "How often judge and worker agree and are both wrong against expert truth. Nearest proxies only. Settled by E2's planted defects."),
    ("gap-04", "Worker adaptation", "How scores move once a judge is deployed - gaming, drift, monoculture. No published study; the divergence sensor must be built. Settled by E5."),
    ("gap-05", "Feedback efficacy for paid annotators", "Adjacent fields transfer with caveats; the direct experiment is unrun. Settled by E6."),
    ("gap-06", "The best routing signal", "Which uncertainty measure predicts human overturns. Never benchmarked head to head. Settled by E4."),
    ("gap-07", "Our own reviewers' floor", "No measured agreement statistic exists for our population. Settled by E1."),
]


def entry_html(c):
    cid = c["id"]
    du = c.get("decision_use") or {}
    ext = c.get("external_verification") or {}
    inc = c.get("in_corpus_verification")
    src = c.get("source") or {}
    g = du.get("grade", "?")
    v = ext.get("verdict") or "unchecked"
    lb = " - load-bearing" if c.get("load_bearing") else ""
    head = '<p class="entry-head"><b>[%s]</b> %s%s - %s - re-check 2026-07-15: %s</p>' % (
        cid, esc(c.get("domain", "")), lb, esc("grade %s (%s)" % (g, GRADE_TITLE.get(g, ""))),
        esc(VERDICT_TXT.get(v, v)))
    claim = '<p class="claim">%s</p>' % esc(c["claim"])
    srcline = '<p class="meta">Source: <a href="%s" target="_blank" rel="noreferrer">%s</a> (%s, %s)</p>' % (
        esc(src.get("url") or "#"), esc(src.get("title") or ""), esc(src.get("date") or ""), esc(src.get("type") or ""))
    bits = []
    if ext.get("quote"):
        bits.append('<details><summary>What the source itself says (retrieved quote)</summary><blockquote>%s</blockquote></details>' % esc(ext["quote"]))
    kv = []
    if ext.get("checked"):
        kv.append("Checked and matching: " + "; ".join(ext["checked"]))
    if ext.get("not_visible"):
        kv.append("Asserted but not visible in retrievable text: " + "; ".join(ext["not_visible"]))
    if ext.get("notes"):
        kv.append("Re-check notes: " + ext["notes"])
    if kv:
        bits.append('<details><summary>Re-check detail</summary><p class="meta">%s</p></details>' % esc(" || ".join(kv)))
    if inc:
        t = inc.get("transcript") or ""
        corr = ("\nCorrected statement: " + inc["corrected"]) if inc.get("corrected") else ""
        bits.append('<details><summary>Research-sweep audit (2026-07-14): %s</summary><p class="meta">%s</p></details>'
                    % (esc(inc.get("verdict") or ""), esc(t + corr)))
    note = du.get("note")
    models = du.get("models_measured") or []
    cur = 'Decision use: %s. Measured on %s%s.%s' % (
        GRADE_TITLE.get(g, g), esc(du.get("measured_on", "n/a")),
        (" (models: " + esc(", ".join(models)) + ")") if models else "",
        (" " + esc(note)) if note else "")
    bits.append('<p class="meta">%s</p>' % cur)
    if c.get("same_source_claims"):
        sibs = " ".join('<a class="ref" href="#%s">[%s]</a>' % (s, s) for s in c["same_source_claims"])
        bits.append('<p class="meta">Same underlying source as %s - repetition across reports is not independent corroboration.</p>' % sibs)
    uses = site_common.USAGE.get(cid) or c.get("used_on") or []
    if uses:
        links = " - ".join('<a href="%s#%s">%s</a>' % (esc(u["page"]), esc(u["anchor"]), esc(u["label"])) for u in uses[:6])
        bits.append('<p class="meta">Cited at: %s</p>' % links)
    search = esc((c["claim"] + " " + (src.get("title") or "")).lower())
    return ('<div class="entry" id="%s" data-grade="%s" data-verdict="%s" data-used="%d" data-search="%s">%s%s%s%s</div>'
            % (cid, g, v, 1 if uses else 0, search, head, claim, srcline, "".join(bits)))


def build_evidence():
    parts = []
    at("evidence.html", "top", "Evidence - method")
    v = S.get("external_verdicts", {})
    gd = S.get("decision_grades", {})
    parts.append("""
<h1>The evidence ledger</h1>
<p class="doc-dateline">Every claim, its source, what we verified, and how much weight it can bear - 2026-07-15</p>

<p>This is the receipts page. It exists so that no sentence anywhere on this site has to be taken on trust.
There are %d entries: %d findings from the research corpus plus %d anchors admitted later under a stricter bar
(their provenance is disclosed on each). Three things were done to every entry. First, during the original
research sweep, a sample was adversarially audited with full transcripts. Second, this week, every claim was
independently re-checked against the one primary source named for it - fetched fresh, verdicts drawn only from
retrieved text, with a verbatim quote kept and anything unverifiable listed rather than assumed: %d confirmed,
%d confirmed with caveats, %d unreachable, none contradicted. Third, each claim was graded for the question
verification cannot answer: is it still decision-grade under the models we would deploy? Grade A (%d claims)
holds regardless of model progress - law, mathematics, human behavior, documented practice. Grade B (%d) is a
current-generation measurement, treated as a strong prior and confirmed at the bakeoff. Grade C (%d) is a
superseded-generation number: the mechanism informs design; the number sets no threshold.</p>
<p>One fact frames all of it: no published study tests the models we would actually deploy on this exact task.
Every capability-dependent bar gets measured there before it binds anything.</p>
""" % (S.get("n_claims", 0), S.get("n_corpus_claims", 132), S.get("n_da_admitted", 0),
       v.get("confirmed", 0), v.get("partially_confirmed", 0), v.get("not_retrievable", 0),
       gd.get("A", 0), gd.get("B", 0), gd.get("C", 0)))

    parts.append("""
<div class="controls">
<input id="q" type="search" placeholder="Search claims and sources" aria-label="Search the ledger">
<button data-f="grade:all" class="active">All grades</button>
<button data-f="grade:A">A</button><button data-f="grade:B">B</button><button data-f="grade:C">C</button>
<button data-f="verdict:all" class="active">All verdicts</button>
<button data-f="verdict:confirmed">Confirmed</button>
<button data-f="verdict:partially_confirmed">With caveats</button>
<button data-f="verdict:not_retrievable">Unreachable</button>
<button data-f="used:all" class="active">Everything</button>
<button data-f="used:1">Cited on these pages</button>
</div>
<p class="count-line" id="count"></p>
<div id="ledger">
""")
    order = ["durable-anchors", "academic-judges", "annotation-quality", "critique-models",
             "grounding-faithfulness", "hybrid-statistical", "rubrics-recent", "industry-practice",
             "contrarian", "frontier-2026", "critic-and-gapfill"]
    labels = {"durable-anchors": "Anchors admitted after the sweep (provenance disclosed)",
              "academic-judges": "Judge capability studies", "annotation-quality": "Annotation science",
              "critique-models": "Critique and assistance studies", "grounding-faithfulness": "Grounding and verification",
              "hybrid-statistical": "Statistics for sparse truth", "rubrics-recent": "Rubric practice",
              "industry-practice": "Industry practice", "contrarian": "The case against",
              "frontier-2026": "2026 developments", "critic-and-gapfill": "Rater science, law, moderation, feedback"}
    for dom in order:
        items = [c for c in DATA["claims"] if c["domain"] == dom]
        if not items:
            continue
        parts.append('<h2 data-domhead="%s">%s</h2>' % (dom, esc(labels[dom])))
        for c in items:
            parts.append(entry_html(c))
    parts.append("</div>")

    at("evidence.html", "gaps", "Evidence - measured gaps")
    gap_items = "".join('<div class="entry" id="%s"><p class="entry-head"><b>[%s]</b> - unmeasured</p><p class="claim"><b>%s.</b> %s</p></div>'
                        % (gid, gid.upper(), esc(t), esc(d)) for gid, t, d in GAPS)
    cands = S.get("da_candidates_not_admitted") or []
    cand_html = ""
    if cands:
        cand_html = '<p class="meta">Proposed anchors not admitted (source unreachable under our retrieval rules, so they stay quarantined rather than quietly included): %s.</p>' % esc(
            "; ".join("%s (%s)" % (c["title"], c["reason"]) for c in cands))
    parts.append("""
<h2 id="gaps">Where the reviewed corpus finds no evidence</h2>
<p>Absence claims here are bounded to what was actually reviewed - "not identified," never "proven absent."
These seven are not weaknesses of the proposal; they are its measurement plan, and each is assigned to a
specific experiment in <a href="decisions.html#experiments">the decision register</a>.</p>
%s
%s
""" % (gap_items, cand_html))

    at("evidence.html", "integrity", "Evidence - package integrity")
    parts.append("""
<h2 id="integrity">Package integrity</h2>
<p>The foundation package validates against its own contract: all eight checks pass (schemas, cross-document
references, canonical hashes, positive-proof invariants), run on this site's copy on 2026-07-15. One defect in
the originally shipped site was found and fixed: its file manifest recorded stale hashes for three files and
omitted the fonts; the manifest now covers every shipped file. The machine-readable forms of everything here:
<a href="assets/claims.json" download>claims.json</a>, <a href="assets/decision-record.json" download>decision-record.json</a>,
<a href="site-manifest.json" download>site-manifest.json</a>. The reasoning layer is <a href="KERNEL.md">KERNEL.md</a>.</p>

<script>
(function(){
  var items=[].slice.call(document.querySelectorAll('#ledger .entry'));
  var heads=[].slice.call(document.querySelectorAll('[data-domhead]'));
  var state={grade:'all',verdict:'all',used:'all',q:''};
  var q=document.getElementById('q'), count=document.getElementById('count');
  function domOf(el){var p=el.previousElementSibling;while(p&&!(p.dataset&&p.dataset.domhead)){p=p.previousElementSibling;}return p?p.dataset.domhead:'';}
  function apply(){
    var n=0, visByDom={};
    items.forEach(function(el){
      var ok=true;
      if(state.grade!=='all'&&el.dataset.grade!==state.grade)ok=false;
      if(state.verdict!=='all'&&el.dataset.verdict!==state.verdict)ok=false;
      if(state.used!=='all'&&el.dataset.used!=='1')ok=false;
      if(state.q&&el.dataset.search.indexOf(state.q)===-1)ok=false;
      el.hidden=!ok;
      if(ok){n++; visByDom[domOf(el)]=true;}
    });
    heads.forEach(function(h){ h.hidden=!visByDom[h.dataset.domhead]; });
    count.textContent=n+' of '+items.length+' entries shown';
  }
  document.querySelectorAll('[data-f]').forEach(function(b){b.addEventListener('click',function(){
    var kv=b.dataset.f.split(':');
    document.querySelectorAll('[data-f^="'+kv[0]+':"]').forEach(function(x){x.classList.remove('active');});
    b.classList.add('active'); state[kv[0]]=kv[1]; apply();});});
  if(q)q.addEventListener('input',function(){state.q=q.value.toLowerCase().trim();apply();});
  apply();
  if(location.hash){var t=document.querySelector(location.hash);if(t){var d=t.querySelector('details');if(d)d.open=true;}}
})();
</script>
""")
    write_page("evidence.html", "Evidence", "The evidence ledger: every claim, its source, its verification, and its decision-use grade.", "evidence", "".join(parts))


def main():
    pb.build_index()
    pb.build_brief()
    pb.build_research()
    ps.build_architecture()
    ps.build_pilot()
    pd.build_decisions()
    build_evidence()
    pd.emit_decision_record()
    flush_usage()


if __name__ == "__main__":
    main()
