"""Compare a camelot baseline vs current raw output across all docs.

Reads v1_<name>.json / v2_<name>.json pairs from the review working
directory and reports, per document and per (page, flavor), how the
current version compares to the baseline on table count, grid coverage
(non-empty cells), and detection. Flags every place the current version
produces less than the baseline so nothing regresses silently.

Working directory: $REVIEW_DIR, or argv[1], default .camelot-eval.
"""

import glob
import json
import os
import sys
from collections import defaultdict

EVAL = sys.argv[1] if len(sys.argv) > 1 else os.environ.get("REVIEW_DIR", ".camelot-eval")


def load(prefix, name):
    p = os.path.join(EVAL, f"{prefix}_{name}.json")
    if not os.path.exists(p):
        return None
    return json.load(open(p))


def by_page_flavor(doc):
    agg = defaultdict(lambda: {"n": 0, "nonempty": 0, "cells": 0})
    for t in doc["tables"]:
        k = (t["page"], t["flavor"])
        agg[k]["n"] += 1
        agg[k]["nonempty"] += t["n_nonempty"]
        agg[k]["cells"] += t["n_cells"]
    return agg


def main():
    names = sorted(os.path.basename(p)[3:-5] for p in glob.glob(os.path.join(EVAL, "v2_*.json")))
    regressions = []
    totals = {"v1_tables": 0, "v2_tables": 0, "v1_nonempty": 0, "v2_nonempty": 0}
    print(f"{'document':<48} {'v1 tbl':>7} {'v2 tbl':>7} {'v1 cells':>9} {'v2 cells':>9}  verdict")
    print("-" * 100)
    for name in names:
        v1, v2 = load("v1", name), load("v2", name)
        if v1 is None or v2 is None:
            print(f"{name[:46]:<48} {'MISSING JSON'}")
            continue
        v1ne = sum(t["n_nonempty"] for t in v1["tables"])
        v2ne = sum(t["n_nonempty"] for t in v2["tables"])
        totals["v1_tables"] += v1["n_tables"]
        totals["v2_tables"] += v2["n_tables"]
        totals["v1_nonempty"] += v1ne
        totals["v2_nonempty"] += v2ne

        a1, a2 = by_page_flavor(v1), by_page_flavor(v2)
        doc_reg = []
        for k in sorted(set(a1) | set(a2)):
            page, flavor = k
            n1, n2 = a1[k]["n"], a2[k]["n"]
            ne1, ne2 = a1[k]["nonempty"], a2[k]["nonempty"]
            # Regression: fewer tables, or materially fewer non-empty cells (>5% drop)
            if n2 < n1 or ne2 < ne1 * 0.95:
                doc_reg.append(f"  p{page} {flavor}: tables {n1}->{n2}, nonempty {ne1}->{ne2}")
        verdict = "OK" if not doc_reg else f"REGRESSION ({len(doc_reg)})"
        if v2["n_tables"] > v1["n_tables"]:
            verdict += " +new"
        print(f"{name[:46]:<48} {v1['n_tables']:>7} {v2['n_tables']:>7} " f"{v1ne:>9} {v2ne:>9}  {verdict}")
        if doc_reg:
            regressions.append((name, doc_reg))

    print("-" * 100)
    print(
        f"{'TOTAL':<48} {totals['v1_tables']:>7} {totals['v2_tables']:>7} "
        f"{totals['v1_nonempty']:>9} {totals['v2_nonempty']:>9}"
    )
    print()
    if regressions:
        print(f"REGRESSIONS in {len(regressions)} document(s):")
        for name, rows in regressions:
            print(f"\n{name}")
            for r in rows:
                print(r)
    else:
        print("NO REGRESSIONS: current version meets or exceeds the baseline on every (doc,page,flavor).")


if __name__ == "__main__":
    main()
