#!/usr/bin/env python3
"""Second-pass checks for xmon 2026-11-04: film AI matches, labs detail,
irrelevant AI listing, key post full texts, quoted-tweet expansion, paid-reach audit."""
import json, re, os
from datetime import datetime, timezone, timedelta

RAW = "/home/kiro/trend-engine/trends/x/raw"
TODAY = "2026-11-04"
d = json.load(open(f"{RAW}/scan_results_{TODAY}_FULL.json"))
raw = d["raw"]
NOW = datetime.now(timezone.utc)
CUT = NOW.replace(hour=0,minute=0,second=0,microsecond=0) - timedelta(days=3)

def pd(s):
    try: return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")
    except Exception: return None
def is_orig(t):
    return not (t.get("retweeted_tweet") or t.get("type")=="retweet" or (t.get("text") or "").startswith("RT @"))
def qual(t):
    return ((t.get("likeCount") or 0)>=50 or (t.get("retweetCount") or 0)>=10 or (t.get("viewCount") or 0)>=1000)

# NOTE: leading AND trailing \b both required -- a leading-only \b matches "ai"
# inside "aired" with re.I and gave a FALSE film-bucket AI hit on 2026-11-04.
STRONG = re.compile(r"\b(AI|A\.I\.|artificial intelligence|generative|LLM|GPT|diffusion|"
                    r"machine learning|neural|prompts?|models? generat\w*)\b", re.I)

print("===== FILM BUCKET AI MATCHES =====")
for h in ("DiscussingFilm","nofilmschool"):
    for t in raw.get(h,[]):
        dt = pd(t.get("createdAt",""))
        if not dt or dt<CUT or not is_orig(t) or not qual(t): continue
        txt = (t.get("text") or "").replace("\n"," ")
        m = STRONG.search(txt)
        if m:
            print(f"  @{h}|{dt.strftime('%m-%d %H:%M')}|{t.get('likeCount')}L|{t.get('viewCount')}V|term={m.group(0)}|{txt[:260]}")
            print(f"     https://x.com/{h}/status/{t.get('id')}")
print("  (empty above = zero AI-relevant)")

print("\n===== LABS DETAIL =====")
for h in ("NVIDIAAI","OpenAI","AnthropicAI"):
    sel=[(pd(t.get('createdAt','')),t) for t in raw.get(h,[]) if pd(t.get('createdAt','')) and pd(t.get('createdAt',''))>=CUT and is_orig(t) and qual(t)]
    print(f"\n  @{h} ({len(sel)}):")
    for dt,t in sorted(sel, key=lambda z: z[0], reverse=True):
        print(f"    {dt.strftime('%m-%d %H:%M')}|{t.get('likeCount')}L|{t.get('retweetCount')}RT|{t.get('viewCount')}V|{(t.get('text') or '').replace(chr(10),' ')[:230]}")

print("\n===== OPENAI GPT-6.x POSTS (any window) =====")
for h in ("OpenAI",):
    for t in raw.get(h,[]):
        txt=(t.get("text") or "").replace("\n"," ")
        if re.search(r"GPT-6", txt, re.I):
            dt=pd(t.get("createdAt",""))
            print(f"  {dt.strftime('%m-%d %H:%M') if dt else '?'}|{t.get('likeCount')}L|{t.get('viewCount')}V|{txt[:300]}")
            print(f"     https://x.com/{h}/status/{t.get('id')}")

print("\n===== KEY POST FULL TEXT + QUOTED EXPANSION =====")
KEYS = [("EHuanglu","Cyberpunk 2077"),("Flovaai","fancy cafe"),("heyglif","YOU CAN PROMPT"),
        ("pika_labs","Turbo LoRA"),("pika_labs","Meet Pika H3 Flex"),("EHuanglu","3D animation in 3hrs")]
for h,needle in KEYS:
    for t in raw.get(h,[]):
        txt=(t.get("text") or "").replace("\n"," ")
        if needle.lower() in txt.lower():
            dt=pd(t.get("createdAt",""))
            print(f"\n  @{h}|{dt.strftime('%Y-%m-%d %H:%M') if dt else '?'}|{t.get('likeCount')}L|{t.get('retweetCount')}RT|{t.get('replyCount')}R|{t.get('viewCount')}V|type={t.get('type')}|media={[m.get('type') for m in (t.get('media') or [])]}")
            print(f"  TEXT: {txt[:600]}")
            print(f"  URL: https://x.com/{h}/status/{t.get('id')}")
            q = t.get("quoted_tweet") or t.get("retweeted_tweet")
            if q:
                print(f"  QUOTED @{(q.get('author') or {}).get('userName','?')}: {(q.get('text') or '').replace(chr(10),' ')[:400]}")
            break

print("\n===== IRRELEVANT AI-RELEVANT (top 12 by score) =====")
def score(t): return ((t.get('likeCount') or 0)+2*(t.get('retweetCount') or 0)+3*(t.get('replyCount') or 0)+(t.get('viewCount') or 0)/100)
IRR = {"AIBuzzNews","AIGuideHQ","AITECHio","AIFrontliner","TheAIColony","AITOPIAai","GoogleLabs","GoogleAI","DonutAI","cladlabs","FaithJaiye2187","KammelaBala","MyStageAI","AlexStudio44","AI_with_jasmin","Ai_Vaidehi","ChrisLaubAI","CINEMA505","OpenAI","AnthropicAI","AIScottAdams","AISuperDomain","AI_Jasonyu","AIByLeo","Soaima_Ai","different_ai","via_ai_","Teamily_AI","AIsaOneHQ","AiWithTariq","numerai"}
lst=[]
for h in IRR:
    for t in raw.get(h,[]):
        dt=pd(t.get('createdAt',''))
        if not dt or dt<CUT or not is_orig(t) or not qual(t): continue
        txt=(t.get('text') or '').replace("\n"," ")
        if STRONG.search(txt):
            lst.append((score(t),h,dt,t,txt))
for s,h,dt,t,txt in sorted(lst, reverse=True)[:12]:
    print(f"  @{h}|{dt.strftime('%m-%d %H:%M')}|{t.get('likeCount')}L|{t.get('viewCount')}V|scr={s:.0f}|{txt[:150]}")

print("\n===== PAID-REACH AUDIT (views>=100k, like rate) =====")
rows=[]
for h,tw in raw.items():
    for t in tw:
        dt=pd(t.get('createdAt',''))
        if not dt or dt<CUT or not is_orig(t): continue
        v=t.get('viewCount') or 0; l=t.get('likeCount') or 0
        if v>=100000:
            rows.append((l/v*100, l, v, h, dt, (t.get('text') or '').replace("\n"," ")[:90]))
for lr,l,v,h,dt,txt in sorted(rows):
    print(f"  lr={lr:6.3f}%|{l}L|{v}V|@{h}|{dt.strftime('%m-%d %H:%M')}|{txt}")

print("\n===== WATCHLIST DIFF =====")
WL="/home/kiro/trend-engine/trends/x/raw/rising-accounts.md"
txt=open(WL).read()
re_h=set(x.lower() for x in re.findall(r"@(\w+)",txt))
# handles actually built by runner
import subprocess
built=set(h.lower() for h in d["handles"])
print(f"  watchlist handles: {len(re_h)}  built: {len(built)}  failures: {d['failures']}")
print(f"  scanned but not in md-regex: {sorted(built-re_h)}")
