"""Build chart specs for the F.2 blind adjudication (adj) and the F4 scale-up (f4).

Every plotted number is read from a results file at build time (never typed in by hand), and each
spec carries a `sources` list naming the file and field for every number. Writes only to
out/exp/charts/specs/. Read-only on everything else.

Run: python build_specs.py   (from any directory)
"""
import json
import math
import os
from collections import Counter

HERE = os.path.dirname(os.path.abspath(__file__))
EXP = os.path.dirname(HERE)
Z = 1.959964


def load(rel):
    with open(os.path.join(EXP, rel), encoding="utf-8") as f:
        return json.load(f)


def jsonl(rel):
    with open(os.path.join(EXP, rel), encoding="utf-8") as f:
        # split on "\n" only: page text contains U+2028 (f4/REPORT-build.md section 5)
        return [json.loads(line) for line in f.read().split("\n") if line.strip()]


def wilson(k, n):
    """Wilson 95% interval in percent, same z as adj/score.py."""
    if n == 0:
        return (0.0, 0.0, 100.0)
    p = k / n
    den = 1 + Z * Z / n
    centre = (p + Z * Z / (2 * n)) / den
    half = Z * math.sqrt(p * (1 - p) / n + Z * Z / (4 * n * n)) / den
    return (round(100 * p, 1), round(100 * max(0.0, centre - half), 1), round(100 * min(1.0, centre + half), 1))


def r1(x):
    return round(x, 1)


# --------------------------------------------------------------------------------------------- F.2
A = load("adj/results.json")
curve = A["jev_router_curve"]
ts = [pt["t"] for pt in curve["primary_armB"]]
assert ts == [pt["t"] for pt in curve["primary_armA"]]
missA = [pt["miss"] for pt in curve["primary_armA"]]
missB = [pt["miss"] for pt in curve["primary_armB"]]
# the two arms' miss curves are plotted as one series only because they are identical
assert [(m["k"], m["n"]) for m in missA] == [(m["k"], m["n"]) for m in missB], "arm A and B miss curves differ"
uB = [pt["useful_accept"] for pt in curve["primary_armB"]]
uA = [pt["useful_accept"] for pt in curve["primary_armA"]]
i08 = ts.index(0.8)
n_fail = A["fail_share"]["k"]
n_all = A["n_labelled"]
n_good = uB[i08]["n"]

adj = []
adj.append({
    "title": (f"Jev auto-accepted none of the {n_fail} live fails at any threshold, and with a saved-page window "
              f"it cleared {uB[i08]['k']} of {n_good} good claims at the current 0.8 rule"),
    "type": "line",
    "x_title": "Jev confidence threshold for accepting a 'none' finding (0 to 1)",
    "y_title": "Claims auto-accepted without review (% of the claim group)",
    "x": ts,
    "series": [
        {"name": f"Good claims accepted, saved-page window (arm B, of {n_good})",
         "values": [m["p"] for m in uB], "ci_low": [m["lo"] for m in uB], "ci_high": [m["hi"] for m in uB],
         "counts": [f"{m['k']}/{m['n']}" for m in uB], "dash": "solid", "marker": "circle"},
        {"name": f"Good claims accepted, draft's own quote (arm A, of {n_good})",
         "values": [m["p"] for m in uA], "ci_low": [m["lo"] for m in uA], "ci_high": [m["hi"] for m in uA],
         "counts": [f"{m['k']}/{m['n']}" for m in uA], "dash": "dashed", "marker": "square"},
        {"name": f"Live fails accepted, either arm (of {n_fail}; the two arms are identical)",
         "values": [m["p"] for m in missB], "ci_low": [m["lo"] for m in missB], "ci_high": [m["hi"] for m in missB],
         "counts": [f"{m['k']}/{m['n']}" for m in missB], "dash": "dotted", "marker": "triangle"},
    ],
    "annotations": ["Current rule is threshold 0.8 on both repeats",
                    "Points are nudged sideways a little so the error bars do not overlap; the x values are the thresholds shown"],
    "error_bars": "Wilson 95%",
    "n_claims": n_all,
    "n_recorded_fails": n_fail,
    "split": "live: F.2 blind Opus review of WS1's 5 fresh dossiers (not in the claims dataset)",
    "direction": "lower is safer for live fails accepted; higher is better for good claims accepted",
    "sources": [
        "adj/results.json -> jev_router_curve.primary_armB[*].useful_accept {p, lo, hi, k, n} (series 1)",
        "adj/results.json -> jev_router_curve.primary_armA[*].useful_accept {p, lo, hi, k, n} (series 2)",
        "adj/results.json -> jev_router_curve.primary_armB[*].miss {p, lo, hi, k, n}; asserted equal to primary_armA[*].miss (series 3)",
        "adj/results.json -> jev_router_curve.primary_armB[*].t (x)",
        "adj/results.json -> n_labelled (n claims), fail_share.k (n fails)",
    ],
})

