"""Score the window-only re-read against the prior verdicts (all 82 were passes) and write REPORT.md.

Reads r*/labels.jsonl (non-superseded rows only). Prints counts by verdict, flag and confidence, every label whose
flag is not `none`, and every page_context that names a different entity than the claim's vendor. Wilson 95% upper
bound on the pooled accept-bad share is recomputed with the new fails added to 0 of 127.
"""
import io, json, glob, math, os, re, collections

HERE = os.path.dirname(os.path.abspath(__file__))
N_ACCEPTS = 127
FAIL_FLAGS = {"dim_disagreement_major", "contradicted_fact", "fabricated_or_unsupported_claim"}


def jl(p):
    return [json.loads(x) for x in io.open(p, encoding="utf-8") if x.strip()]


def wilson_hi(k, n, z=1.959964):
    if n == 0:
        return float("nan")
    p = k / n
    d = 1 + z * z / n
    c = p + z * z / (2 * n)
    h = z * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n))
    return (c + h) / d


def main():
    packets = {}
    for sp in sorted(glob.glob(os.path.join(HERE, "inputs", "slice_r*.jsonl"))):
        for p in jl(sp):
            packets[p["packet_id"]] = p
    labels = {}
    per_agent = collections.Counter()
    for lp in sorted(glob.glob(os.path.join(HERE, "r*", "labels.jsonl"))):
        for l in jl(lp):
            if l.get("superseded"):
                continue
            assert l["packet_id"] in packets, l["packet_id"]
            assert "saved_page" in l["checked"], (l["packet_id"], l["checked"])
            labels[l["packet_id"]] = l
            per_agent[l["agent"]] += 1
    n_exp, n_got = len(packets), len(labels)
    missing = sorted(set(packets) - set(labels))
    by_verdict = collections.Counter(l["verdict"] for l in labels.values())
    by_flag = collections.Counter(l["flag"] for l in labels.values())
    by_conf = collections.Counter(l["confidence"] for l in labels.values())
    by_checked = collections.Counter(c for l in labels.values() for c in l["checked"])
    fails = [l for l in labels.values() if l["verdict"] == "fail"]
    assert all((l["flag"] in FAIL_FLAGS) == (l["verdict"] == "fail") for l in labels.values())

    def vendor_in_context(l):
        v = packets[l["packet_id"]]["vendor"].lower()
        v0 = re.sub(r"[^a-z0-9]", "", v.split()[0]) if v else ""
        return v0 and v0 in re.sub(r"[^a-z0-9]", "", l.get("page_context", "").lower())

    other_entity_words = re.compile(r"acquir|parent|subsidiar|now part of|rebrand|formerly|owned by|merged|"
                                    r"not the (vendor|company)|different (company|entity)|third[- ]party|reseller|"
                                    r"press|news|wikipedia|review site|g2|capterra|crunchbase", re.I)
    flagged_ctx = [l for l in labels.values() if other_entity_words.search(l.get("page_context", "")) or not vendor_in_context(l)]

    k = len(fails)
    lines = []
    w = lines.append
    w("# Re-read of the window-only accepts: results")
    w("")
    w(f"status: {'complete' if not missing else 'INCOMPLETE'}, {n_got} of {n_exp} labelled "
      f"({', '.join(f'{a} {c}' for a, c in sorted(per_agent.items()))}).")
    if missing:
        w(f"missing: {' '.join(missing)}")
    w("")
    w("Every label lists `saved_page`; the recording script refused any that did not. Prior verdicts were withheld.")
    w("")
    w("## Counts")
    w("")
    w(f"- verdicts: {dict(by_verdict)}")
    w(f"- flags: {dict(by_flag)}")
    w(f"- confidence: {dict(by_conf)}")
    w(f"- consulted: {dict(by_checked)}")
    w("")
    w("## Against the prior verdicts")
    w("")
    w(f"All {n_exp} were passes on a review that recorded no page read. On the whole-page re-read, {k} fail.")
    w(f"Pooled accept-bad share: {k} of {N_ACCEPTS} (Wilson 95% upper bound {100*wilson_hi(k, N_ACCEPTS):.1f}%), "
      f"was 0 of 127 (2.9%).")
    w("")
    w("## Every label whose flag is not `none`")
    w("")
    w("| packet | vendor | claim (short) | flag | verdict | conf | reason |")
    w("|---|---|---|---|---|---|---|")
    for l in sorted(labels.values(), key=lambda x: (x["verdict"] != "fail", x["flag"], x["packet_id"])):
        if l["flag"] == "none":
            continue
        p = packets[l["packet_id"]]
        w(f"| {l['packet_id']} | {p['vendor']} | {p['claim'][:70].replace('|', '/')} | {l['flag']} | {l['verdict']} | "
          f"{l['confidence']} | {l['reason'][:220].replace('|', '/')} |")
    w("")
    w("## Pages that may belong to a different entity than the claim names (keyword filter on `page_context`, for a human to read)")
    w("")
    for l in sorted(flagged_ctx, key=lambda x: x["packet_id"]):
        p = packets[l["packet_id"]]
        w(f"- {l['packet_id']} {p['vendor']} [{l['flag']}]: {l['page_context']}")
    w("")
    w("Scored by `score.py`; no model was called.")
    io.open(os.path.join(HERE, "REPORT.md"), "w", encoding="utf-8", newline="\n").write("\n".join(lines) + "\n")
    print("\n".join(lines[:16]))
    print(f"... non-none flags: {sum(1 for l in labels.values() if l['flag'] != 'none')}; "
          f"context-flagged: {len(flagged_ctx)}; report written")


if __name__ == "__main__":
    main()
