#!/usr/bin/env python3
"""2026-10-18 pass 2: correct buckets (matches analyze script), proper chronological
fetch-window roll-off, dormancy, offers, arrival check, per-handle detail."""
import json, re
from datetime import datetime, timezone
from collections import Counter

DATE = "2026-10-18"
d = json.load(open(f"/home/kiro/trend-engine/trends/x/raw/scan_results_{DATE}_FULL.json"))
raw, qual, failures, handles = d["raw"], d["qualifying"], d["failures"], d["handles"]

AI_TERMS = re.compile(r"\b(ai\b|a\.i\.|seedance|kling|runway|veo|sora|minimax|hailuo|higgsfield|"
    r"midjourney|nano ?banana|gpt-?[0-9.]*|chatgpt|gemini|generative|render|vfx|ai film|aifilm|"
    r"grokbot|motion capture|deepfake|davinci|blender|autocad|storyboard|(ai|3d|generated) ?animation|"
    r"diffusion|comfyui|(?<!\bfilm )(?<!\bfilm-)(?<!\bscreenplay )(?<!\bscreenplay-)\bprompt)", re.I)

FILM_INDUSTRY = {"DiscussingFilm", "nofilmschool"}
AI_NEWS = {"[OI]", "AnthropicAI"}
IRRELEVANT = {"AIBuzzNews","AIGuideHQ","AITECHio","AIFrontliner","TheAIColony","AITOPIAai",
  "GoogleLabs","GoogleAI","DonutAI","cladlabs","FaithJaiye2187","KammelaBala","MyStageAI",
  "AlexStudio44","AI_with_jasmin","Ai_Vaidehi","ChrisLaubAI","CINEMA505","AIScottAdams",
  "AISuperDomain","AI_Jasonyu","AIByLeo"}

core, film, filt, labs = [], [], [], []
for x in qual:
    ai = bool(AI_TERMS.search(x["text"]))
    h = x["handle"]
    if h in FILM_INDUSTRY: film.append(x) if ai else None
    elif h in AI_NEWS: labs.append(x)
    elif h in IRRELEVANT: filt.append(x) if ai else None
    else: core.append(x)

print(f"scanned {len(handles)} | failures {len(failures)} | qualifying {len(qual)}")
print(f"core {len(core)} | film-industry AI-relevant {len(film)} | filtered AI-relevant {len(filt)} | labs {len(labs)}")
film_all = [x for x in qual if x["handle"] in FILM_INDUSTRY]
irr_all = [x for x in qual if x["handle"] in IRRELEVANT]
print(f"film-industry total {len(film_all)} | irrelevant total {len(irr_all)}")

# core top 10 with cutoff
cs = sorted(core, key=lambda x: -x["score"])
print("\n--- CORE TOP 12 (cutoff = rank10 score) ---")
for i, x in enumerate(cs[:12], 1):
    print(f"{i:2}. @{x['handle']:16} {x['likes']:6}L {x['rt']:4}RT {x['views']:8}V  score {x['score']:8.0f}  {x['date'][:16]}")
    print(f"    {(x['text'] or '')[:165].replace(chr(10),' ')}")
print(f"\ncutoff(rank10)={cs[9]['score']:.0f}  rank11={cs[10]['score']:.0f} (@{cs[10]['handle']})")

# ---- fetch window, PROPER chronological ----
print("\n--- FETCH WINDOW (chronological min/max; ALL-IN-WINDOW = oldest still inside cutoff) ---")
cutoff = datetime(2026, 10, 15, tzinfo=timezone.utc)
rows = []
for h, rs in raw.items():
    if not rs: continue
    try:
        ds = [datetime.strptime(x["createdAt"], "%a %b %d %H:%M:%S %z %Y") for x in rs]
    except Exception:
        continue
    rows.append((h, min(ds), max(ds), len(rs)))
for h, mn, mx, n in sorted(rows, key=lambda r: -r[1].timestamp()):
    flag = "  <<< ENTIRE SET IN-WINDOW" if mn > cutoff else ""
    print(f"  @{h:18} n={n:2} oldest {mn:%Y-%m-%d %H:%M}  newest {mx:%Y-%m-%d %H:%M}{flag}")

# ---- dormancy ----
def is_orig(t):
    return not t.get("retweeted_tweet") and not (t.get("text") or "").startswith("RT @")
