import os,tempfile,contextlib,io,json,shutil
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
se._ACTIVE_PAPER = "sakshi"     # gate Sakshi-specific clustering passes
RUN=Path(os.environ.get("SAK_RUN","output/Sakshi_Jangaon_District_20260520_20260609_210104")); D=RUN/"pages"
OUT=RUN/"all_article_crops_review"; P="sakshi"
if OUT.exists(): shutil.rmtree(OUT)
for pg in (1,2):
    png=str(D/f"page_{pg:03d}.png")
    regs=json.loads((D/f"page_{pg:03d}.regions.json").read_text())["regions"]
    buf=io.StringIO()
    with contextlib.redirect_stdout(buf):
        regs=se.dedup_overlapping_regions(regs); regs=se.merge_stacked_title_lines(regs,png)
        regs=se.drop_masthead_regions(regs,P,pg,png); regs=se.drop_classifieds_regions(regs,png); regs=se.tag_legal_notices(regs,png); regs=se.promote_paragraph_titles(regs,png,P)
        regs=se.detect_sakshi_headline_bands(regs,png)
        regs=se.mark_bullet_titles(regs,png)
        ds=se.find_article_starts_by_dateline(regs,png,P)
        bars=separator_barriers(png,regs)
        _probe=se.cluster_all_article_blocks(regs,ds,sep_lines=bars)
        if se.recover_orphan_text_headlines(_probe,regs,png): ds=se.find_article_starts_by_dateline(regs,png,P)
        blocks=se.cluster_all_article_blocks(regs,ds,sep_lines=bars)
        se.draw_boundary_map_debug(png,regs,D,pg,dateline_starts=ds,sep_lines=bars)
        n=se.crop_all_article_blocks(png,regs,str(OUT),pg,dateline_starts=ds,sep_lines=bars,overlay=True)
    log=buf.getvalue()
    rm={r['id']:r for r in regs}; dlids={s['region_id'] for s in ds}
    print(f"\n=== page {pg}: {n} crops, {len(ds)} datelines ===")
    img_orphans=[]
    for b in sorted(blocks,key=lambda b:(b['bbox'][1],b['bbox'][0])):
        x1,y1,x2,y2=b['bbox']; w,h=x2-x1,y2-y1
        types={}
        for m in b['members']:
            t=rm.get(m,{}).get('type','?'); types[t]=types.get(t,0)+1
        ntext=types.get('text',0); nimg=types.get('image',0)
        is_art=any(rm.get(m,{}).get('type') in ('doc_title','paragraph_title') for m in b['members']) or any(m in dlids for m in b['members'])
        flags=[]
        if nimg>0 and not is_art: flags.append("IMG-ORPHAN?")
        if b['kind']=='O': flags.append("ORPHAN")
        if nimg>0 and not is_art and ntext==0: img_orphans.append(b['anchor_id'])
        print(f"  R{b['anchor_id']:>3} {b['kind']} bbox=[{x1},{y1},{x2},{y2}] {w}x{h} mem={len(b['members'])} {types} {' '.join(flags)}")
    binds=sorted({l.strip() for l in log.splitlines()
                  if "Orphan photo" in l or "Masthead" in l or "📐" in l or "rule-split" in l})
    print(f"  >>> image-only orphans REMAINING: {img_orphans}")
    for l in binds: print(f"  {l}")
print("\nDONE ->",OUT)
