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("output/NamastheTelangana_Siddipet_20260602_20260604_214937"); D=RUN/"pages"
OUT=RUN/"all_article_crops_final"; P="namaste_telangana"
if OUT.exists(): shutil.rmtree(OUT)
for pg in (1,2,3,4):
    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()
    tags={k:sum(1 for l in log.splitlines() if k in l) for k in
          ["Caption bound","Caption orphan dropped","bullet titles flagged","Headline-strip",
           "Floor-trim","Block dateline split","Multi-column body","orphan overlap"]}
    tags={k:(v//2 if k in ("Headline-strip","Floor-trim","Multi-column body","Block dateline split","orphan overlap") else v) for k,v in tags.items()}
    over=[b['anchor_id'] for b in blocks if len([m for m in b['members'] if m in {s['region_id'] for s in ds}])>=2]
    print(f"page {pg}: {n} crops, {len(ds)} datelines, 2+dl-blocks={over or 'none'} | "+", ".join(f"{k}:{v}" for k,v in tags.items() if v))
print("DONE ->",OUT)
