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

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:
    print("NO API KEY")
    raise SystemExit(1)

WATCHLIST = "/home/kiro/trend-engine/trends/x/raw/rising-accounts.md"
with open(WATCHLIST) as f:
    content = f.read()

# Extract handles via regex @(\w+)
raw_handles = re.findall(r"@(\w+)", content)

# Noise words to skip (reference URL link-text, not real handles)
SKIP_WORDS = {'x', 'youtube', 'facebook', 'marketingbrew', 'besttwitteraccounts',
              'theankler', 'xo2', 'etcjournal', 'letsdatascience', 'pixflow',
              'blogs', 'nvidia', 'nofilmschool_com', 'curiousrefuge_com'}

# CORE handles (verified Tier 1/2/3 + rising accounts)
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", "Chandan_Perla",
]

# IRRELEVANT / auto-discovered accounts to filter (still scanned, shown in Filtered section)
IRRELEVANT = [
    "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",
]

core_set = set(CORE)
irrelevant_set = set(IRRELEVANT)

# Build final handle list: core first (preserving order), then irrelevant
handles = []
seen = set()
for h in CORE:
    if h not in seen:
        handles.append(h); seen.add(h)
for h in IRRELEVANT:
    if h not in seen:
        handles.append(h); seen.add(h)

# Also include any other extracted handle that looks like a real account
for h in raw_handles:
    if h in SKIP_WORDS:
        continue
    if h in seen:
        continue
    if h in core_set or h in irrelevant_set:
        continue
    handles.append(h)
    seen.add(h)

print(f"Total handles to scan: {len(handles)}")
print(f"Core: {len(CORE)}, Irrelevant: {len(IRRELEVANT)}")

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 [], f"HTTP {resp.status_code}"
        data = resp.json()
        return data.get("tweets", []), None
    except Exception as e:
        return [], str(e)

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

DATE_FMT = "%a %b %d %H:%M:%S %z %Y"

all_results = {}
failed = []
for handle in handles:
    tweets, err = scan_handle(handle)
    if err:
        failed.append((handle, err))
        print(f"FAIL {handle}: {err}")
    else:
        all_results[handle] = tweets
        print(f"OK {handle}: {len(tweets)} tweets")
    time.sleep(0.8)

today = datetime.now(timezone.utc).strftime("%Y-%m-%d")
out_path = f"/home/kiro/trend-engine/trends/x/scan_results_{today}_FULL.json"
with open(out_path, "w") as f:
    json.dump({"results": all_results, "failed": [x[0] for x in failed]}, f)
print(f"\nSaved full results to {out_path}")

# Score tweets
def parse_date(s):
    try:
        return datetime.strptime(s, DATE_FMT)
    except Exception:
        return None

qualifying = []  # (handle, tweet) for core
filtered_qualifying = []  # for irrelevant handles

for handle, tweets in all_results.items():
    if not tweets:
        continue
    is_core = handle in core_set
    for tw in tweets:
        if not isinstance(tw, dict):
            continue
        # skip retweets of others
        if tw.get("retweeted_tweet"):
            continue
        likes = tw.get("likeCount") or 0
        rts = tw.get("retweetCount") or 0
        replies = tw.get("replyCount") or 0
        views = tw.get("viewCount") or 0
        score = likes + (rts * 2) + (replies * 3) + (views / 100)
        created = tw.get("createdAt") or tw.get("created_at")
        dt = parse_date(created) if created else None
        in_window = dt is not None and dt >= cutoff
        qualifies = likes >= 50 or rts >= 10 or views >= 1000
        if qualifies and in_window:
            entry = {"handle": handle, "tweet": tw, "score": score,
                     "likes": likes, "rts": rts, "replies": replies, "views": views,
                     "dt": dt}
            if is_core:
                qualifying.append(entry)
            else:
                filtered_qualifying.append(entry)

# Sort core qualifying by score desc
qualifying.sort(key=lambda x: -x["score"])

print(f"\nCore qualifying tweets: {len(qualifying)}")
print(f"Filtered qualifying tweets: {len(filtered_qualifying)}")
print(f"Failed handles: {len(failed)}")
for h, e in failed:
    print(f"  - {h}: {e}")
