#!/usr/bin/env python3
# Watch for the recycle payout to complete after funding. Exit 0 when a recycle flips to
# status 'paid' with a payout_tx; exit 3 on timeout.
import json, time, sys
P = "/home/paul/Aura/data/ledger/recycle.json"
LOGP = "/tmp/claude-1000/-home-paul-Aura/a2239ea0-e18d-4178-9864-2c3f48478801/scratchpad/payout_confirm_watch.log"
def log(m):
    with open(LOGP,"a") as f: f.write(f"{time.strftime('%H:%M:%S')} {m}\n")
log("PAYOUT CONFIRM WATCH START")
deadline = time.time() + 6*60
while time.time() < deadline:
    time.sleep(25)
    try:
        d = json.load(open(P))
    except Exception:
        continue
    for r in d.get("recycles", []):
        if r.get("status") == "paid" and r.get("payout_tx"):
            log(f"PAID — {r.get('payout_usd')} USDC -> {r.get('wallet')} tx {r.get('payout_tx')}")
            sys.exit(0)
        if r.get("status") in ("review","error"):
            log(f"STUCK — status={r.get('status')} {r.get('payout_usd')} -> {r.get('wallet')} note={r.get('note','')}")
            sys.exit(2)
log("TIMEOUT — payout not completed in 6 min (check pay cron / desk balance)")
sys.exit(3)