g = A["production_gates"]
cv = A["jev_checker_view"]
adj.append({
    "title": (f"Today's two publish gates caught none of the {n_fail} live fails; Jev used as a checker caught "
              f"{cv['B']['fails_caught']['k']} to {cv['A']['fails_caught']['k']} of them but also flagged "
              f"{round(cv['B']['passes_flagged']['p'])}% to {round(cv['A']['passes_flagged']['p'])}% of good claims"),
    "type": "grouped_bar",
    "x_title": "Check",
    "y_title": "Claims flagged by the check (% of the claim group)",
    "x": ["ground.py (production gate)", "Step 03, Mistral on the whole draft (production gate)",
          "Jev as checker, draft's own quote (arm A)", "Jev as checker, saved-page window (arm B)"],
    "series": [
        {"name": f"Live fails caught (of {n_fail})",
         "values": [g["ground_py_caught"]["p"], g["step03_caught"]["p"], cv["A"]["fails_caught"]["p"], cv["B"]["fails_caught"]["p"]],
         "ci_low": [g["ground_py_caught"]["lo"], g["step03_caught"]["lo"], cv["A"]["fails_caught"]["lo"], cv["B"]["fails_caught"]["lo"]],
         "ci_high": [g["ground_py_caught"]["hi"], g["step03_caught"]["hi"], cv["A"]["fails_caught"]["hi"], cv["B"]["fails_caught"]["hi"]],
         "counts": [f"{d['k']}/{d['n']}" for d in (g["ground_py_caught"], g["step03_caught"], cv["A"]["fails_caught"], cv["B"]["fails_caught"])],
         "pattern": "solid"},
        {"name": f"Good claims flagged (of {n_good}; each one is review work)",
         "values": [None, None, cv["A"]["passes_flagged"]["p"], cv["B"]["passes_flagged"]["p"]],
         "ci_low": [None, None, cv["A"]["passes_flagged"]["lo"], cv["B"]["passes_flagged"]["lo"]],
         "ci_high": [None, None, cv["A"]["passes_flagged"]["hi"], cv["B"]["passes_flagged"]["hi"]],
         "counts": ["not scored", "not scored"] + [f"{d['k']}/{d['n']}" for d in (cv["A"]["passes_flagged"], cv["B"]["passes_flagged"])],
         "pattern": "hatched"},
    ],
    "annotations": ["'not scored': adj/results.json scores the production gates on the 9 fails only",
                    "Jev checker view: the claim counts as flagged if either repeat reports an issue"],
    "error_bars": "Wilson 95%",
    "n_claims": n_all,
    "n_recorded_fails": n_fail,
    "split": "live: F.2 blind Opus review of WS1's 5 fresh dossiers",
    "direction": "higher is better for live fails caught; lower is better for good claims flagged",
    "sources": [
        "adj/results.json -> production_gates.ground_py_caught / step03_caught {p, lo, hi, k, n}",
        "adj/results.json -> jev_checker_view.A|B.fails_caught {p, lo, hi, k, n}",
        "adj/results.json -> jev_checker_view.A|B.passes_flagged {p, lo, hi, k, n}",
    ],
})

