"""Sandbox simulation of the Sakshi Jangaon page-1/2 crop run.
Telugu tesseract is unavailable here, so dateline starts are INJECTED with the
exact anchor set the real 20260609_210104 run found (DL anchors from the saved
crop filenames). Usage:  python3 _sim71.py <tag>
Writes block table to stdout and crops to output/.../_sim_crops_<tag>/."""
import sys, types
sys.modules.setdefault("fitz", types.ModuleType("fitz"))
import os, tempfile, contextlib, io, json, shutil
tempfile.tempdir = os.path.abspath("_tess_tmp"); os.environ["TMPDIR"] = tempfile.tempdir
os.makedirs("_tess_tmp", exist_ok=True)
from pathlib import Path
import smart_extractor as se
from _lines import separator_barriers

TAG = sys.argv[1] if len(sys.argv) > 1 else "base"
RUN = Path("output/Sakshi_Jangaon_District_20260520_20260609_210104"); D = RUN / "pages"
OUT = RUN / f"_sim_crops_{TAG}"; P = "sakshi"
DL_IDS = {1: [71, 11, 4, 3, 9, 5], 2: []}          # real run's headline-less datelines
if OUT.exists(): shutil.rmtree(OUT)

for pg in (1, 2):
    png = str(D / f"page_{pg:03d}.png")
    regs = json.loads((D / f"page_{pg:03d}.regions.json").read_text())["regions"]
    buf = io.StringIO()
    with contextlib.redirect_stdout(buf):
        regs = se.dedup_overlapping_regions(regs)
        regs = se.merge_stacked_title_lines(regs, png)
        regs = se.drop_masthead_regions(regs, P, pg, png)
        regs = se.drop_classifieds_regions(regs, png)
        regs = se.tag_legal_notices(regs, png)
        regs = se.promote_paragraph_titles(regs, png, P)
        regs = se.mark_bullet_titles(regs, png)
        rm = {r["id"]: r for r in regs}
        ds = [{"region_id": i, "dateline": "sim:", "bbox": rm[i]["bbox"],
               "headline_region": None} for i in DL_IDS[pg] if i in rm]
        bars = separator_barriers(png, regs)
        blocks = se.cluster_all_article_blocks(regs, ds, sep_lines=bars)
        n = se.crop_all_article_blocks(png, regs, str(OUT), pg,
                                       dateline_starts=ds, sep_lines=bars)
    print(f"=== page {pg}: {n} crops, {len(ds)} injected datelines ===")
    for b in sorted(blocks, key=lambda b: (b["bbox"][1], b["bbox"][0])):
        x1, y1, x2, y2 = b["bbox"]
        types = {}
        for m in b["members"]:
            t = rm.get(m, {}).get("type", "?"); types[t] = types.get(t, 0) + 1
        print(f"  R{b['anchor_id']:>3} {b['kind']} bbox=[{x1},{y1},{x2},{y2}] "
              f"{x2-x1}x{y2-y1} mem={len(b['members'])} {types}")
    for l in buf.getvalue().splitlines():
        if "Masthead" in l: print(" ", l.strip())
print("DONE ->", OUT)
