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

# --- API key from .env ---
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].strip()
            break
if not API_KEY:
    print("NO API KEY", file=sys.stderr)
    sys.exit(1)

# --- Watchlist ---
wl = "/home/kiro/trend-engine/trends/x/raw/rising-accounts.md"
with open(wl) as f:
    text = f.read()

handles = re.findall(r"@(\w+)", text)
# de-dup preserving order
seen = set()
ordered = []
for h in handles:
    if h not in seen:
        seen.add(h)
        ordered.append(h)

SKIP_WORDS = {
    'x', 'youtube', 'facebook', 'marketingbrew', 'theankler', 'xo2',
    'pixflow', 'besttwitteraccounts', 'blogs', 'nvidia', 'etcjournal',
    'letsdatascience', 'with_replies', 'lang', 'en', 'highlights',
    'curiousrefuge',  # placeholder replaced below -- keep CuriousRefuge
}
# IMPORTANT: nofilmschool + CuriousRefuge are Tier 1, must NOT be skipped.
SKIP_WORDS.discard('curiousrefuge')
SKIP_WORDS.discard('nofilmschool')

IRRELEVANT = {
    # AI news aggregators
    'AIBuzzNews', 'AIGuideHQ', 'AITECHio', 'AIFrontliner', 'AIWarper', 'TheAIColony', 'AITOPIAai',
    # Non-filmmaking auto-discoveries
    'GoogleLabs', 'GoogleAI', 'DonutAI', 'cladlabs', '4aibsc', 'FaithJaiye2187', 'KammelaBala', 'MyStageAI', 'AlexStudio44',
    # Content repost accounts
    'AI_Video_Dumps', 'AI_with_jasmin', 'Ai_Vaidehi', 'CharaspowerAI', 'ChrisLaubAI',
    # Other non-core
    'CINEMA505', 'Chandan_Perla',
}

CORE = [
    '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',
]

final_handles = []
for h in ordered:
    if h in SKIP_WORDS:
        continue
    final_handles.append(h)

print(f"TOTAL EXTRACTED: {len(ordered)}")
print(f"FINAL TO SCAN: {len(final_handles)}")
print("HANDLES:", final_handles)

# --- Scan ---
def scan_handle(handle):
    url = f"https://api.getxapi.com/twitter/user/tweets?userName={handle}&count=5"
    try:
        resp = requests.get(url, headers={"Authorization": f"Bearer {API_KEY}"}, timeout=60)
        if resp.status_code != 200:
            return None, f"HTTP {resp.status_code}"
        data = resp.json()
        tweets = data.get("tweets", [])
        return tweets, None
    except Exception as e:
        return None, str(e)

cutoff = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=3)

all_results = {}
failed = []
for h in final_handles:
    tweets, err = scan_handle(h)
    if err:
        failed.append((h, err))
        time.sleep(0.8)
        continue
    qualified = []
    for t in tweets:
        if t.get("retweeted_tweet"):
            continue  # skip RTs
        likes = t.get("likeCount") or 0
        rts = t.get("retweetCount") or 0
        reps = t.get("replyCount") or 0
        views = t.get("viewCount") or 0
        created = t.get("createdAt")
        dt = None
        if created:
            try:
                dt = datetime.strptime(created, "%a %b %d %H:%M:%S %z %Y")
            except Exception:
                dt = None
        if dt is None:
            continue
        if dt < cutoff:
            continue
        if likes >= 50 or rts >= 10 or views >= 1000:
            score = likes + (rts * 2) + (reps * 3) + (views / 100)
            qualified.append({
                "handle": h,
                "id": t.get("id"),
                "text": (t.get("text") or "").replace("\n", " ")[:160],
                "likes": likes, "rts": rts, "reps": reps, "views": views,
                "score": score, "date": dt.strftime("%Y-%m-%d"),
            })
    all_results[h] = qualified
    time.sleep(0.8)

today = datetime.now(timezone.utc).strftime("%Y-%m-%d")
out = {"results": all_results, "failed": failed}
with open(f"scan_results_{today}_FULL.json", "w") as f:
    json.dump(out, f)

# --- Report ---
top = []
filtered = []
inactive = []
for h, q in all_results.items():
    if h in IRRELEVANT:
        if q:
            filtered.extend(q)
        continue
    if not q:
        inactive.append(h)
        continue
    for t in q:
        t["irrelevant"] = False
        top.append(t)

top.sort(key=lambda x: -x["score"])
filtered.sort(key=lambda x: -x["score"])

print("\n===== REPORT =====")
print(f"FAILED: {failed}")
print(f"INACTIVE (no qualifying tweets): {inactive}")
print(f"\nTOP candidates: {len(top)}")
print(f"FILTERED candidates: {len(filtered)}")

# Save a compact report too
report = {
    "top": top[:10],
    "filtered": filtered,
    "inactive": inactive,
    "failed": failed,
}
with open(f"report_{today}.json", "w") as f:
    json.dump(report, f)

for i, t in enumerate(top[:10], 1):
    print(f"{i}. @{t['handle']} | views={t['views']} likes={t['likes']} rts={t['rts']} reps={t['reps']} | {t['date']} | https://x.com/{t['handle']}/status/{t['id']}")
    print(f"   {t['text']}")

print("\n--- FILTERED (not in top 10) ---")
for t in filtered:
    print(f"@{t['handle']} | views={t['views']} | {t['date']} | https://x.com/{t['handle']}/status/{t['id']}")