sec = A["fail_share_by_section"]
order = sorted(sec, key=lambda s: -sec[s]["n"])
adj.append({
    "title": (f"Live fails turned up in four of five dossier sections, but with {n_fail} fails in all "
              "the section rates overlap and cannot be ranked"),
    "type": "bar",
    "x_title": "Dossier section (claims in scope)",
    "y_title": "Claims the blind review failed (% of the section's claims)",
    "x": [f"{s} ({sec[s]['n']})" for s in order],
    "series": [
        {"name": "Fail share (primary verdict)", "values": [sec[s]["p"] for s in order],
         "ci_low": [sec[s]["lo"] for s in order], "ci_high": [sec[s]["hi"] for s in order],
         "counts": [f"{sec[s]['k']}/{sec[s]['n']}" for s in order], "pattern": "solid"},
    ],
    "annotations": ["Descriptive only: the review report says rates by section are shown for description, not comparison"],
    "error_bars": "Wilson 95%",
    "n_claims": n_all,
    "n_recorded_fails": n_fail,
    "split": "live: F.2 blind Opus review of WS1's 5 fresh dossiers",
    "direction": "lower is safer",
    "sources": ["adj/results.json -> fail_share_by_section.<section> {k, n, p, lo, hi}"],
})

# --------------------------------------------------------------------------------------------- F4
F = load("f4/adj/results.json")
acc = F["primary_bad_share_armB_accepts"]
acs = F["strict_bad_share_armB_accepts"]
smp = F["sample_fail_share"]
sms = F["sample_strict_share"]


def pct(d):
    return [r1(100 * d["share"]), r1(100 * d["wilson95"][0]), r1(100 * d["wilson95"][1])]


f4 = []
pa, ps, pas, pss = pct(acc), pct(smp), pct(acs), pct(sms)
f4.append({
    "title": (f"None of the {acc['n']} claims Jev auto-accepted was bad (upper bound {pa[2]}%), against "
              f"{smp['k']} of {smp['n']} sampled claims it left for review ({ps[0]}%)"),
    "type": "grouped_bar",
    "x_title": "Claim group reviewed blind",
    "y_title": "Claims the blind review marked (% of the group)",
    "x": [f"Claims Jev auto-accepted (all {acc['n']})",
          f"Claims Jev left for review (random {smp['n']} of {F['n_rest']})"],
    "series": [
        {"name": "Bad: fail verdict (primary view)", "values": [pa[0], ps[0]], "ci_low": [pa[1], ps[1]],
         "ci_high": [pa[2], ps[2]], "counts": [f"{acc['k']}/{acc['n']}", f"{smp['k']}/{smp['n']}"], "pattern": "solid"},
        {"name": "Any flag, minor ones included (strict view)", "values": [pas[0], pss[0]], "ci_low": [pas[1], pss[1]],
         "ci_high": [pas[2], pss[2]], "counts": [f"{acs['k']}/{acs['n']}", f"{sms['k']}/{sms['n']}"], "pattern": "hatched"},
    ],
    "annotations": ["Intervals treat claims as independent; claims cluster by vendor, so the true uncertainty is somewhat wider"],
    "error_bars": "Wilson 95%",
    "n_claims": F["n_packet"],
    "n_claims_note": f"{F['n_packet']} claims reviewed blind, out of {F['n_in_scope']} in Jev's scope",
    "n_recorded_fails": acc["k"] + smp["k"],
    "split": "live: F4 blind Opus review of 22 new dossiers (no WS1 vendor)",
    "direction": "lower is safer",
    "sources": [
        "f4/adj/results.json -> primary_bad_share_armB_accepts {k, n, share, wilson95}",
        "f4/adj/results.json -> sample_fail_share {k, n, share, wilson95}",
        "f4/adj/results.json -> strict_bad_share_armB_accepts, sample_strict_share {k, n, share, wilson95}",
        "f4/adj/results.json -> n_packet, n_in_scope, n_rest",
    ],
})

