import sys, types; sys.modules.setdefault("fitz", types.ModuleType("fitz"))
import os, tempfile, json
tempfile.tempdir = os.path.abspath("_tess_tmp"); os.environ["TMPDIR"]=tempfile.tempdir
os.makedirs("_tess_tmp", exist_ok=True)
from pathlib import Path
import smart_extractor as se
from _lines import separator_barriers
RUN = Path("output/Sakshi_Jangaon_District_20260520_20260609_210104")
png = str(RUN/"pages/page_001.png")
regions = json.loads((RUN/"pages/page_001.regions.json").read_text())["regions"]
regions = se.dedup_overlapping_regions(regions)
regions = se.merge_stacked_title_lines(regions, png)
regions = se.drop_masthead_regions(regions, "sakshi", 1, png)
regions = se.promote_paragraph_titles(regions, png, "sakshi")
dls = se.find_article_starts_by_dateline(regions, png, "sakshi")
seps = separator_barriers(png, regions)
blocks = se.cluster_all_article_blocks(regions, dls, sep_lines=seps)
rmap = {r['id']:r for r in regions}
for b in blocks:
    if b['anchor_id']==71:
        print("BLOCK:", {k:v for k,v in b.items() if k!='members'})
        print("members:")
        for m in sorted(b['members']+[b['anchor_id']], key=lambda m: rmap[m]['bbox'][1] if m in rmap else 0):
            r = rmap.get(m)
            print("  ", m, r['type'] if r else '?', r['bbox'] if r else '?')
json.dump({'blocks':[{'anchor':b['anchor_id'],'kind':b['kind'],'bbox':b['bbox'],'members':b['members']} for b in blocks]}, open('/tmp/blocks71.json','w'))
