import requests, json, os, time
from datetime import datetime, timezone, timedelta

API_KEY = "get-x-api-6157f8edb420c3d5e50e062287389aa55635cd7c9630c81f"
DATE_STR = "2026-06-21"
OUT_DIR = "/home/kiro/trend-engine/trends/x/account-monitor"
RESULTS_FILE = os.path.join(OUT_DIR, f"scan_results_{DATE_STR}_CHUNK1.json")
CUTOFF = datetime.now(timezone.utc) - timedelta(days=3)

HANDLES = [
    "runwayml", "CuriousRefuge", "nofilmschool", "venturetwins", "Kling_ai",
    "paultrillo", "c_valenzuelab", "CorridorDigital", "karenxcheng", "higgsfield",
    "pika_labs", "EHuanglu", "azed_ai", "JeffSynthesized", "ai_filmmaker",
    "visiblemaker", "Magiermogul", "WuxiaRocks", "JiamingSuen", "moonvalley",
    "higgsfield_ai", "Chandan_Perla"
]

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

def fetch_tweets(handle):
    url = f"https://api.getxapi.com/twitter/user/tweets?userName={handle}&count=5"
    headers = {"Authorization": f"Bearer {API_KEY}"}
    try:
        resp = requests.get(url, headers=headers, timeout=30)
        resp.raise_for_status()
        data = resp.json()
        if isinstance(data, list):
            return data
        elif isinstance(data, dict):
            tweets = data.get("tweets", data.get("data", []))
            if isinstance(tweets, dict):
                return [tweets]
            return tweets if isinstance(tweets, list) else []
    except Exception as e:
        return {"error": str(e)}

# Load existing results for resume support
all_results = {"accounts": [], "errors": [], "scan_date": f"{DATE_STR} 06:00 UTC", "chunk": 1}
done_handles = set()

if os.path.exists(RESULTS_FILE):
    with open(RESULTS_FILE) as f:
        all_results = json.load(f)
    done_handles = set(a["handle"] for a in all_results["accounts"])
    # Also include error handles
    for e in all_results.get("errors", []):
        done_handles.add(e["handle"])

for i, handle in enumerate(HANDLES):
    if handle in done_handles:
        continue
    
    data = fetch_tweets(handle)
    
    if isinstance(data, dict) and "error" in data:
        all_results["errors"].append({"handle": handle, "error": data["error"]})
        # Save incrementally
        with open(RESULTS_FILE + ".tmp", "w") as f:
            json.dump(all_results, f, indent=2)
        time.sleep(1)
        continue
    
    if not data or not isinstance(data, list):
        all_results["errors"].append({"handle": handle, "error": "no data or non-list response"})
        with open(RESULTS_FILE + ".tmp", "w") as f:
            json.dump(all_results, f, indent=2)
        time.sleep(1)
        continue
    
    account_tweets = []
    for tweet in data:
        if not isinstance(tweet, dict):
            continue
        created_at = tweet.get("createdAt", "")
        dt = parse_date(created_at)
        if dt and dt < CUTOFF:
            continue  # skip old tweets
        
        like_count = tweet.get("likeCount", 0) or 0
        retweet_count = tweet.get("retweetCount", 0) or 0
        reply_count = tweet.get("replyCount", 0) or 0
        view_count = tweet.get("viewCount", 0) or 0
        bookmark_count = tweet.get("bookmarkCount", 0) or 0
        
        # Check thresholds
        if like_count < 50 and retweet_count < 10 and view_count < 1000:
            continue
        
        # Get author info
        author = tweet.get("author", {})
        author_name = handle
        if isinstance(author, dict):
            author_name = author.get("userName", handle) or handle
        
        tweet_id = tweet.get("id", "")
        text = tweet.get("text", "")
        # Truncate text for snippet
        text_snippet = text[:80].replace("\n", " ").strip()
        if len(text) > 80:
            text_snippet += "..."
        
        score = like_count + (retweet_count * 2) + reply_count + (view_count // 100)
        parsed_dt = parse_date(created_at)
        tweet_date = parsed_dt.strftime("%Y-%m-%d") if parsed_dt else ""
        
        account_tweets.append({
            "handle": handle,
            "author_name": author_name,
            "tweet_id": str(tweet_id),
            "text": text_snippet,
            "full_text": text[:200],
            "score": score,
            "likes": like_count,
            "retweets": retweet_count,
            "replies": reply_count,
            "views": view_count,
            "bookmarks": bookmark_count,
            "date": tweet_date,
            "createdAt": created_at
        })
    
    all_results["accounts"].append({
        "handle": handle,
        "tweets": account_tweets,
        "qualifying_count": len(account_tweets)
    })
    
    # Save incrementally
    with open(RESULTS_FILE + ".tmp", "w") as f:
        json.dump(all_results, f, indent=2)
    
    time.sleep(1)

# Finalize
if os.path.exists(RESULTS_FILE + ".tmp"):
    os.rename(RESULTS_FILE + ".tmp", RESULTS_FILE)

# Print summary
total_qualifying = sum(a["qualifying_count"] for a in all_results["accounts"])
print(f"CHUNK1 done: {len(all_results['accounts'])} accounts, {total_qualifying} qualifying tweets, {len(all_results['errors'])} errors")
