import os, tempfile, contextlib, io, json, sys, 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

# Replays the POLITICAL crop step (no API): saved political_result.json member ids
# + freshly recomputed region prep chain -> crop_political_articles (block-snap).
RUNS = [
    ("sakshi", Path("output/Sakshi_Jangaon_District_20260520_20260609_210104"), [1]),
    ("andhra_jyothi", Path("output/AndhraJyothi_Siddipet District_20260602_20260603_144919"), [2, 3, 4]),
    ("namaste_telangana", Path("output/NamastheTelangana_Jangaon_20260520_20260608_124720"), [1]),
]
for paper, RUN, pages in RUNS:
    se._ACTIVE_PAPER = paper
    D = RUN / "pages"
    OUT = RUN / "articles_review"
    if OUT.exists():
        shutil.rmtree(OUT)
    for pg in pages:
        png = str(D / f"page_{pg:03d}.png")
        prf = D / f"page_{pg:03d}.political_result.json"
        if not prf.exists() or not os.path.exists(png):
            continue
        political = json.load(open(prf)).get("political_articles", [])
        if not political:
            continue
        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, paper, pg, png)
            regs = se.drop_classifieds_regions(regs, png)
            regs = se.tag_legal_notices(regs, png)
            regs = se.promote_paragraph_titles(regs, png, paper)
            if paper == "sakshi":
                regs = se.detect_sakshi_headline_bands(regs, png)
            regs = se.mark_bullet_titles(regs, png)
            ds = se.find_article_starts_by_dateline(regs, png, paper)
            bars = separator_barriers(png, regs)
            _p = se.cluster_all_article_blocks(regs, ds, sep_lines=bars)
            if se.recover_orphan_text_headlines(_p, regs, png):
                ds = se.find_article_starts_by_dateline(regs, png, paper)
            recs = se.crop_political_articles(png, political, regs, OUT, pg,
                                              dateline_starts=ds, sep_lines=bars)
        print(f"=== {RUN.name} page {pg}: {len(political)} political -> {len(recs)} crops")
        for l in buf.getvalue().splitlines():
            if any(k in l for k in ("Block-snap", "Cropped:", "Clipped", "Floored", "Grew",
                                    "Banner", "block-snap unavailable")):
                print("  ", l.strip())
print("DONE")
