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_002.png")
regs = json.loads((D / "page_002.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", 2, 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)
rm = {r["id"]: r for r in regs}
print("BLOCKS:")
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}] mem={sorted(set(b['members']))}{f}")
WATCH = [20, 29, 34, 51, 3, 14, 31, 65, 87, 105, 121, 76, 8, 10, 36, 46, 81]
own = {}
for b in blocks:
    tag = ("F" if b.get("_furniture") else b["kind"]) + str(b["anchor_id"])
    for m in set(b["members"]) | {b["anchor_id"]}:
        own.setdefault(m, []).append(tag)
print("\nWATCH owners:", {f"R{w}": own.get(w, "NONE") for w in WATCH})
print("\nbars y2380-2820 / y3400-3700 / y4640-4760:")
for L in bars:
    if not L.get("vert") and "y" in L and (2380 <= L["y"] <= 2820 or 3400 <= L["y"] <= 3700 or 4640 <= L["y"] <= 4760):
        print(f"  y={L['y']} x={L['x1']}-{L['x2']} w={L['x2']-L['x1']}")
print("\ndatelines:", sorted(s["region_id"] for s in ds))
print("\npipeline lines:")
for l in buf.getvalue().splitlines():
    if any(k in l for k in ("Type-2", "rule-split", "📐", "Masthead", "⇤", "re-home", "📸", "Orphan photo", "carve", "furniture")):
        print(" ", l.strip())
