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)
    ds = se.find_article_starts_by_dateline(regs, str(png), "andhra_jyothi")

print(f"=== {len(ds)} dateline starts ===")
for s in sorted(ds, key=lambda s: s["bbox"][1]):
    print(f"  DL region={s['region_id']:>3} bbox={s['bbox']} headline={s.get('headline_region')}")

byid = {r["id"]: r for r in regs}
print("\n=== all regions sorted by y ===")
for r in sorted(regs, key=lambda r: (r["bbox"][1], r["bbox"][0])):
    b = r["bbox"]
    print(f"  R{r['id']:>3} {r['type']:<16} bbox={b}")
