import sys, json, shutil, os
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"]
regions2 = se.drop_masthead_regions(regions, "andhra_jyothi", 1, png)
ds = se.find_article_starts_by_dateline(regions2, str(png), "andhra_jyothi")
bars = separator_barriers(str(png), regions2)
blocks = se.cluster_all_article_blocks(regions2, dateline_starts=ds, sep_lines=bars)

dl_ids = {s["region_id"] for s in ds}
dl_anchors = {b["anchor_id"] for b in blocks if any(m in dl_ids for m in b["members"])}
print(f"{len(ds)} datelines -> {len(dl_anchors)} dateline blocks: {sorted(dl_anchors)}")
for b in sorted(blocks, key=lambda d: (d["bbox"][1], d["bbox"][0])):
    if b["anchor_id"] in dl_anchors:
        print(f"  {b['kind']} anchor={b['anchor_id']:>3} bbox={b['bbox']} members={sorted(b['members'])}")

out = Path("output/AndhraJyothi_Siddipet District_20260602_20260603_144919/dateline_crops")
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,
                               only_anchor_ids=dl_anchors)
print(f"\ndateline crops written: {n}")
for f in sorted(os.listdir(out / "page_001")):
    print("   ", f)
