#!/usr/bin/env python3
"""2026-10-18 verification: totals, buckets, fetch-window roll-off, dormancy (originals-only),
offer grep, top-10 arrival check, tier-2 in-window originals, outlier verification.

SHAPE: raw[handle][] uses userName/likeCount/retweetCount/viewCount/createdAt/text
       qualifying[] uses handle/score/date/likes/rt/replies/views/text/url
"""
import json, re
from datetime import datetime, timezone
from collections import defaultdict

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

IRRELEVANT = set("""AIBuzzNews AIGuideHQ AITECHio AIFrontliner TheAIColony AITOPIAai AISuperDomain
GoogleLabs GoogleAI DonutAI cladlabs FaithJaiye2187 KammelaBala MyStageAI AlexStudio44
AI_with_jasmin Ai_Vaidehi ChrisLaubAI AnthropicAI openai CINEMA505 AIScottAdams AI_Jasonyu
AIByLeo""".split())
FILM_INDUSTRY = {"DiscussingFilm", "nofilmschool"}
CORE = set("""higgsfield higgsfield_ai CharaspowerAI EHuanglu AIWarper venturetwins azed_ai c_valenzuelab
Kling_ai runwayml Flovaai AI__TSUBAKI WuxiaRocks pika_labs paultrillo CuriousRefuge CorridorDigital
JeffSynthesized ai_filmmaker visiblemaker Magiermogul JiamingSuen moonvalley Chandan_Perla karenxcheng""".split())

AI_TERMS = re.compile(r"\b(ai|a\.i\.|gpt|chatgpt|claude|gemini|sora|veo|seedance|kling|runway|midjourney|"
    r"higgsfield|nano ?banana|stable diffusion|generative|diffusion|llm|prompt|vfx|render|"
    r"machine learning|neural|model 5\.5|deepseek|flux|upscal)", re.I)

now = datetime.now(timezone.utc)
cutoff = datetime(2026, 10, 15, 0, 0, tzinfo=timezone.utc)
print(f"cutoff: {cutoff}")

# ---- buckets from qualifying[] ----
core_q, film_q, irr_q = [], [], []
for q in qual:
    h = q["handle"]
    if h in FILM_INDUSTRY: film_q.append(q)
    elif h in IRRELEVANT: irr_q.append(q)
    else: core_q.append(q)
print(f"BUCKETS: core {len(core_q)} | film-industry {len(film_q)} "
      f"(AI-relevant {sum(1 for q in film_q if AI_TERMS.search(q['text'] or ''))}) | "
      f"irrelevant {len(irr_q)} (AI-relevant {sum(1 for q in irr_q if AI_TERMS.search(q['text'] or ''))})")
labs = [q for q in irr_q if q["handle"] in {"openai", "AnthropicAI"}]
print(f"AI-labs subset of irrelevant: {len(labs)}")
print(f"coverage: {sum(1 for v in raw.values() if v)} handles with >=1 raw tweet")

# ---- core top 12 + arrival check ----
cs = sorted(core_q, key=lambda x: -x["score"])[:12]
print("\n--- CORE TOP 12 (arrival check) ---")
for i, q in enumerate(cs, 1):
    h = q["handle"]
    tag = "CORE" if h in CORE else ("IRREL!" if h in IRRELEVANT else "unclassified")
    print(f"{i:2}. @{h:16} [{tag:12}] {q['likes']:6}L {q['rt']:4}RT {q['replies']:4}R {q['views']:8}V "
          f"score={q['score']:8.0f} {q['date'][:16]}")
    print(f"     {(q['text'] or '')[:150]}")

# ---- fetch-window roll-off ----
print("\n--- FETCH WINDOW: whole fetched set inside 3-day window ---")
roll = []
for h, rs in raw.items():
    if not rs: continue
    ds = sorted(x.get("createdAt", "") for x in rs)
    try:
        mn = datetime.strptime(ds[0], "%a %b %d %H:%M:%S %z %Y")
        mx = datetime.strptime(ds[-1], "%a %b %d %H:%M:%S %z %Y")
    except Exception:
        continue
    if mn > cutoff:
        roll.append((h, mn, mx, len(rs)))
for h, mn, mx, n in sorted(roll, key=lambda x: -x[1].timestamp()):
    print(f"  @{h:18} n={n:2} oldest {mn:%Y-%m-%d %H:%M} newest {mx:%Y-%m-%d %H:%M}  <<< ALL IN-WINDOW")
print(f"  ({len(roll)} such handles)")