# pooled accepts: F.2 accepts at 0.8 on arm B = good claims accepted + fails accepted
f2_bad = missB[i08]["k"]
f2_acc = uB[i08]["k"] + missB[i08]["k"]
pool_k, pool_n = f2_bad + acc["k"], f2_acc + acc["n"]
w_f2, w_f4, w_pool = wilson(f2_bad, f2_acc), (pa[0], pa[1], pa[2]), wilson(pool_k, pool_n)
f4.append({
    "title": (f"Pooled over both blind reviews, {pool_k} of {pool_n} Jev auto-accepts were bad, "
              f"which puts the bad share at no more than {w_pool[2]}% (Wilson 95% upper bound)"),
    "type": "point_ci",
    "x_title": "Blind review",
    "y_title": "Bad claims among Jev's auto-accepts (%)",
    "x": [f"F.2: WS1's 5 dossiers ({f2_acc} accepts)", f"F4: 22 new dossiers ({acc['n']} accepts)",
          f"Pooled F.2 + F4 ({pool_n} accepts)"],
    "series": [
        {"name": "Bad share among auto-accepts (point and Wilson 95% interval)",
         "values": [w_f2[0], w_f4[0], w_pool[0]], "ci_low": [w_f2[1], w_f4[1], w_pool[1]],
         "ci_high": [w_f2[2], w_f4[2], w_pool[2]],
         "counts": [f"{f2_bad}/{f2_acc}", f"{acc['k']}/{acc['n']}", f"{pool_k}/{pool_n}"],
         "marker": "diamond", "dash": "none"},
    ],
    "annotations": ["Both reviews used the same rule: flag 'none', confidence at least 0.8 on both repeats, saved-page window",
                    "Intervals treat claims as independent; claims cluster by vendor, so the true uncertainty is somewhat wider"],
    "error_bars": "Wilson 95%",
    "n_claims": pool_n,
    "n_claims_note": f"{pool_n} auto-accepted claims ({f2_acc} in F.2, {acc['n']} in F4)",
    "n_recorded_fails": pool_k,
    "n_recorded_fails_note": (f"among the auto-accepts; the reviews found {n_fail} fails in F.2 and {smp['k']} in F4's "
                              f"{smp['n']}-claim sample of non-accepts, none of them accepted"),
    "split": "live: F.2 (5 WS1 dossiers) and F4 (22 other dossiers), blind Opus review",
    "direction": "lower is safer",
    "sources": [
        "F.2 accepts = adj/results.json -> jev_router_curve.primary_armB[t=0.8].useful_accept.k + .miss.k; bad = .miss.k",
        "F4 = f4/adj/results.json -> primary_bad_share_armB_accepts {k, n, wilson95}",
        "F.2 and pooled intervals computed here with the Wilson formula (z = 1.959964, as in adj/score.py)",
    ],
})

# accepts by section: in-scope denominators and per-section accepts come from the raw shadow claims,
# cross-checked against the results files below (asserts)
def by_section(rel):
    rows = [r for r in jsonl(rel) if r["scope"] == "in_scope" and r.get("set") == "primary"]
    return Counter(r["section"] for r in rows), Counter(r["section"] for r in rows if r["accept_B_conservative"])


ws1_in, ws1_acc = by_section("ws1/shadow_claims.jsonl")
f4_in, f4_acc = by_section("f4/shadow_claims.jsonl")
assert sum(ws1_in.values()) == n_all and sum(ws1_acc.values()) == f2_acc
assert all(ws1_in[s] == sec[s]["n"] for s in sec), "F.2 section sizes disagree with adj/results.json"
assert sum(f4_in.values()) == F["n_in_scope"] and sum(f4_acc.values()) == acc["n"]
assert all(f4_acc[s] == F["armB_accepts_by_section"][s]["n"] for s in F["armB_accepts_by_section"])
assert all(F["armB_accepts_by_section"][s]["bad"] == 0 for s in F["armB_accepts_by_section"])

cats = [("Identity", ["Identity"]), ("Headline numbers", ["Headline numbers"]), ("Integrations", ["Integrations"]),
        ("Dimension scores", ["Dimension scores"]), ("Pricing or right-of-reply", ["Pricing detail", "Right-of-reply"])]


