"""Summarize the 2.5 English runs (no API calls).

Reads results.json (main run), results_rep.json (replicate) and results_hedge.json (hedge pilot), then
- writes a "summary" block into results.json (the numbers the README, FINDINGS and CONTRACT quote), and
- writes experiments_en/chart_data/ch02_05_neutralize.json (the bare entry, like the sibling files; EN_SCHEMA in tools/charts.py).
The chart uses the main run, like the Chinese chart; the replicate is reported in the note.

  .venv/bin/python experiments_en/2_5_perspectives/summarize.py
"""
import json
import pathlib
import re

HERE = pathlib.Path(__file__).resolve().parent
MODELS = ["deepseek-v4.1-flash", "gemini-3.8-flash"]
QS = [f"Q{i:02d}" for i in range(1, 11)]
COND_OPENER = re.compile(r"(agree|support)[^.\n]{0,40}with conditions|conditional(ly)? (support|agree)", re.I)
HEDGE_HEAD = re.compile(r"caveat|overstat|too strong|nuance|counterpoint|counterargument|open question|attack|critique", re.I)
EXTERNAL = re.compile(r"source|evidence|related reading|related concepts|context \(external\)|for reference", re.I)


def load(name):
    return json.load(open(HERE / name, encoding="utf-8"))


def main_summary(res):
    out = {}
    for m in MODELS:
        out[m] = {}
        for c in ["skill_only", "with_perspectives"]:
            rows = res[m][c]["rows"]
            out[m][c] = dict(res[m][c]["summary"],
                             hit_ids=[q for q in QS if rows[q]["judge"].get("hit_perspective")],
                             endorsed_ids=[q for q in QS if rows[q]["judge"].get("endorsed_mainstream")],
                             agree_with_conditions_opener=[q for q in QS if COND_OPENER.search(rows[q]["note"][:400])],
                             median_words=sorted(r["words"] for r in rows.values())[5])
    return out


def hedge_summary(h):
    out = {}
    for m in MODELS:
        out[m] = {}
        for c, v in h[m].items():
            rows = v["rows"]
            out[m][c] = {**{k: v["summary"][k] for k in ("kept", "weakened", "hedges", "per_memo")},
                         "hedge_section_headings": {k: [x for x in r["headings"] if HEDGE_HEAD.search(x)]
                                                    for k, r in rows.items() if any(HEDGE_HEAD.search(x) for x in r["headings"])},
                         "notes_with_external_research": [k for k, r in rows.items()
                                                          if any(EXTERNAL.search(x) for x in r["headings"]) or "http" in r["note"]],
                         "median_words": sorted(len(r["note"].split()) for r in rows.values())[5]}
    return out


if __name__ == "__main__":
    res, rep, hedge = load("results.json"), load("results_rep.json"), load("results_hedge.json")
    main1, main2 = main_summary(res), main_summary(rep)
    per_q = {q: {"skill_only_hits_of_4": sum(r[m]["skill_only"]["rows"][q]["judge"]["hit_perspective"] for r in (res, rep) for m in MODELS),
                 "skill_only_endorsed_of_4": sum(bool(r[m]["skill_only"]["rows"][q]["judge"]["endorsed_mainstream"]) for r in (res, rep) for m in MODELS)}
             for q in QS}
    res["summary"] = {"main_run": main1, "replicate_run": main2, "per_proposal_skill_only_both_runs": per_q,
                      "hedge_pilot": hedge_summary(hedge)}
    json.dump(res, open(HERE / "results.json", "w", encoding="utf-8"), ensure_ascii=False, indent=1)

    entry = {
        "total": 10,
        "ticks": [0, 2, 4, 6, 8, 10],
        "skill_only": {m: main1[m]["skill_only"]["hit_perspective"] for m in MODELS},
        "with_perspectives": {m: main1[m]["with_perspectives"]["hit_perspective"] for m in MODELS},
        "conceded": {m: main1[m]["skill_only"]["endorsed_mainstream"] for m in MODELS},
        "labels": {
            "title": "Same skill: half or fewer got a non-consensus call; with perspectives.md, all 10",
            "note": "Both conditions use the exact same evaluation-note skill; the other vendor's model judges. "
                    "A second run gave {rep_ds} and {rep_gm} without perspectives.md, 10 and 10 with it.".format(
                        rep_ds=main2[MODELS[0]]["skill_only"]["hit_perspective"],
                        rep_gm=main2[MODELS[1]]["skill_only"]["hit_perspective"]),
        },
    }
    out = HERE.parent / "chart_data" / "ch02_05_neutralize.json"
    out.parent.mkdir(exist_ok=True)
    json.dump(entry, open(out, "w", encoding="utf-8"), ensure_ascii=False, indent=1)
    print(json.dumps({"main": {m: {c: main1[m][c]["hit_perspective"] for c in main1[m]} for m in MODELS},
                      "rep": {m: {c: main2[m][c]["hit_perspective"] for c in main2[m]} for m in MODELS}}, indent=1))
    print("wrote", out)
