"""Replay the post-Claude pipeline tail on a saved run's fixtures, WITHOUT any
billable Claude calls. Loads regions.json + political_result.json (already
post-safeguard), then applies ONLY the new steps that change the crop:
  - grow_articles_final (Fix #1 floor threshold + Fix #3 final grow pass)
  - crop_political_articles (Fix #2 column-aware foreign-headline clip)
and writes regenerated crops to a /tmp dir for visual inspection.

Usage: python3 _replay_crop.py <run_dir> <page_num>
"""
import json
import sys
from pathlib import Path

import smart_extractor
from smart_extractor import (
    find_article_starts_by_dateline,
    grow_articles_final,
    crop_political_articles,
)


def paper_of_height(h):
    if 7040 <= h <= 7060:
        return "sakshi"
    if h in (6833, 6549):
        return "andhra_jyothi"
    if h == 6422:
        return "namaste_telangana"
    return "unknown"


def main(run_dir, page_num):
    run = Path(run_dir)
    pages = run / "pages"
    png = pages / f"page_{page_num:03d}.png"
    reg_path = pages / f"page_{page_num:03d}.regions.json"
    pol_path = pages / f"page_{page_num:03d}.political_result.json"

    regions = json.loads(reg_path.read_text())["regions"]
    political = json.loads(pol_path.read_text())["political_articles"]

    from PIL import Image
    paper = paper_of_height(Image.open(png).height)
    print(f"paper={paper}  regions={len(regions)}  articles={len(political)}")

    dateline_starts = find_article_starts_by_dateline(regions, str(png), paper)

    print("\n--- BEFORE grow_articles_final (saved member sets) ---")
    for i, a in enumerate(political, 1):
        m = sorted(a.get("member_region_ids", []) or [])
        print(f"  a{i:03d}: {a.get('headline_english','?')[:38]!r:42} members={m}")

    political = grow_articles_final(political, regions, dateline_starts)

    print("\n--- AFTER grow_articles_final ---")
    for i, a in enumerate(political, 1):
        m = sorted(a.get("member_region_ids", []) or [])
        print(f"  a{i:03d}: {a.get('headline_english','?')[:38]!r:42} members={m}")

    out = Path(f"/tmp/replay_{run.name}_p{page_num:03d}")
    out.mkdir(parents=True, exist_ok=True)
    print(f"\n--- CROP (writing to {out}) ---")
    crop_political_articles(str(png), political, regions, out, page_num)
    print(f"\nCrops written to: {out}")
    for f in sorted(out.glob("*.png")):
        print(f"  {f.name}")


if __name__ == "__main__":
    main(sys.argv[1], int(sys.argv[2]))
