import sys, json
sys.path.insert(0, "/Users/kranthikumar/Documents/telugu_extractor")
from pathlib import Path
import smart_extractor as se
from _lines import separator_barriers

D = Path("output/AndhraJyothi_Siddipet District_20260602_20260603_144919/pages")
for pg in (1, 2, 3, 4):
    png = D / f"page_{pg:03d}.png"
    regions = json.loads((D / f"page_{pg:03d}.regions.json").read_text())["regions"]
    regions = se.drop_masthead_regions(regions, "andhra_jyothi", pg, png)
    ds = se.find_article_starts_by_dateline(regions, str(png), "andhra_jyothi")
    bars = separator_barriers(str(png), regions)
    blocks = se.cluster_all_article_blocks(regions, dateline_starts=ds, sep_lines=bars)
    rmap = {r["id"]: r for r in regions}
    print(f"\n==== PAGE {pg} ====")
    for b in blocks:
        anchor = rmap.get(b["anchor_id"])
        if not anchor:
            continue
        atop = anchor["bbox"][1]
        # members whose ENTIRE box is above the anchor headline top
        above_mems = [m for m in b["members"]
                      if m != b["anchor_id"] and rmap[m]["bbox"][3] <= atop + 20]
        if above_mems and b["kind"] == "H":
            print(f"  block H anchor={b['anchor_id']} (top y={atop}) has {len(above_mems)} member(s) ABOVE it:")
            for m in above_mems:
                rb = rmap[m]["bbox"]
                print(f"      R{m} {rmap[m]['type']:<14} y[{rb[1]}..{rb[3]}] x[{rb[0]}..{rb[2]}]")
