#!/usr/bin/env python3
"""Scratch driver: read JSON list of {post_id, session, text} from argv[1] and
run link_edit_helper mark-edited + dm_short_links backfill-post for each.
Gitignored scratch helper for the reddit link-edit run."""
import json, subprocess, sys, os

HERE = os.path.dirname(os.path.abspath(__file__))
items = json.load(open(sys.argv[1]))
for it in items:
    pid = str(it["post_id"]); sess = it["session"]; text = it["text"]
    src = it.get("source", "plain_url_ab_skip")
    r1 = subprocess.run([sys.executable, os.path.join(HERE, "link_edit_helper.py"),
                         "mark-edited", "--post-id", pid, "--content", text, "--source", src],
                        capture_output=True, text=True)
    r2 = subprocess.run([sys.executable, os.path.join(HERE, "dm_short_links.py"),
                         "backfill-post", "--minted-session", sess, "--post-id", pid],
                        capture_output=True, text=True)
    bf = (r2.stdout or "").strip().splitlines()[-1:] or [""]
    print(f"post {pid}: mark_edited_rc={r1.returncode} backfill={bf[0]}"
          + (f" ERR1={r1.stderr.strip()}" if r1.returncode else ""))