qh = {x["handle"] for x in qual}
now = datetime.now(timezone.utc)
print("\n--- DORMANCY (originals only, RT-filtered) ---")
for h in handles:
    if h in qh: continue
    rs = raw.get(h) or []
    origs = [x for x in rs if is_orig(x)]
    tier = "IRREL" if h in IRRELEVANT else ("FILM" if h in FILM_INDUSTRY else ("LAB" if h in AI_NEWS else "CORE"))
    if not origs:
        print(f"  @{h:18} [{tier:5}] n={len(rs):2} NO ORIGINALS -> RT-only")
        continue
    ds = [datetime.strptime(x["createdAt"], "%a %b %d %H:%M:%S %z %Y") for x in origs]
    newest = max(ds)
    inwin = [(x, dt) for x, dt in zip(origs, ds) if dt > cutoff]
    best = max(((x.get("likeCount") or 0, x.get("viewCount") or 0) for x, _ in inwin), default=(0, 0))
    v = f"IN-WINDOW {len(inwin)} origs best {best[0]}L/{best[1]}V" if inwin else f"OUT {(now-newest).days}d"
    print(f"  @{h:18} [{tier:5}] newest orig {newest:%Y-%m-%d %H:%M} | {v}")

# ---- offers (loose) ----
print("\n--- OFFERS ---")
PATS = {
 "Flova/Seedance 80%": r"80%|kicks off|days to go|days left",
 "CapCut 60/40": r"60% off|40% off|oct 20",
 "Dreamina Basic promo": r"\$1\.50|basic promotion",
 "freebeat challenge": r"freebeat|lyric video challenge",
 "FLUX 3 Upscaler": r"flux ?3",
 "Higgsfield Academy": r"higgsfield academy",
 "Kling 4.0": r"kling ?4\.?0",
 "DeepSeek V4.1": r"deepseek v4\.?1",
 "filmera": r"filmera",
 "Mirelo": r"mirelo",
 "Pika x ChatGPT": r"plugin extension|pika x",
 "Genjutsu": r"genjutsu", "Wan 3.0": r"wan ?3\.?0", "cashback": r"cashback",
 "Magnific": r"magnific", "Dreamina Originals": r"dreamina originals",
 "Dolphin/XX-AYCMR8": r"xx-aycmr8|dolphin", "Air unlimited": r"air unlimited|unlimited credit",
 "ACN GPU": r"\bacn\b",
}
for name, pat in PATS.items():
    rx = re.compile(pat, re.I)
    hits = [x for x in qual if rx.search(x["text"] or "")]
    if not hits:
        print(f"  [{name}] 0 hits"); continue
    print(f"  [{name}] {len(hits)} hits")
    for x in sorted(hits, key=lambda y: -y["score"])[:3]:
        print(f"     @{x['handle']} {x['likes']}L/{x['views']}V {x['date'][:10]} :: {(x['text'] or '')[:185].replace(chr(10),' ')}")

# ---- azed_ai / CharaspowerAI / TSUBAKI full sets (creative + cost signal) ----
for h in ["azed_ai", "AI__TSUBAKI", "c_valenzuelab", "AIWarper", "WuxiaRocks"]:
    print(f"\n--- @{h} qualifying ---")
    for x in sorted([y for y in qual if y["handle"] == h], key=lambda y: -y["score"]):
        print(f"  {x['likes']:5}L/{x['views']:8}V {x['date'][:10]} :: {(x['text'] or '')[:200].replace(chr(10),' ')}")

# ---- arrival check: any auto-discovered handle in core top 10? ----
print("\n--- ARRIVAL CHECK ---")
KNOWN_CORE = {"CharaspowerAI","Kling_ai","venturetwins","Flovaai","pika_labs","EHuanglu",
 "higgsfield","higgsfield_ai","runwayml","paultrillo","CuriousRefuge","AIWarper","azed_ai",
 "c_valenzuelab","AI__TSUBAKI","WuxiaRocks","Chandan_Perla","karenxcheng","JeffSynthesized",
 "ai_filmmaker","Magiermogul","JiamingSuen"}
for i, x in enumerate(cs[:10], 1):
    h = x["handle"]
    print(f"  {i:2}. @{h:16} {'CORE' if h in KNOWN_CORE else '*** UNCLASSIFIED ARRIVAL ***'}")
