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

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)

byid = {r["id"]: r for r in regs}
# focus region 35 and 55
for rid in (35, 55):
    if rid in byid:
        r = byid[rid]
        print(f"R{rid} type={r['type']} bbox={r['bbox']}")
    else:
        print(f"R{rid} NOT in regs (dropped?)")

print("\n-- all regions near x 150..750, y 1100..1600 --")
for r in sorted(regs, key=lambda r: r["bbox"][1]):
    b = r["bbox"]
    if b[1] < 1700 and b[3] > 1050 and b[0] < 760:
        print(f"  R{r['id']:>3} {r['type']:<16} bbox={b}")

print("\n-- raw horizontal lines near y=1375 --")
res = detect_separator_lines(png)
for L in sorted(res["h"], key=lambda d: d["y1"]):
    if 1300 < L["y1"] < 1450:
        print(f"  H y={L['y1']} x[{L['x1']}..{L['x2']}] len={L['len']} gray={L['gray']}")

print("\n-- separator_barriers near y=1375 --")
bars = separator_barriers(str(png), regs)
for L in sorted(bars, key=lambda d: d["y"]):
    if 1300 < L["y"] < 1450:
        print(f"  bar y={L['y']} x[{L['x1']}..{L['x2']}] mc={L.get('mc')}")
