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
RUN=Path("output/AndhraJyothi_Siddipet District_20260602_20260603_144919"); D=RUN/"pages"
OUT=RUN/"all_article_crops_rescue"; PAPER="andhra_jyothi"
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"]   # 0.5 saved
    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,PAPER,pg,png); regs=se.promote_paragraph_titles(regs,png,PAPER)
        bars=separator_barriers(png,regs)
        regs=se.rescue_headlines_above_photos(regs,png,PAPER,bars)     # NEW
        ds=se.find_article_starts_by_dateline(regs,png,PAPER)
        blocks=se.cluster_all_article_blocks(regs,ds,sep_lines=bars)
        n=se.crop_all_article_blocks(png,regs,str(OUT),pg,dateline_starts=ds,sep_lines=bars)
    resc=[l for l in buf.getvalue().splitlines() if "HEADLINE RESCUE" in l]
    print(f"page {pg}: {n} crops | {len(resc)} headline(s) rescued")
    for l in resc: print("   ",l.strip())
print("DONE")