# ---- dormancy ----
def is_orig(t):
    if t.get("retweeted_tweet"): return False
    return not (t.get("text") or "").startswith("RT @")

print("\n--- DORMANCY (originals only, RT-filtered) -- zero-qualifying handles ---")
qh = {q["handle"] for q in qual}
for h in sorted(raw):
    if h in qh: continue
    rs = raw[h]
    origs = [x for x in rs if is_orig(x)]
    tier = "IRREL" if h in IRRELEVANT else ("FILM" if h in FILM_INDUSTRY else "CORE")
    if not origs:
        print(f"  @{h:18} [{tier:5}] NO ORIGINALS (n={len(rs)}) -> RT-only")
        continue
    newest = max(datetime.strptime(x["createdAt"], "%a %b %d %H:%M:%S %z %Y") for x in origs)
    inwin = [x for x in origs if datetime.strptime(x["createdAt"], "%a %b %d %H:%M:%S %z %Y") > cutoff]
    best = max(((x.get("likeCount", 0), x.get("viewCount", 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-OF-WINDOW {(now-newest).days}d"
    print(f"  @{h:18} [{tier:5}] newest orig {newest:%Y-%m-%d %H:%M} | {v}")

# ---- offer grep ----
print("\n--- OFFER GREP ---")
OFFERS = {
 "Flova Seedance 80%": r"(flova|seedance)[^.]{0,90}80%|80% off|days (left|to go)",
 "Genjutsu": r"genjutsu", "Wan 3.0": r"wan ?3\.?0", "cashback": r"cashback",
 "Ads Studio/Multiplier": r"ads? studio|ad multiplier", "AI Influencer free": r"influencer[^.]{0,50}free",
 "FLUX 3": r"flux ?3", "CapCut BF": r"capcut[^.]{0,140}(60%|40%)|black friday",
 "Magnific": r"magnific", "Dreamina Originals": r"dreamina originals", "Dolphin/XX-AYCMR8": r"XX-AYCMR8|dolphin ai",
 "Air unlimited": r"air unlimited|unlimited credits", "Higgsfield Academy": r"higgsfield academy",
 "freebeat": r"freebeat", "Kling 4.0": r"kling ?4\.?0", "Mirelo": r"mirelo",
 "AITECHio/ACN": r"\bacn\b|agent forge", "filmera": r"filmera", "Seedance 2.5": r"seedance 2\.5",
 "DeepSeek V4.1": r"deepseek v4\.?1", "Pika x ChatGPT": r"pika x chatgpt|plugin extension",
}
for name, pat in OFFERS.items():
    rx = re.compile(pat, re.I)
    hits = [q for q in qual if rx.search((q["text"] or "") + " " + (q["url"] or ""))]
    if hits:
        print(f"\n  [{name}] {len(hits)} hits")
        for q in sorted(hits, key=lambda x: -x["score"])[:3]:
            print(f"    @{q['handle']} {q['likes']}L/{q['views']}V {q['date'][:10]} :: {(q['text'] or '')[:190]}")
    else:
        print(f"  [{name}] 0 hits")

# ---- tier-2 ----
TIER2 = ["CuriousRefuge","paultrillo","CorridorDigital","JeffSynthesized","ai_filmmaker",
         "visiblemaker","Magiermogul","JiamingSuen","moonvalley","Chandan_Perla"]
print("\n--- TIER-2 ---")
for h in TIER2:
    rs = raw.get(h) or []
    origs = [x for x in rs if is_orig(x)]
    inwin = [x for x in origs if datetime.strptime(x["createdAt"], "%a %b %d %H:%M:%S %z %Y") > cutoff]
    nq = sum(1 for q in qual if q["handle"] == h)
    print(f"  @{h:18} fetched {len(rs):2} origs {len(origs):2} in-window-origs {len(inwin):2} qualifying {nq}")

# ---- outlier verify ----
print("\n--- OUTLIER: @CharaspowerAI raw (author + media) ---")
for x in sorted(raw.get("CharaspowerAI", []), key=lambda x: -(x.get("likeCount") or 0))[:3]:
    au = x.get("author") or {}
    print(f"  {x.get('likeCount')}L/{x.get('retweetCount')}RT/{x.get('viewCount')}V {x.get('createdAt')}")
    print(f"    author={(au.get('userName') if isinstance(au, dict) else au)} media={x.get('media') if not isinstance(x.get('media'), list) else [m.get('type') for m in x.get('media')]}")
    print(f"    RT-of={bool(x.get('retweeted_tweet'))} text={(x.get('text') or '')[:130]}")
