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 (2, 3):
    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)
    dl_ids = {s['region_id'] for s in ds}
    print(f"\n==== PAGE {pg}  datelines={len(ds)} bars={len(bars)} blocks={len(blocks)} ====")
    # each dateline must sit in a block that ALSO has a headline/title anchor
    for s in ds:
        rid = s['region_id']
        owner = None
        for b in blocks:
            if rid in b['members'] or rid == b['anchor_id']:
                owner = b; break
        kind = owner['kind'] if owner else '??'
        ndl = sum(1 for m in owner['members'] if m in dl_ids) if owner else 0
        flag = ''
        if owner is None:
            flag = 'LOST'
        elif kind == 'DL':
            flag = 'headless(DL)'
        elif ndl > 1:
            flag = f'MERGED({ndl} datelines)'
        print(f"  DL R{rid} -> block anchor={owner['anchor_id'] if owner else None} kind={kind} {flag}")
