"""Re-crop EVERY article on all 4 pages using the EXISTING smart_extractor code
(no Claude / no paid API). Runs the FULL pre-processing chain the real pipeline uses
(dedup -> merge stacked titles -> drop masthead -> promote paragraph-titles -> dateline
scan -> separators -> cluster/crop), so the replay crops match real output. Tesseract
temp files are redirected off /tmp so OCR can read them. Output goes to a fresh dir."""
import os, tempfile, json
tempfile.tempdir = os.path.abspath("_tess_tmp")          # keep Tesseract off /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")
PAGES = RUN / "pages"
OUT   = RUN / "all_article_crops_new"                    # fresh output dir
PAPER = "andhra_jyothi"

for pg in (1, 2, 3, 4):
    png     = str(PAGES / f"page_{pg:03d}.png")
    regions = json.loads((PAGES / f"page_{pg:03d}.regions.json").read_text())["regions"]

    # Full pre-processing chain (matches the real pipeline order):
    regions = se.dedup_overlapping_regions(regions)
    regions = se.merge_stacked_title_lines(regions, png)
    regions = se.drop_masthead_regions(regions, PAPER, pg, png)
    regions = se.promote_paragraph_titles(regions, png, PAPER)        # paragraph_title -> doc_title
    dateline_starts = se.find_article_starts_by_dateline(regions, png, PAPER)
    sep_lines = separator_barriers(png, regions)

    n = se.crop_all_article_blocks(png, regions, str(OUT), pg,
                                   dateline_starts=dateline_starts, sep_lines=sep_lines)
    print(f"page {pg}: {len(dateline_starts)} datelines -> {n} article crop(s) written")

print(f"\nCrops written under: {OUT}")
