#!/usr/bin/env python3
"""Build charts.json from ws2_results.json (no computation beyond unit conversion)."""
import json
import os

HERE = os.path.dirname(os.path.abspath(__file__))
R = json.load(open(os.path.join(HERE, "ws2_results.json"), encoding="utf-8"))
best = R["best_candidate"]["id"]
dev = R["counts"]["dev"]
test = R["counts"]["test"]
pct = lambda v: None if v is None else round(100 * v, 1)  # noqa: E731


def curve_pts(sid):
    pts = []
    for p in R["q2_3"]["curves"][sid]:
        a, m = p["accept_share_recorded_pass"], p["miss_rate"]
        pts.append({"x": pct(a["p"]), "y": pct(m["p"]), "y_lo": pct(m["lo"]), "y_hi": pct(m["hi"]), "cut": p["cut"]})
    return pts


def op_pt(summary, label):
    a, m = summary["accept_share_recorded_pass"], summary["misses"]
    return {"x": pct(a["p"]), "y": pct(m["p"]), "y_lo": pct(m["lo"]), "y_hi": pct(m["hi"]), "label": label}


z = R["q2_2"]["zero_miss"]
chart1 = {
    "title": "On dev, the current rule lets no recorded fail through but accepts only 26% of recorded passes; "
             "accepting more costs misses quickly, and a probability-based score traces almost the same curve.",
    "type": "line",
    "x_title": "Recorded passes accepted without review (% of dev recorded passes)",
    "y_title": "Recorded fails let through (% of dev recorded fails)",
    "series": [
        {"name": "Current rule family: both repeats 'none', confidence >= cut", "values": curve_pts("C0"),
         "dash": "solid", "marker": "circle"},
        {"name": f"Best candidate {best}: {R['best_candidate']['desc']} >= cut", "values": curve_pts(best),
         "dash": "dash", "marker": "square"},
        {"name": "Current rule as deployed (cut 0.8), in-sample",
         "values": [op_pt(R["existing_rule_0p8"]["dev"]["two_repeat"], "C0 >= 0.8")], "dash": "none", "marker": "star"},
        {"name": "Out-of-fold result when the cut is tuned to zero misses on the other 4 folds",
         "values": [op_pt(z["C0"]["summary"], "current rule, CV"), op_pt(z["nested_candidate"]["summary"], "candidate, CV")],
         "dash": "none", "marker": "diamond"},
    ],
    "n_claims": dev["claims"], "n_recorded_fails": dev["recorded_fail"], "split": "dev",
    "direction": "lower is safer",
    "note": "Curves sweep every cut over all dev claims (description, not a choice). Miss-rate intervals are Wilson 95% "
            "(y_lo, y_hi). Claims from one vendor are not independent.",
}


def rel(split, key="none_conf_pooled"):
    return [{"x": pct(r["mean_conf"]), "y": pct(r["p"]), "y_lo": pct(r["lo"]), "y_hi": pct(r["hi"]), "n": r["n"],
             "too_small": bool(r.get("too_small"))} for r in R["q2_1"][split][key]["table"]]


cd = R["q2_1"]["dev"]["none_conf_pooled"]
ct = R["q2_1"]["test"]["none_conf_pooled"]
chart2 = {
    "title": f"When Jev answers 'none', the claim is a recorded pass far more often than its confidence says: "
             f"it is underconfident (dev ECE {cd['ece']:.2f}), not overconfident.",
    "type": "line",
    "x_title": "Jev's stated confidence, bin average (%)",
    "y_title": "Answers whose claim is a recorded pass (%)",
    "series": [
        {"name": f"Dev ({cd['n_calls']} 'none' answers)", "values": rel("dev"), "dash": "solid", "marker": "circle"},
        {"name": f"Held-out test, descriptive only ({ct['n_calls']} 'none' answers)", "values": rel("test"),
         "dash": "dash", "marker": "triangle"},
        {"name": "Perfect calibration", "values": [{"x": 0, "y": 0}, {"x": 100, "y": 100}], "dash": "dot", "marker": "none"},
    ],
    "n_claims": dev["claims"] + test["claims"], "n_recorded_fails": dev["recorded_fail"] + test["recorded_fail"],
    "split": "dev (primary) and held-out test (descriptive)",
    "direction": "higher is better",
    "note": "Points above the dotted diagonal mean underconfidence. Unit is the call (2 repeats per claim). Bins with "
            "n < 10 carry too_small = true and should be drawn hollow.",
}

