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 = "namaste_telangana"   # gates paper-specific clustering passes (pipeline sets this too)
RUN=Path(os.environ.get("JG_RUN","output/NamastheTelangana_Jangaon_20260520_20260609_130635")); D=RUN/"pages"
OUT=RUN/"all_article_crops_review"; P="namaste_telangana"
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)
        if pg==1: regs=se.detect_sakshi_headline_bands(regs,png)   # NT: front-page display headlines only
        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)
    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 ===")
    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
        ndl=sum(1 for m in b['members'] if m in dlids)
        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)
        flags=[]
        if ndl>=2: flags.append(f"{ndl}DL(roundup?)")
        if ntext==0 and nimg>0: flags.append("IMG-ONLY(no text)")
        if len(b['members'])==1: flags.append("SINGLE-region")
        if b['kind']=='O': flags.append("ORPHAN")
        print(f"  R{b['anchor_id']:>3} {b['kind']} {'RAW' if b.get('_raw_rect') else '   '} "
              f"bbox=[{x1},{y1},{x2},{y2}] {w}x{h} mem={len(b['members'])} {types} "
              f"{' '.join(flags)}")
    for key in ("Section banner","Containment-merge","Headline-merge","Banner rectangle"):
        c=[l.strip() for l in log.splitlines() if key in l]
        if c: print(f"  [{key}] x{len(c)//2 if key in ('Headline-merge','Containment-merge','Section banner','Banner rectangle') else len(c)}")
print("\nDONE ->",OUT)
