#!/usr/bin/env python3
"""Verify pass 2026-10-27: new-handle profiling, film bucket audit, offer grep,
tier-2 dormancy detail, core ranks 12-30."""
import json, re
from datetime import datetime, timezone, timedelta
from collections import Counter

DATE = "2026-10-27"
RAW = "/home/kiro/trend-engine/trends/x/raw"
d = json.load(open(f"{RAW}/scan_results_{DATE}_FULL.json"))
q = d["qualifying"]; raws = d["raw"]

def pd(s):
    try: return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")
    except Exception: return None

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

# ---------- NEW HANDLE PROFILING ----------
print("===== NEW HANDLES (added today) =====")
for h in ["ItsMaryamAi","Soaima_Ai","different_ai","numerai","openart_ai"]:
    tw = raws.get(h, [])
    orig = [t for t in tw if not t.get("retweeted_tweet") and not (t.get("text") or "").startswith("RT @")]
    inw = [t for t in orig if (pd(t.get("createdAt","")) or datetime.min.replace(tzinfo=timezone.utc)) >= cutoff]
    med = Counter((t.get("media") or [{}])[0].get("type","none") for t in orig)
    likes = sorted([t.get("likeCount") or 0 for t in orig], reverse=True)
    AI = re.compile(r"\b(ai\b|seedance|kling|runway|veo|sora|minimax|hailuo|higgsfield|midjourney|nano ?banana|gpt|chatgpt|gemini|generative|vfx|prompt)\b", re.I)
    ai_hits = sum(1 for t in orig if AI.search(t.get("text") or ""))
    print(f"\n@{h}: fetched {len(tw)} orig {len(orig)} in-window-orig {len(inw)} media={dict(med)}")
    print(f"   likes top5={likes[:5]} median={likes[len(likes)//2] if likes else 0}  AI-term density {ai_hits}/{len(orig)}")
    for t in sorted(inw, key=lambda t: -(t.get("likeCount") or 0))[:5]:
        print(f"   {pd(t['createdAt']).strftime('%m-%d %H:%M')} {t.get('likeCount',0)}L/{t.get('viewCount',0)}V [{(t.get('media') or [{}])[0].get('type','none')}] {(t.get('text') or '')[:150]}")

# ---------- FILM BUCKET ----------
print("\n\n===== FILM BUCKET (STRONG_TERMS audit) =====")
FILM = {"DiscussingFilm","nofilmschool"}
STRONG = re.compile(r"\b(a\.i\.|seedance|kling|runway|veo|sora|minimax|hailuo|higgsfield|midjourney|nano ?banana|chatgpt|gemini|generative|vfx|ai film|aifilm|deepfake|comfyui|diffusion|(ai|3d|generated) ?animation)\b", re.I)
fb = [x for x in q if x["handle"] in FILM]
print(f"film qualifying total: {len(fb)}  " + ", ".join(f"@{h}({n})" for h,n in Counter(x['handle'] for x in fb).most_common()))
hits = [(x, STRONG.search(x["text"])) for x in fb]
hits = [(x,m) for x,m in hits if m]
print(f"STRONG_TERMS matches: {len(hits)}")
for x,m in hits:
    print(f"  @{x['handle']} term='{m.group(0)}' {x['likes']}L :: {x['text'][:200]}")
# bare 'ai' token check with URL stripped
BARE = re.compile(r"\bai\b", re.I)
bare = []
for x in fb:
    stripped = re.sub(r"https?://\S+", " ", x["text"])
    if BARE.search(stripped): bare.append(x)
print(f"bare-'ai' matches AFTER URL strip (would be real signal): {len(bare)}")
for x in bare: print(f"  @{x['handle']} {x['likes']}L :: {x['text'][:200]}")

# ---------- OTHER BUCKET counts ----------
print("\n===== BUCKET SIZES =====")
print(Counter(("FILM" if x["handle"] in FILM else
               "LABS" if x["handle"] in {"OpenAI","AnthropicAI","NVIDIAAI"} else
               "OFFTOPIC" if x["handle"] in {"interesting_aIl","hubermanlab"} else
               "IRREL" if x["handle"] in {"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"} else "CORE") for x in q))

# ---------- CORE ranks 12-30 ----------
print("\n===== CORE RANKS 12-30 (raw engagement order, incl offtopic) =====")
q2 = sorted(q, key=lambda x: -x["score"])
excl = FILM | {"OpenAI","AnthropicAI","NVIDIAAI","interesting_aIl","hubermanlab","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"}
cor = [x for x in q2 if x["handle"] not in excl]
for i,x in enumerate(cor[11:30],12):
    print(f"{i}|@{x['handle']}|{x['likes']}L|{x['views']}V|{x['date']}|{x['score']:.0f}|{x['text'][:110]}")
