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")
out={}
for pg in (1,2,3,4):
    png=D/f"page_{pg:03d}.png"
    regs=json.loads((D/f"page_{pg:03d}.regions.json").read_text())["regions"]
    with contextlib.redirect_stdout(io.StringIO()):
        regs=se.drop_masthead_regions(regs,"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)
    out[pg]={b["anchor_id"]:[b["kind"],tuple(sorted(b["members"]))] for b in bl}
tag=sys.argv[1] if len(sys.argv)>1 else "base"
Path(f"scratch_py/_snap_{tag}.json").write_text(json.dumps(out,indent=0))
print(f"saved _snap_{tag}.json")
for pg in (1,2,3,4):
    print(f"PAGE{pg}: {len(out[pg])} blocks  anchors={sorted(out[pg])}")
