"""Measure the arm-B evidence channel across every shadow claim.

No model calls. Reads the committed sidecar rows and the saved page files and
reports, per set: the evidence kind mix, the keyword-overlap score
distribution, and how often the stored window is NOT a contiguous substring of
the page it was cut from.
"""
import json
import os
import re
import sys
import statistics as st

EXP = r"out/exp/"
SPIKE = r"spike/"
sys.path.insert(0, SPIKE)
from t2_raw_excerpt import tokens, sentences  # noqa: E402


def load(p):
    with open(p, encoding="utf-8") as fh:
        return [json.loads(l) for l in fh if l.strip()]


def page_text(path):
    """Resolve a saved_page_path against the known roots."""
    if not path:
        return None
    cands = [path, os.path.join(EXP, path), os.path.join(SPIKE, path)]
    for c in cands:
        if os.path.isfile(c):
            with open(c, encoding="utf-8", errors="replace") as fh:
                return fh.read()
    return None


def score_of(claim, page):
    """Recompute t2_raw_excerpt's best-sentence overlap score."""
    cn, cw = tokens(claim)
    best = 0
    for s in sentences(page):
        sn, sw = tokens(s)
        sc = 3 * len(cn & sn) + len(cw & sw)
        if sc > best:
            best = sc
    return best


def contiguous(window, page):
    """True if the window appears verbatim in the page.

    Falls back to a whitespace-normalized comparison, because the saved page is
    read raw here while the window was joined with single spaces.
    """
    if not window or not page:
        return None
    if window in page:
        return True
    norm = lambda t: re.sub(r"\s+", " ", t).strip()
    return norm(window) in norm(page)


def longest_run(window, page):
    """Longest leading run of the window's words that is contiguous in the page."""
    words = window.split()
    npage = re.sub(r"\s+", " ", page)
    best = 0
    for start in range(len(words)):
        lo, hi = 0, len(words) - start
        while lo < hi:
            mid = (lo + hi + 1) // 2
            if " ".join(words[start:start + mid]) in npage:
                lo = mid
            else:
                hi = mid - 1
        best = max(best, lo)
        if best == len(words):
            break
    return best, len(words)


def report(name, rows):
    rows = [r for r in rows if r.get("arm_b_evidence")]
    kinds = {}
    for r in rows:
        kinds[r.get("arm_b_kind")] = kinds.get(r.get("arm_b_kind"), 0) + 1
    print(f"\n## {name}: {len(rows)} claims with arm-B evidence")
    for k, v in sorted(kinds.items(), key=lambda x: -x[1]):
        print(f"   {k}: {v} ({100*v/len(rows):.0f}%)")

    kw = [r for r in rows if r.get("arm_b_kind") == "keyword_window"]
    scores, missing_page, spliced, runs = [], 0, [], []
    for r in kw:
        page = page_text(r.get("saved_page_path"))
        if page is None:
            missing_page += 1
            s = r.get("keyword_score")
            if s is not None:
                scores.append(s)
            continue
        s = r.get("keyword_score")
        if s is None:
            s = score_of(r["claim"], page)
        scores.append(s)
        if contiguous(r["arm_b_evidence"], page) is False:
            got, tot = longest_run(r["arm_b_evidence"], page)
            spliced.append((r["claim_id"], got, tot))
        runs.append(1)

    if scores:
        scores.sort()
        print(f"   keyword overlap score: n={len(scores)} min={min(scores)} "
              f"median={st.median(scores):.0f} max={max(scores)} "
              f"| <=3: {sum(1 for s in scores if s <= 3)} "
              f"| <=1: {sum(1 for s in scores if s <= 1)}")
    print(f"   pages checked for contiguity: {len(runs)} "
          f"(saved page not found for {missing_page})")
    print(f"   NOT contiguous page text: {len(spliced)}")
    for cid, got, tot in spliced[:12]:
        print(f"      {cid}: longest contiguous run {got} of {tot} words")
    return len(rows), kinds, scores, len(spliced), len(runs)


if __name__ == "__main__":
    ws1 = load(os.path.join(EXP, "ws1/shadow_claims.jsonl"))
    f4 = load(os.path.join(EXP, "f4/shadow_claims.jsonl"))
    a = report("ws1 pilot", ws1)
    b = report("f4 scale-up", f4)

    tot = a[0] + b[0]
    kw = a[1].get("keyword_window", 0) + b[1].get("keyword_window", 0)
    vb = a[1].get("verbatim_window", 0) + b[1].get("verbatim_window", 0)
    alls = sorted(a[2] + b[2])
    print(f"\n## combined: {tot} claims")
    print(f"   keyword_window {kw} ({100*kw/tot:.0f}%), verbatim_window {vb} ({100*vb/tot:.0f}%)")
    if alls:
        print(f"   score min={min(alls)} median={st.median(alls):.0f} max={max(alls)} "
              f"<=3: {sum(1 for s in alls if s <= 3)} ({100*sum(1 for s in alls if s<=3)/len(alls):.0f}% of keyword windows)")
    print(f"   not contiguous: {a[3] + b[3]} of {a[4] + b[4]} checked")