def ser(inn, ac):
    ks = [sum(ac[s] for s in ss) for _, ss in cats]
    ns = [sum(inn[s] for s in ss) for _, ss in cats]
    w = [wilson(k, n) for k, n in zip(ks, ns)]
    return ks, ns, w


k1, n1, w1 = ser(ws1_in, ws1_acc)
k2, n2, w2 = ser(f4_in, f4_acc)
dim_n = ws1_in["Dimension scores"] + f4_in["Dimension scores"]
dim_k = ws1_acc["Dimension scores"] + f4_acc["Dimension scores"]
assert dim_k == 0
f4.append({
    "title": (f"Every Jev auto-accept in both reviews was a fact row: it accepted none of the {dim_n} "
              "dimension-score rows, which it checks without the scoring rubric"),
    "type": "grouped_bar",
    "x_title": "Dossier section",
    "y_title": "In-scope claims Jev auto-accepted (% of the section)",
    "x": [c for c, _ in cats],
    "series": [
        {"name": f"F.2: WS1's 5 dossiers ({sum(k1)} accepts of {sum(n1)})", "values": [w[0] for w in w1],
         "ci_low": [w[1] for w in w1], "ci_high": [w[2] for w in w1],
         "counts": [f"{k}/{n}" for k, n in zip(k1, n1)], "pattern": "solid"},
        {"name": f"F4: 22 new dossiers ({sum(k2)} accepts of {sum(n2)})", "values": [w[0] for w in w2],
         "ci_low": [w[1] for w in w2], "ci_high": [w[2] for w in w2],
         "counts": [f"{k}/{n}" for k, n in zip(k2, n2)], "pattern": "hatched"},
    ],
    "annotations": ["The blind review found 0 bad among these accepts (F.2 0 of 16, F4 0 of 111)",
                    "'Pricing or right-of-reply' is 1 claim in each review: F.2 pricing detail, F4 right-of-reply"],
    "error_bars": "Wilson 95%",
    "n_claims": sum(n1) + sum(n2),
    "n_claims_note": f"{sum(n1) + sum(n2)} in-scope claims ({sum(n1)} F.2 + {sum(n2)} F4)",
    "n_recorded_fails": n_fail + smp["k"],
    "n_recorded_fails_note": f"F.2 {n_fail} of {n_all} (all reviewed); F4 {smp['k']} in the {smp['n']}-claim sample of non-accepts, 0 among accepts",
    "split": "live: F.2 (5 WS1 dossiers) and F4 (22 other dossiers)",
    "direction": "higher is better (more claims cleared without review; the cleared ones reviewed at 0 bad)",
    "sources": [
        "counts per section derived from ws1/shadow_claims.jsonl and f4/shadow_claims.jsonl: rows with scope == 'in_scope', "
        "set == 'primary', grouped by section; accepts = accept_B_conservative",
        "cross-checked (asserts): F.2 section sizes == adj/results.json fail_share_by_section.<s>.n; F.2 total accepts == "
        "jev_router_curve.primary_armB[t=0.8] useful_accept.k + miss.k; F4 accepts per section == "
        "f4/adj/results.json armB_accepts_by_section.<s>.n (bad 0 each); F4 in-scope total == n_in_scope",
        "intervals computed here with the Wilson formula (z = 1.959964)",
    ],
})

os.makedirs(os.path.join(HERE, "specs"), exist_ok=True)
for name, specs in (("adj", adj), ("f4", f4)):
    with open(os.path.join(HERE, "specs", f"{name}.json"), "w", encoding="utf-8") as f:
        json.dump(specs, f, indent=1, ensure_ascii=False)
    print(name, len(specs), "specs:", [s["title"][:70] for s in specs])
print("pooled", pool_k, pool_n, w_pool, "F.2", f2_bad, f2_acc, w_f2)
print("sections F.2", list(zip(k1, n1)), "F4", list(zip(k2, n2)))
