import sys, json, contextlib, io
sys.path.insert(0, ".")
from pathlib import Path
import smart_extractor as se
from _lines import separator_barriers

D = Path("output/AndhraJyothi_Siddipet District_20260602_20260603_144919/pages")
png = D / "page_001.png"
regs0 = json.loads((D / "page_001.regions.json").read_text())["regions"]
with contextlib.redirect_stdout(io.StringIO()):
    regs = se.drop_masthead_regions(regs0, "andhra_jyothi", 1, png)
    ds = se.find_article_starts_by_dateline(regs, str(png), "andhra_jyothi")
    bars = separator_barriers(str(png), regs)
    bl = se.cluster_all_article_blocks(regs, dateline_starts=ds, sep_lines=bars)

owner = {}
for b in bl:
    for m in b["members"]:
        owner[m] = b["anchor_id"]
for rid in (76, 68, 46):
    print(f"R{rid} -> block anchor {owner.get(rid)}")

# manual barrier check R32 vs R76/R68
byid = {r["id"]: r for r in regs}
print("bars (full-width-ish) near y4227/4915:", [b for b in bars if b["y"] in (4227,4915) or 4200<b["y"]<4250])
for rid in (76,68):
    print(f"R{rid} bbox={byid[rid]['bbox']}")
