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")
png = D / "page_001.png"
regions = json.loads((D / "page_001.regions.json").read_text())["regions"]
print(f"raw regions: {len(regions)}")

# 1) masthead drop
regions2 = se.drop_masthead_regions(regions, "andhra_jyothi", 1, png)
dropped = {r['id'] for r in regions} - {r['id'] for r in regions2}
print(f"masthead dropped ids: {sorted(dropped)}  -> {len(regions2)} regions")

# 2) dateline starts
ds = se.find_article_starts_by_dateline(regions2, str(png), "andhra_jyothi")
print(f"dateline starts: {len(ds)}")
for s in ds:
    print(f"   DL region_id={s['region_id']} headline_id={s.get('headline_id')} bbox={s['bbox']}")

# 3) separator barriers
bars = separator_barriers(str(png), regions2)
print(f"separator barriers: {len(bars)}")

# 4) cluster
blocks = se.cluster_all_article_blocks(regions2, dateline_starts=ds, sep_lines=bars)
print(f"\nblocks: {len(blocks)}")
dl_ids = {s['region_id'] for s in ds}
for b in sorted(blocks, key=lambda d: (d['bbox'][1], d['bbox'][0])):
    mem = b['members']
    has_dl = any(m in dl_ids for m in mem)
    print(f"  block anchor={b['anchor_id']} kind={b['kind']} dl={has_dl} bbox={b['bbox']} members={sorted(mem)}")

# 5) render crops to /tmp for visual check
import shutil
out = Path("output/AndhraJyothi_Siddipet District_20260602_20260603_144919/all_article_crops_test")
if out.exists():
    shutil.rmtree(out)
out.mkdir(parents=True)
n = se.crop_all_article_blocks(str(png), regions2, str(out), 1,
                               dateline_starts=ds, sep_lines=bars)
print(f"\ncrops written: {n} -> {out}/page_001")
import os
for f in sorted(os.listdir(out / "page_001")):
    print("   ", f)
