#!/usr/bin/env python3
"""Scan all watchlist accounts via GetXAPI and save results incrementally."""

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

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")

# All 44 handles extracted from the watchlist
HANDLES = [
    # Tier 1
    "runwayml", "CuriousRefuge", "nofilmschool", "venturetwins", "Kling_ai",
    # Tier 2
    "paultrillo", "c_valenzuelab", "CorridorDigital", "karenxcheng", "higgsfield",
    # Tier 3
    "pika_labs", "EHuanglu",
    # Rising
    "azed_ai", "JeffSynthesized", "ai_filmmaker", "visiblemaker", "Magiermogul",
    "WuxiaRocks", "JiamingSuen", "moonvalley", "higgsfield_ai", "Chandan_Perla",
    # Auto-discovered
    "AITOPIAai", "FaithJaiye2187", "KammelaBala", "MyStageAI",
    "AI_Video_Dumps", "ChrisLaubAI", "GoogleLabs", "TheAIColony", "cladlabs",
    "AIBuzzNews", "AlexStudio44", "DonutAI", "GoogleAI",
    "AI_with_jasmin", "CharaspowerAI",
    "Ai_Vaidehi",
    "CINEMA505",
    "AIFrontliner", "AIGuideHQ",
    "AIWarper",
    "AITECHio",
    "4aibsc",
]

def fetch_tweets(handle):
    """Fetch latest 5 tweets for a handle via GetXAPI."""
    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):
    """Parse Rails-style date string to YYYY-MM-DD."""
    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 if any (for resume support)
all_results = {"accounts": [], "errors": [], "scan_date": f"{DATE_STR} 06:00 UTC"}
done_handles = set(a["handle"] for a in all_results.get("accounts", []))

print(f"Starting scan of {len(HANDLES)} accounts at {datetime.now(timezone.utc).strftime('%H:%M:%S')} UTC")
print(f"Results file: {RESULTS_FILE}")

for i, handle in enumerate(HANDLES):
    if handle in done_handles:
        print(f"  [{i+1}/{len(HANDLES)}] {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(HANDLES)}] {handle} — ERROR: {tweets[1][:60]}")
    else:
        tweet_list = tweets[0]
        # Parse and filter tweets
        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(HANDLES)}] {handle} — {len(parsed)} tweets")

    # Incremental save after each account
    tmp_file = RESULTS_FILE + ".tmp"
    with open(tmp_file, "w") as f:
        json.dump(all_results, f, indent=2)

    time.sleep(1)

# On completion, rename .tmp to final
os.rename(RESULTS_FILE + ".tmp", RESULTS_FILE)

total = len(all_results.get("accounts", []))
errors = len(all_results.get("errors", []))
print(f"\nScan complete. {total} accounts scanned, {errors} errors.")
print(f"Results saved to {RESULTS_FILE}")
