#!/usr/bin/env python3
"""E107 offline verifier (stdlib only, imports no CAIN code).

Re-derives from rows.json: every success count, every McNemar test, the step sign test, the practice
counts and oracle accuracy, and both permutation gates (independent re-implementation of the
preregistered statistic and null, same seed), then checks every verdict in summary.json."""
import hashlib, json, math, random, sys
from pathlib import Path

C = []


def check(n, ok, d=""):
    C.append((n, bool(ok), str(d)))


def mc(a, b):
    n01 = sum(1 for x, y in zip(a, b) if x and not y); n10 = sum(1 for x, y in zip(a, b) if y and not x); n = n01 + n10
    return n01, n10, (min(1.0, sum(math.comb(n, i) for i in range(min(n01, n10) + 1)) / 2 ** n * 2) if n else 1.0)


def sign(d):
    pos = sum(1 for x in d if x > 0); neg = sum(1 for x in d if x < 0); n = pos + neg
    return pos, neg, (min(1.0, sum(math.comb(n, i) for i in range(min(pos, neg) + 1)) / 2 ** n * 2) if n else 1.0)


def feats(size):
    return [("col", k) for k in range(size)] + [("row", k) for k in range(size)] + [("cell", (x, y)) for x in range(size) for y in range(size)]


def inside(f, c):
    k, v = f
    return (k == "col" and c[0] == v) or (k == "row" and c[1] == v) or (k == "cell" and tuple(c) == v)


def stat(causes, F, E):
    best, arg = -1e9, None
    for f, e in zip(F, E):
        if e < 1.0:
            continue
        z = (sum(1 for c in causes if inside(f, c)) - e) / e ** 0.5
        if z > best:
            best, arg = z, f
    return best, arg


def gate(rows, perms=999, seed=107, alpha=0.01):
    size = rows[0]["size"]; F = feats(size)
    sup = [[tuple(c) for c in r["support"]] for r in rows]
    E = [sum(sum(1 for c in s if inside(f, c)) / len(s) for s in sup) for f in F]
    obs, arg = stat([tuple(r["cause"]) for r in rows], F, E)
    rng = random.Random(seed)
    null = sorted(stat([rng.choice(s) for s in sup], F, E)[0] for _ in range(perms))
    p = (1 + sum(1 for v in null if v >= obs)) / (1 + len(null))
    return round(obs, 3), [arg[0], list(arg[1]) if arg[0] == "cell" else arg[1]], round(p, 4), p <= alpha


def main():
    root = Path(sys.argv[1] if len(sys.argv) > 1 else ".")
    for rel, h in json.loads((root / "MANIFEST.json").read_text())["files"].items():
        check(f"hash {rel}", hashlib.sha256((root / rel).read_bytes()).hexdigest() == h)
    raw = json.loads((root / "rows.json").read_text()); s = json.loads((root / "summary.json").read_text())
    T = s["tests"]
    for name in ("edge_lock", "grid_key_door", "grid_other"):
        a, b = raw[f"{name}_control"], raw[f"{name}_treatment"]; t = T[name]
        check(f"{name}: same tasks in both arms", [r["id"] for r in a] == [r["id"] for r in b])
        check(f"{name}: success recount", (sum(r["success"] for r in a), sum(r["success"] for r in b), len(a)) == (t["success_a"], t["success_b"], t["n"]))
        x, y, p = mc([r["success"] for r in a], [r["success"] for r in b])
        check(f"{name}: McNemar recomputed", (x, y) == (t["mcnemar"]["a_only"], t["mcnemar"]["b_only"]) and abs(p - t["mcnemar"]["p"]) < 1e-9)
        st = lambda r: r["steps"] if r["success"] else r["max_steps"]
        sp, sn, sq = sign([st(v) - st(u) for u, v in zip(a, b)])
        check(f"{name}: step sign test recomputed", (sp, sn) == (t["steps_b_minus_a"]["pos"], t["steps_b_minus_a"]["neg"]) and abs(sq - t["steps_b_minus_a"]["p"]) < 1e-9)
    G = {}
    for k in ("edge", "grid_kd"):
        pr = raw[f"practice_{k}"]; ps = s[f"practice_{k}"]
        u = [r for r in pr if r["used"]]
        check(f"practice {k}: counts", (len(pr), sum(r["t1_success"] for r in pr), len(u)) == (ps["n"], ps["trial1_solved"], ps["used"]))
        check(f"practice {k}: used rows have exactly one candidate and a complete census",
              all(r["candidates"] and len(r["candidates"]) == 1 and r["census_complete"] and r["cause"] == r["candidates"][0] for r in u))
        bad = [r["id"] for r in u if r["cause"] not in r["support"]]
        ph = (s.get("post_hoc_findings") or {}).get(k, {})
        check(f"practice {k}: rows whose cause lies outside its own support = {len(bad)}, all disclosed", bad == ph.get("cause_outside_support", []))
        if bad:
            ok = [r for r in u if r["id"] not in bad]
            o2, f2, p2, u2 = gate(ok)
            g2 = ph["gate_without_them"]
            check(f"practice {k}: gate re-derived without those rows (feature {f2}, p {p2}, usable {u2}) matches the disclosed sensitivity",
                  (o2, f2, p2, u2) == (g2["observed_max_z"], g2["feature"], g2["p_value"], g2["usable"]))
        check(f"practice {k}: oracle accuracy recount", sum(bool(r["oracle_cause_ok"]) for r in u) == ps["oracle_cause_ok"])
        obs, f, p, use = gate(u)
        g = ps["gate"]; G[k] = (use, f)
        check(f"practice {k}: gate re-derived independently (z {obs}, feature {f}, p {p}, usable {use})",
              (obs, f, p, use) == (g["observed_max_z"], g["feature"], g["p_value"], g["usable"]))
    v = T["verdicts"]
    win = lambda t: t["mcnemar"]["p"] < 0.05 and t["mcnemar"]["b_only"] > t["mcnemar"]["a_only"]
    loss = lambda t: t["mcnemar"]["p"] < 0.05 and t["mcnemar"]["a_only"] > t["mcnemar"]["b_only"]
    e = T["edge_lock"]
    check("H1 verdict", v["H1_primary"] == win(e))
    check("H1b verdict", v["H1b"] == (e["steps_b_minus_a"]["p"] < 0.05 and e["steps_b_minus_a"]["neg"] > e["steps_b_minus_a"]["pos"]))
    check("H2 verdict", v["H2_gate_power"] == (G["edge"][0] and G["edge"][1] == ["col", 0]))
    check("H3 verdict", v["H3_gate_guardrail"] == (not G["grid_kd"][0]))
    check("H4 verdict", v["H4_attribution"] == all(s[f"practice_{k}"]["used"] and s[f"practice_{k}"]["oracle_cause_ok"] / s[f"practice_{k}"]["used"] >= 0.95 for k in ("edge", "grid_kd")))
    check("H5 verdict", v["H5_no_harm"] == (not loss(T["grid_key_door"]) and not loss(T["grid_other"])))
    passed = sum(ok for _, ok, _ in C)
    for n, ok, d in C:
        print(("PASS " if ok else "FAIL ") + n + ("" if ok else f" :: {d}"))
    print(f"\n{passed}/{len(C)} checks passed")
    return 0 if passed == len(C) else 1


if __name__ == "__main__":
    raise SystemExit(main())