bars = []
for sid, lab in [("C0", "Current rule (C0)")] + [(k, k) for k in ["K1", "K2", "K3", "K4", "K5", "K6", "K7"]]:
    s = z["C0"]["summary"] if sid == "C0" else z["per_score_exploratory"][sid]["summary"]
    a = s["accept_share_recorded_pass"]
    bars.append({"x": lab, "y": pct(a["p"]), "y_lo": pct(a["lo"]), "y_hi": pct(a["hi"]),
                 "oof_misses": f"{s['misses']['k']}/{s['misses']['n']}"})
chart3 = {
    "title": "Tuned to zero misses by 5-fold vendor-grouped cross-validation, no probability-based score accepts "
             "clearly more recorded passes than the current confidence rule.",
    "type": "bar",
    "x_title": "Accept score (C0 current rule family; K1-K7 candidates, see REPORT.md)",
    "y_title": "Dev recorded passes accepted out of fold (%)",
    "series": [
        {"name": "Current rule family", "values": [bars[0]], "pattern": "solid"},
        {"name": "Candidate scores", "values": bars[1:], "pattern": "hatched"},
    ],
    "n_claims": dev["claims"], "n_recorded_fails": dev["recorded_fail"], "split": "dev (5-fold vendor-grouped CV)",
    "direction": "higher is better",
    "note": "Error bars are Wilson 95%. oof_misses = recorded fails accepted out of fold; K5-K7 accept fewer passes "
            "but also let through fewer fails, so they sit at a different operating point.",
}

sz = []
for op in R["q2_3"]["operating_points"]:
    if op["score"] != "C0":
        continue
    lab = {"cv_zero_miss": "CV zero-miss cut", "cv_cap_5pct": "CV 5% miss cap",
           "deployed_0.8_in_sample": "Deployed 0.8"}.get(op["point"])
    if not lab:
        continue
    for r in op["sample_sizes"]:
        if r["assumed"] == "oof_point":
            sz.append((lab, r["half_width"], r["n_fails_wilson"], r["p"]))
chart4 = {
    "title": "Pinning Jev's miss rate to within 3 points takes roughly 125 to 340 adjudicated recorded fails at the "
             "current rule's operating points; within 5 points takes roughly 75 to 150.",
    "type": "bar",
    "x_title": "Operating point of the current rule family (assumed miss rate from dev cross-validation)",
    "y_title": "Adjudicated recorded fails needed (count)",
    "series": [
        {"name": "95% interval within +-5 points (Wilson)",
         "values": [{"x": f"{l} (p={p:.3f})", "y": n} for l, d, n, p in sz if d == 0.05], "pattern": "solid"},
        {"name": "95% interval within +-3 points (Wilson)",
         "values": [{"x": f"{l} (p={p:.3f})", "y": n} for l, d, n, p in sz if d == 0.03], "pattern": "hatched"},
    ],
    "n_claims": dev["claims"], "n_recorded_fails": dev["recorded_fail"], "split": "dev (assumptions only)",
    "direction": "lower is safer",
    "note": "Lower means less adjudication work. Claims to adjudicate blind = fails needed / live fail base rate "
            "(dev base rate 0.337; see REPORT.md table for 0.20 and 0.10). The 5% and 10% caps give the same point for C0.",
}

with open(os.path.join(HERE, "charts.json"), "w", encoding="utf-8") as fh:
    json.dump([chart1, chart2, chart3, chart4], fh, indent=1)
print("wrote charts.json", [len(c["series"]) for c in (chart1, chart2, chart3, chart4)])
