"""Summarize the benchmark: for each engine, pages opened, text recall against Chrome (same run, same page),
snapshot size in Claude tokens, time to network idle and memory. Recall uses the same word counts as the
October 17 Lightpanda post: visible body text, scripts and styles removed."""
import collections, json, pathlib, re, statistics as S
from bs4 import BeautifulSoup

OUT = pathlib.Path("out")
BLOCK = re.compile(r"access denied|attention required|just a moment|verify you are (a )?human|are you a robot|press & hold|captcha|request blocked|unusual traffic|you have been blocked|pardon our interruption", re.I)


def words(html):
    s = BeautifulSoup(html or "", "html.parser")
    for t in s(["script", "style", "noscript", "template"]):
        t.decompose()
    text = s.body.get_text(" ") if s.body else ""
    return collections.Counter(re.findall(r"\w+", text.lower())), text


def load(run, engine, i):
    d = OUT / run / engine
    row = json.loads((d / f"{i:02d}.json").read_text())
    c, text = words((d / f"{i:02d}.html").read_text())
    row["words"] = sum(c.values())
    row["blocked"] = bool(BLOCK.search(text[:3000])) or bool(BLOCK.search(row.get("title", "")))
    return row, c


def summarize(runs, engines, n=50):
    rows = []
    for run in runs:
        for i in range(1, n + 1):
            ref, rc = load(run, "chrome", i)
            for e in engines:
                row, c = load(run, e, i)
                row["run"] = run
                total = sum(rc.values())
                row["recall"] = round(sum((rc & c).values()) / total, 3) if total >= 50 and not ref["blocked"] else None
                row["ref_ok"] = total >= 50 and not ref["blocked"]
                rows.append(row)
    return rows


if __name__ == "__main__":
    import sys
    runs = sys.argv[1:] or ["r1", "r2"]
    rows = summarize(runs, ["chrome", "lightpanda", "obscura"])
    json.dump(rows, open("summary_rows.json", "w"), indent=1)
    for e in ("chrome", "lightpanda", "obscura"):
        rs = [r for r in rows if r["engine"] == e]
        scored = [r for r in rs if r["ref_ok"]]
        good = [r for r in scored if r["recall"] is not None and r["recall"] >= 0.8]
        print(f"{e:<11} pages {len(rs):>3} | blocked {sum(r['blocked'] for r in rs):>3} | scored {len(scored):>3} | recall>=0.8 {len(good):>3} | "
              f"median recall {S.median([r['recall'] for r in scored]) if scored else '-'} | median ready {S.median(r['t_ready'] for r in rs):.1f}s | "
              f"median snap {S.median(r['snap_chars'] for r in rs):.0f} chars | median mem {S.median(r['rss_mb'] for r in rs):.0f} MB")
