"""Every difference between a variant and the E baseline (10 chunks + document line), with evidence for adjudication."""
import json, glob, os, re, sys, psycopg
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from numeq import same_number
S=os.path.dirname(os.path.abspath(__file__)); V=sys.argv[1]
conn=psycopg.connect(host=os.environ["POSTGRES_HOST"],user="quber",password=os.environ["POSTGRES_PASSWORD"],dbname="quber_rag")
titles={r[0]:r[1] for r in conn.execute("select doc_key, title from ade_playground.documents").fetchall()}
CELL=re.compile(r"^t(\d+)-(\d+)-(\d+)$")
def cell(doc_id, ref):
    r=conn.execute("select cell_text, page from ade_playground.groundings where document_id=%s and ref_id=%s",(doc_id,ref)).fetchone(); return r
def label(doc_id, ref):
    m=CELL.match(ref or "")
    if not m: return None
    r=cell(doc_id, f"t{m.group(1)}-{m.group(2)}-0"); return r[0] if r else None
def header(doc_id, ref):
    m=CELL.match(ref or "")
    if not m: return None
    r=cell(doc_id, f"t{m.group(1)}-0-{m.group(3)}"); return r[0] if r else None
def describe(doc_id, rec):
    if rec["shape"]!="scalar": return f"{rec['shape']}: {(rec['payload'].get('reason') or rec.get('error') or '')[:220]!r}"
    c=rec["cited_ids"][0] if rec["cited_ids"] else None; g=cell(doc_id,c) if c else None
    return f"{rec['payload'].get('value')!r}  cite {c}  cell={g[0] if g else None!r} page={g[1]+1 if g else None} row={label(doc_id,c)!r} col={header(doc_id,c)!r}"
E={}
for f in glob.glob(f"{S}/rerun_*.pipeE.json"):
    d=json.load(open(f))
    for r in d["rows"]: E[(d["doc_key"],r["index"])]=r
out=[]; counts={"same":0,"diff":0,"lost":0,"gained":0,"both_unanswerable":0}
for f in sorted(glob.glob(f"{S}/rerun_*.pipe{V}.json")):
    d=json.load(open(f)); doc_id=d["doc_id"]; t=titles[d["doc_key"]]
    for r in d["rows"]:
        e=E.get((d["doc_key"],r["index"]))
        if e is None: continue
        es,vs=e["shape"],r["shape"]
        if es=="scalar" and vs=="scalar":
            if same_number(e["payload"].get("value"), r["payload"].get("value")): counts["same"]+=1; continue
            kind="DIFF"; counts["diff"]+=1
        elif es=="scalar": kind="LOST"; counts["lost"]+=1
        elif vs=="scalar": kind="GAINED"; counts["gained"]+=1
        else: counts["both_unanswerable"]+=1; continue
        out.append(f"{kind} | {t} [{r['index']}] {r['question']!r}\n   E: {describe(doc_id,e)}\n   {V}: {describe(doc_id,r)}\n   {V} context: {r['context_ids']}")
print(f"{V} vs E: {counts}\n")
print("\n".join(out))
