"""F.3: founder labels vs the blind Opus labels (adj/a1..a4). Run from this folder.

Writes kept.json (the rule-based keep set from DECISION.md) and SCORE.md (aggregate only)."""
import glob, json, os, re, math
from collections import Counter, defaultdict

HERE = os.path.dirname(os.path.abspath(__file__))
ADJ = os.path.dirname(os.path.dirname(HERE))
PAGE = os.path.join(ADJ, "founder", "dossier-claim-labels-v2.html")

html = open(PAGE, encoding="utf-8").read()
data = json.loads(re.search(r'<script type="application/json" id="claim-data">(.*?)</script>', html, re.S).group(1))
VERDICT = {f["key"]: f["verdict"] for f in data["flags"]}
claims = {c["id"]: c for c in data["claims"]}

founder = {}
for f in glob.glob(os.path.join(HERE, "labels-snapshot", "*.json")):
    d = json.load(open(f, encoding="utf-8"))
    founder[d["packet_id"]] = d

# DECISION.md rules 1 and 2, applied mechanically
FRAGMENTS = {"0fa5cd45"}
kept = sorted(p for p in founder
              if claims[p]["section"] != "Dimension scores" and p not in FRAGMENTS)
json.dump({"rule": "DECISION.md rules 1-2", "kept": kept, "n": len(kept)},
          open(os.path.join(HERE, "kept.json"), "w"), indent=1)

opus = defaultdict(list)
for f in glob.glob(os.path.join(ADJ, "a[1-4]", "labels.jsonl")):
    for line in open(f, encoding="utf-8"):
        if line.strip():
            r = json.loads(line)
            opus[r["packet_id"]].append(r)

def kappa(pairs):
    n = len(pairs)
    po = sum(a == b for a, b in pairs) / n
    ca, cb = Counter(a for a, _ in pairs), Counter(b for _, b in pairs)
    pe = sum(ca[k] * cb[k] for k in set(ca) | set(cb)) / n / n
    return po, (po - pe) / (1 - pe) if pe < 1 else float("nan")

def wilson(k, n, z=1.96):
    p = k / n; d = 1 + z * z / n
    c = (p + z * z / (2 * n)) / d; h = z * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n)) / d
    return c - h, c + h

def block(ids, name):
    missing = [p for p in ids if p not in opus]
    multi = [p for p in ids if len(opus.get(p, [])) > 1]
    pairs, flag_pairs = [], []
    for p in ids:
        if p not in opus:
            continue
        o = opus[p][0]  # the first blind label is the answer key
        pairs.append((founder[p]["verdict"], o["verdict"]))
        flag_pairs.append((founder[p]["flag"], o["flag"]))
    agree = sum(a == b for a, b in pairs)
    po, k = kappa(pairs)
    lo, hi = wilson(agree, len(pairs))
    cells = Counter(f"founder {a} / Opus {b}" for a, b in pairs)
    fl = sum(a == b for a, b in flag_pairs)
    return [f"## {name}",
            f"- compared: {len(pairs)} (no Opus label: {len(missing)}; more than one Opus label, first used: {len(multi)})",
            f"- verdicts agree: **{agree} of {len(pairs)}** ({po:.0%}, 95% interval {lo:.0%} to {hi:.0%}); kappa {k:.2f}",
            f"- table: " + "; ".join(f"{k2} {v}" for k2, v in sorted(cells.items())),
            f"- exact flag agree: {fl} of {len(flag_pairs)}", ""]

out = ["# F.3 score (aggregate only; per-claim Opus answers are not shown here)", ""]
out += block(kept, f"Kept set ({len(kept)}, primary)")
out += block(sorted(founder), f"All {len(founder)} as labelled (secondary)")

# sensitivity: claims where the Opus reviewers split, scored as a fail
split = [p for p in kept if len({r["verdict"] for r in opus[p]}) > 1]
anyfail = sum(founder[p]["verdict"] == ("fail" if any(r["verdict"] == "fail" for r in opus[p]) else "pass")
              for p in kept)
out += ["## Sensitivity", f"- kept claims where the Opus reviewers split: {len(split)}",
        f"- kept agreement if a split counts as fail: {anyfail} of {len(kept)}", ""]

# the subset that bears on the headline: claims Jev auto-accepted (arm B, conservative)
EXP = os.path.dirname(ADJ)
sha = {}
for ws in ("ws1", "f4"):
    for pid, v in json.load(open(os.path.join(EXP, ws, "adjudication_key.json"), encoding="utf-8"))["key"].items():
        sha[pid] = (ws, v["claim_sha256"])
acc = {}
for ws in ("ws1", "f4"):
    for line in open(os.path.join(EXP, ws, "shadow_claims.jsonl"), encoding="utf-8"):
        r = json.loads(line)
        acc[(ws, r.get("claim_sha256"))] = r.get("accept_B_conservative")
accepted = [p for p in kept if acc.get(sha.get(p))]
out += ["## Jev accepts in the kept set",
        f"- accepted by Jev: {len(accepted)} of {len(kept)}",
        f"- founder pass on those: {sum(founder[p]['verdict'] == 'pass' for p in accepted)}; "
        f"Opus pass: {sum(opus[p][0]['verdict'] == 'pass' for p in accepted)}",
        f"- disagreements that were accepts: {sum(founder[p]['verdict'] != opus[p][0]['verdict'] for p in accepted)}", ""]
open(os.path.join(HERE, "SCORE.md"), "w", encoding="utf-8").write("\n".join(out))
print("\n".join(out))
