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
se._ACTIVE_PAPER = "namaste_telangana"
RUN = Path(os.environ.get("NTA_RUN", "output/NamastheTelangana_Adilabad_20260528_20260610_160056"))
D = RUN / "pages"; P = "namaste_telangana"
OUT = RUN / "all_article_crops_review"
if OUT.exists(): shutil.rmtree(OUT)
pages = sorted(int(p.stem.split("_")[1]) for p in D.glob("page_*.png"))
for pg in pages:
    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)
        if pg == 1: regs = se.detect_sakshi_headline_bands(regs, png)   # NT: front-page only
        regs = se.mark_bullet_titles(regs, png)
        ds = se.find_article_starts_by_dateline(regs, png, P)
        bars = separator_barriers(png, regs)
        _p = se.cluster_all_article_blocks(regs, ds, sep_lines=bars)
        if se.recover_orphan_text_headlines(_p, regs, png):
            ds = se.find_article_starts_by_dateline(regs, png, P)
        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, overlay=True)
    print(f"=== page {pg}: {n} crops, {len(ds)} datelines ===")
    for b in sorted(blocks, key=lambda b: (b["bbox"][1], b["bbox"][0])):
        x1, y1, x2, y2 = b["bbox"]
        f = " FURN" if b.get("_furniture") else ""
        print(f"  {b['kind']}{b['anchor_id']:<4} [{x1},{y1},{x2},{y2}] {x2-x1}x{y2-y1} mem={len(b['members'])}{f}")
    for l in sorted({l.strip() for l in buf.getvalue().splitlines()
                     if "rule-split" in l or "Masthead" in l or "Orphan photo" in l}):
        print("  ", l)
print("DONE ->", OUT)
