#!/usr/bin/env python3
"""F4 blind review packet (adapted from ws1/scripts/make_packet.py).
Selected = every in-scope claim accepted by arm A or arm B (conservative 2-repeat rule, 0.8) plus a random sample of
ceil(0.10 * N_rest) of the other in-scope claims (random.Random("f4-sample-2026-09-22") over the rest sorted by
(run_id, unit_index)). Shuffled with random.Random("f4-packet-2026-09-22"); packet ids = 8 hex chars from that
generator. The packet carries NO model output, NO ground.py / step 03 result and NO selection reason; the key is
written separately to adjudication_key.json."""
import hashlib
import json
import math
import random
from collections import Counter
from pathlib import Path

F4 = Path("out/exp/f4")
SAMPLE_SEED, PACKET_SEED, FRAC = "f4-sample-2026-09-22", "f4-packet-2026-09-22", 0.10

rows = [json.loads(l) for l in (F4 / "shadow_claims.jsonl").read_text(encoding="utf-8").split("\n") if l.strip()]
ins = [r for r in rows if r["scope"] == "in_scope"]
acc = [r for r in ins if r.get("accept_A_conservative") is True or r.get("accept_B_conservative") is True]
rest = sorted((r for r in ins if not (r.get("accept_A_conservative") is True or r.get("accept_B_conservative") is True)),
              key=lambda r: (r["run_id"], r["unit_index"]))
n_s = math.ceil(FRAC * len(rest))
samp = random.Random(SAMPLE_SEED).sample(rest, n_s)


def why(r):
    a, b = r.get("accept_A_conservative") is True, r.get("accept_B_conservative") is True
    return "accept_AB" if a and b else "accept_A" if a else "accept_B" if b else "random_sample"


sel = [(r, why(r)) for r in acc] + [(r, "random_sample") for r in samp]
names = {}
for r, _ in sel:
    k = (r["pass"], r["slug"])
    if k not in names:
        meta = json.loads((F4 / f"onboarding-io-p{r['pass']}" / "runs" / r["slug"] / "bundle" / "meta.json").read_text(encoding="utf-8"))
        names[k] = meta.get("name") or r["slug"]
rng = random.Random(PACKET_SEED)
order = list(range(len(sel)))
rng.shuffle(order)
ids, packet, key = set(), [], {}
for i in order:
    r, w = sel[i]
    pid = "%08x" % rng.getrandbits(32)
    while pid in ids:
        pid = "%08x" % rng.getrandbits(32)
    ids.add(pid)
    packet.append({"packet_id": pid, "vendor": names[(r["pass"], r["slug"])], "section": r["section"], "claim": r["claim"],
                   "source_url": r["source_url"], "evidence": r["arm_b_evidence"], "saved_page_path": r["saved_page_path"],
                   "as_of": r["date"]})
    key[pid] = {"run_id": r["run_id"], "pass": r["pass"], "slug": r["slug"], "half": r["half"], "set": r["set"],
                "driver_exit": r["driver_exit"], "claim_id": r["claim_id"], "unit_index": r["unit_index"],
                "claim_sha256": r["claim_sha256"], "arm_b_sha256": r["arm_b_sha256"], "selected_because": w}
with open(F4 / "adjudication_packet.jsonl", "w", encoding="utf-8", newline="\n") as f:
    for p in packet:
        f.write(json.dumps(p, ensure_ascii=False) + "\n")
cnt = Counter(v["selected_because"] for v in key.values())
json.dump({"note": "packet_id -> claim and selection reason; keep away from the reviewer",
           "sample_seed": SAMPLE_SEED, "packet_seed": PACKET_SEED, "sample_fraction": FRAC,
           "n_in_scope": len(ins), "n_accepted_either_arm": len(acc), "N_rest": len(rest), "n_s": n_s,
           "rest_weight": (len(rest) / n_s) if n_s else None, "n_packet": len(packet), "selected_counts": dict(cnt),
           "n_errored_claims_in_rest": sum(1 for r in rest if r.get("accept_A_conservative") is None or r.get("accept_B_conservative") is None),
           "packet_sha256": hashlib.sha256((F4 / "adjudication_packet.jsonl").read_bytes()).hexdigest(),
           "key": key}, open(F4 / "adjudication_key.json", "w", encoding="utf-8"), indent=1)
print(json.dumps({"in_scope": len(ins), "accepted_either": len(acc), "N_rest": len(rest), "n_s": n_s,
                  "packet": len(packet), "counts": dict(cnt)}))
