#!/usr/bin/env python3
"""Retry failed handles and merge into the day's scan_results JSON."""
import json, os, sys, time, datetime
import requests

DATE = sys.argv[1]
RAW = "/home/kiro/trend-engine/trends/x/raw"
JSON = os.path.join(RAW, f"scan_results_{DATE}_FULL.json")

env_path = os.path.expanduser("~/.hermes/.env")
API_KEY = None
with open(env_path) as f:
    for line in f:
        if line.startswith("GETXAPI_KEY="):
            API_KEY = line.strip().split("=", 1)[1]
            break
if not API_KEY:
    sys.exit("no GETXAPI_KEY")

data = json.load(open(JSON))
failed = data.get("failed", [])
print("failed handles:", failed)
handles = [f.split(":")[0].strip().lstrip("@") for f in failed]
if not handles:
    print("nothing to retry")
    sys.exit(0)

for h in handles:
    url = f"https://api.getxapi.com/twitter/user/tweets?userName={h}&count=5"
    try:
        r = requests.get(url, headers={"Authorization": f"Bearer {API_KEY}"}, timeout=60)
        if r.status_code != 200:
            print(f"{h}: still failing HTTP {r.status_code}")
            continue
        tw = r.json().get("tweets", [])
        data.setdefault("results", {})[h] = tw
        data.setdefault("raw", {})[h] = tw
        print(f"{h}: recovered {len(tw)} tweets")
    except Exception as e:
        print(f"{h}: retry error {e}")
    time.sleep(1.0)

# drop handles we recovered from the failed list
still = [f for f in failed if f.split(":")[0].strip().lstrip("@") not in data.get("results", {})]
data["failed"] = still
json.dump(data, open(JSON, "w"))
print("remaining failures:", still)
