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 blocks_for(pg, strip_mc):
    png = D / f"page_{pg:03d}.png"
    regs = json.loads((D / f"page_{pg:03d}.regions.json").read_text())["regions"]
    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)
    if strip_mc:
        bars = [{k: v for k, v in b.items() if k not in ("mc", "anchor")} for b in bars]
    bl = se.cluster_all_article_blocks(regs, dateline_starts=ds, sep_lines=bars)
    return {b["anchor_id"]: tuple(sorted(b["members"])) for b in bl}


for pg in (1, 2, 3, 4):
    with contextlib.redirect_stdout(io.StringIO()):
        cur = blocks_for(pg, False)
        base = blocks_for(pg, True)
    print(f"==== PAGE {pg} ====")
    changed = False
    for k in sorted(set(cur) | set(base)):
        if cur.get(k) != base.get(k):
            changed = True
            print(f"  anchor {k}: base={base.get(k)}")
            print(f"             curr={cur.get(k)}")
    if not changed:
        print("  (no change from mc clamp)")
