#!/usr/bin/env python3
"""X account monitor deep pass 2026-10-21: fetch windows, buckets, dormancy, arrival check."""
import json, re, os
from datetime import datetime, timezone, timedelta

BASE = "/home/kiro/trend-engine/trends/x/raw"
DATE = "2026-10-21"
d = json.load(open(f"{BASE}/scan_results_{DATE}_FULL.json"))
print("TOP KEYS:", list(d.keys()))
print("failures:", repr(d.get("failures")))

raw = d["raw"]
qual = d["qualifying"]
print("raw handles:", len(raw), "qualifying:", len(qual))

IRRELEVANT = {"AIBuzzNews","AIGuideHQ","AITECHio","AIFrontliner","TheAIColony","AITOPIAai",
 "AISuperDomain","GoogleLabs","GoogleAI","DonutAI","cladlabs","FaithJaiye2187","KammelaBala",
 "MyStageAI","AlexStudio44","AI_with_jasmin","Ai_Vaidehi","ChrisLaubAI","OpenAI","AnthropicAI",
 "CINEMA505","AIScottAdams","AI_Jasonyu","AIByLeo"}
FILM = {"DiscussingFilm","nofilmschool"}
LABS = {"OpenAI","AnthropicAI"}

def pdt(s):
    return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")

now = datetime.now(timezone.utc)
cutoff = datetime(now.year, now.month, now.day, tzinfo=timezone.utc) - timedelta(days=3)
print("cutoff(3d midnight):", cutoff.strftime("%Y-%m-%d %H:%M"), " now:", now.strftime("%Y-%m-%d %H:%M"))

# ---------- fetch windows ----------
print("\n===== FETCH WINDOWS (parsed datetimes) =====")
win = {}
for h, items in sorted(raw.items()):
    dts = [pdt(i["createdAt"]) for i in items if i.get("createdAt")]
    if not dts:
        win[h] = None; continue
    mn, mx = min(dts), max(dts)
    inwin = sum(1 for x in dts if x >= cutoff)
    age_h = (now - mx).total_seconds()/3600
    win[h] = dict(n=len(dts), mn=mn, mx=mx, inwin=inwin, age=age_h)
    flag = ""
    if age_h > 24: flag += " STALE>24h"
    if inwin < len(dts): flag += f" [{len(dts)-inwin} rolled out]"
    print(f"{h:18s} n={len(dts):2d} inwin={inwin:2d} span {mn:%m-%d %H:%M} -> {mx:%m-%d %H:%M} age={age_h:5.1f}h{flag}")

# ---------- buckets ----------
core, film_, irr, labs = [], [], [], []
for q in qual:
    h = q["handle"]
    if h in LABS: labs.append(q)
    elif h in FILM: film_.append(q)
    elif h in IRRELEVANT: irr.append(q)
    else: core.append(q)
print(f"\n===== BUCKET COUNTS =====\ntotal={len(qual)} core={len(core)} film={len(film_)} irrelevant={len(irr)} labs={len(labs)}")

AI_TERMS = re.compile(r"\b(ai|a\.i\.|genai|gen-?ai|sora|veo|seedance|kling|runway|midjourney|higgsfield|"
                      r"pika|minimax|wan ?3|wan3|gemini|gpt|claude|opus|deepseek|veo|flux|comfyui|"
                      r"stable diffusion|diffusion|text-to-video|image-to-video|generative|vfx|"
                      r"prompt|prompts|3d|animation|model)\b", re.I)
print("\n===== CORE TOP 12 =====")
core.sort(key=lambda x: -x["score"])
for i, q in enumerate(core[:12], 1):
    print(f"{i:2d}|@{q['handle']:16s}|{q['likes']}L|{q['rt']}RT|{q['replies']}R|{q['views']}V|{q['date']}|{q['score']:.0f}")
    print(f"    {q['text'][:180].replace(chr(10),' ')}")
    print(f"    {q['url']}")
print("\nrank 11 score:", core[10]["score"], "rank 10 score:", core[9]["score"])

print("\n===== FILM BUCKET (AI-relevant only) =====")
film_ai = [q for q in film_ if AI_TERMS.search(q["text"] or "")]
print(f"{len(film_ai)} of {len(film_)} AI-relevant")
for q in film_ai:
    print(f"@{q['handle']}|{q['likes']}L|{q['views']}V|{q['date']}|{q['text'][:200]}")
    print(f"  {q['url']}")

print("\n===== IRRELEVANT AI-RELEVANT =====")
irr_ai = [q for q in irr if AI_TERMS.search(q["text"] or "")]
print(f"{len(irr_ai)} of {len(irr)}")
for q in irr_ai[:20]:
    print(f"@{q['handle']:14s}|{q['likes']}L|{q['views']}V|{q['date']}|{q['score']:.0f}|{q['text'][:120]}")
    print(f"  {q['url']}")

print("\n===== ARRIVAL CHECK (core top 10 handles) =====")
for q in core[:10]:
    tag = "CORE" if q["handle"] not in IRRELEVANT|FILM else "MISROUTED"
    print(f"@{q['handle']:16s} {tag}")

# ---------- dormancy (originals only) ----------
print("\n===== DORMANCY (originals only, RT-filtered) =====")
qual_handles = {q["handle"] for q in qual}
for h in sorted(raw.keys()):
    items = raw[h]
    origs = [i for i in items if not i.get("retweeted_tweet") and not str(i.get("text","")).startswith("RT @")]
    odts = [pdt(i["createdAt"]) for i in origs if i.get("createdAt")]
    newest = max(odts) if odts else None
    inwin_orig = [i for i in origs if pdt(i["createdAt"]) >= cutoff]
    zero = h not in qual_handles
    if zero or h in {"WuxiaRocks","CuriousRefuge","paultrillo","karenxcheng","Chandan_Perla","JeffSynthesized","AI__TSUBAKI","higgsfield_ai","higgsfield","runwayml"}:
        best = ""
        if inwin_orig:
            best = max(inwin_orig, key=lambda i: i.get("likeCount",0))
            best = f"best_inwin={best.get('likeCount')}L/{best.get('viewCount')}V"
        nw = f"{newest:%Y-%m-%d %H:%M}" if newest else "none"
        days = f"{(now-newest).days}d" if newest else "-"
        print(f"{h:18s} zero={str(zero):5s} origs={len(origs):2d}/{len(items):2d} newest_orig={nw} ({days}) inwin_orig={len(inwin_orig)} {best}")
