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

D = Path("output/AndhraJyothi_Siddipet District_20260602_20260603_144919/pages")

def headline_above(regions, start_bboxes, body, max_gap):
    cx = (body[0] + body[2]) / 2
    cands = [r for r in regions
             if (r.get("type") == "doc_title" or r.get("_promoted_from"))
             and r["bbox"][0] <= cx <= r["bbox"][2]
             and r["bbox"][3] <= body[1] + 40
             and (body[1] - r["bbox"][3]) <= max_gap]
    if not cands:
        return None, None
    h = max(cands, key=lambda r: r["bbox"][3])
    gap = body[1] - h["bbox"][3]
    for ob in start_bboxes:
        if ob is body:
            continue
        ocx = (ob[0] + ob[2]) / 2
        if h["bbox"][3] < ob[1] < body[1] and cx - 120 <= ocx <= cx + 120:
            return None, gap
    return h["id"], gap

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")
    sb = [s["bbox"] for s in ds]
    print(f"\n==== PAGE {pg} ====")
    for s in ds:
        rid = s["region_id"]
        row = []
        for g in (900, 1000, 1100, 1300):
            hid, gap = headline_above(regions, sb, s["bbox"], g)
            row.append(f"g{g}:R{hid}" )
        print(f"  DL R{rid:>3}  gap_to_nearest_doc_title above -> " + "  ".join(row))
