import sys, json, contextlib, io
sys.path.insert(0,".")
from pathlib import Path
import smart_extractor as se
from _lines import separator_barriers
D=Path("output/AndhraJyothi_Siddipet District_20260602_20260603_144919/pages")
def cx(b): return (b[0]+b[2])/2
for pg in (1,2,3,4):
    png=D/f"page_{pg:03d}.png"
    regs0=json.loads((D/f"page_{pg:03d}.regions.json").read_text())["regions"]
    with contextlib.redirect_stdout(io.StringIO()):
        regs=se.drop_masthead_regions(regs0,"andhra_jyothi",pg,png)
        ds=se.find_article_starts_by_dateline(regs,str(png),"andhra_jyothi")
        bars=separator_barriers(str(png),regs)
        bl=se.cluster_all_article_blocks(regs,dateline_starts=ds,sep_lines=bars)
    tws=sorted(r["bbox"][2]-r["bbox"][0] for r in regs if r.get("type")=="text")
    pitch=tws[len(tws)//2] if tws else 0
    dl=set(s["region_id"] for s in ds)
    docids=set(r["id"] for r in regs if r.get("type")=="doc_title")
    hard=dl|docids
    vsep=[L for L in bars if L.get("vert")]
    print(f"== PAGE{pg} pitch={pitch} vrules={len(vsep)} ==")
    for blk in bl:
        b=blk["bbox"]; w=b[2]-b[0]
        if blk["kind"]=="H" and w>=1.6*pitch:
            lo,hi=b[2],b[2]+int(1.4*pitch)
            col=[r for r in regs if r.get("type") not in ("header","number") and lo+5<=cx(r["bbox"])<=hi]
            hardhit=[r["id"] for r in col if r["id"] in hard]
            print(f"  banner R{blk['anchor_id']} bbox={b} w={w} (~{w/pitch:.1f}col) rightcol_regs={[r['id'] for r in col]} hard={hardhit}")
