#!/usr/bin/env python3
"""Resume scan: fetch remaining accounts and merge into existing results."""

import urllib.request
import json
import time
import os
from datetime import datetime, timezone

API_KEY = "get-x-api-6157f8edb420c3d5e50e062287389aa55635cd7c9630c81f"
OUTPUT_DIR = "/home/kiro/trend-engine/trends/x/account-monitor"
DATE_STR = "2026-06-10"
RESULTS_FILE = os.path.join(OUTPUT_DIR, f"scan_results_{DATE_STR}.json")

# Remaining handles after the 34 that already scanned
REMAINING = [
    "GoogleAI", "AI_with_jasmin", "CharaspowerAI", "Ai_Vaidehi",
    "CINEMA505", "AIFrontliner", "AIGuideHQ", "AIWarper",
    "AITECHio", "4aibsc",
]

def fetch_tweets(handle):
    url = f"https://api.getxapi.com/twitter/user/tweets?userName={handle}&count=5"
    req = urllib.request.Request(url, headers={"Authorization": f"Bearer {API_KEY}"})
    try:
        with urllib.request.urlopen(req, timeout=30) as resp:
            body = resp.read().decode("utf-8", errors="replace")
        data = json.loads(body, strict=False)
        tweets = data.get("tweets", data)
        if isinstance(tweets, dict):
            tweets = [tweets]
        elif not isinstance(tweets, list):
            tweets = []
        return tweets, None
    except Exception as e:
        return [], str(e)

def parse_date(created_at):
    if not created_at:
        return ""
    try:
        dt = datetime.strptime(created_at, "%a %b %d %H:%M:%S %z %Y")
        return dt.strftime("%Y-%m-%d")
    except:
        return ""

# Load existing results
with open(RESULTS_FILE) as f:
    all_results = json.loads(f.read(), strict=False)

done_handles = set(a["handle"] for a in all_results.get("accounts", []))

print(f"Resuming scan: {len(REMAINING)} accounts remaining")
print(f"Already scanned: {len(done_handles)}")

for i, handle in enumerate(REMAINING):
    if handle in done_handles:
        print(f"  [{i+1}/{len(REMAINING)}] {handle} — already scanned, skipping")
        continue

    tweets = fetch_tweets(handle)
    if tweets[1]:
        all_results.setdefault("errors", []).append({"handle": handle, "error": tweets[1]})
        print(f"  [{i+1}/{len(REMAINING)}] {handle} — ERROR: {tweets[1][:60]}")
    else:
        tweet_list = tweets[0]
        parsed = []
        for t in tweet_list:
            if not isinstance(t, dict):
                continue
            created_at = t.get("createdAt", "")
            date_str = parse_date(created_at)
            author = t.get("author", {}) or {}
            parsed.append({
                "id": t.get("id", ""),
                "text": t.get("text", ""),
                "createdAt": created_at,
                "date": date_str,
                "likeCount": t.get("likeCount", 0),
                "retweetCount": t.get("retweetCount", 0),
                "replyCount": t.get("replyCount", 0),
                "viewCount": t.get("viewCount", 0),
                "bookmarkCount": t.get("bookmarkCount", 0),
                "authorName": author.get("userName", handle),
                "authorFollowers": author.get("followers", 0),
            })
        all_results.setdefault("accounts", []).append({
            "handle": handle,
            "tweets": parsed,
            "tweet_count": len(parsed),
        })
        print(f"  [{i+1}/{len(REMAINING)}] {handle} — {len(parsed)} tweets")

    tmp_file = RESULTS_FILE + ".tmp"
    with open(tmp_file, "w") as f:
        json.dump(all_results, f, indent=2)

    time.sleep(1)

os.rename(RESULTS_FILE + ".tmp", RESULTS_FILE)

total = len(all_results.get("accounts", []))
errors = len(all_results.get("errors", []))
print(f"\nComplete. {total} accounts total, {errors} errors.")
