import os, tempfile, contextlib, io, json
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 = "sakshi"
RUN = Path("output/Sakshi_Jangaon_District_20260520_20260609_210104"); D = RUN / "pages"
png = str(D / "page_001.png")
regs = json.loads((D / "page_001.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, "sakshi", 1, png)
    regs = se.drop_classifieds_regions(regs, png); regs = se.tag_legal_notices(regs, png)
    regs = se.promote_paragraph_titles(regs, png, "sakshi")
    regs = se.detect_sakshi_headline_bands(regs, png)
    regs = se.mark_bullet_titles(regs, png)
    ds = se.find_article_starts_by_dateline(regs, png, "sakshi")
    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, "sakshi")
    blocks = se.cluster_all_article_blocks(regs, ds, sep_lines=bars)
WATCH = [44, 16, 36, 69, 61, 46, 23, 4, 74, 49, 17, 88, 33, 28, 3, 53, 56]
own = {}
for b in blocks:
    for m in set(b["members"]) | {b["anchor_id"]}:
        own.setdefault(m, []).append(f"{b['kind']}{b['anchor_id']}")
print("region owners:")
for w in WATCH:
    print(f"  R{w:>3} -> {own.get(w, ['(NONE)'])}")
print("\nhorizontal bars near lift-1 (y3300-4100) and R50 zone (y4350-4900):")
for L in bars:
    if not L.get("vert") and "y" in L and (3300 <= L["y"] <= 4100 or 4350 <= L["y"] <= 4900):
        print(f"  y={L['y']} x={L['x1']}-{L['x2']} w={L['x2']-L['x1']}")
print("\ndateline regions:", sorted(s["region_id"] for s in ds))
print("\npipeline lines mentioning watched regions / passes:")
for l in buf.getvalue().splitlines():
    if any(k in l for k in ("R44", "R69", "R61", "R46", "R23", "rebind", "Sakshi", "Orphan photo", "Masthead", "📐")):
        print(" ", l.strip())
