#!/usr/bin/env python3
import json, re
from datetime import datetime, timezone

RAW="/home/kiro/trend-engine/trends/x/raw/"
d=json.load(open(RAW+"scan_results_2026-11-08_FULL.json"))
raw=d["raw"]
def pdt(s): return datetime.strptime(s,"%a %b %d %H:%M:%S %z %Y")
now=datetime.now(timezone.utc)
CUT=now.replace(hour=0,minute=0,second=0,microsecond=0).replace(day=5)

orig=[]
for h,tw in raw.items():
    for t in tw:
        if t.get("retweeted_tweet") or t.get("type")!="tweet": continue
        if pdt(t["createdAt"])<CUT: continue
        orig.append((h,t))

STRONG=r"\bAI\b|artificial intelligence|generative|midjourney|stable diffusion|comfyui|seedance|kling|runway|sora|veo|pika|higgsfield|Luma|dreamina|gpt image|nano banana|firefly|premiere|resolve|topaz|neural|model"
print("=== FILM BUCKET (DiscussingFilm, nofilmschool) AI-term audit ===")
for h in ["DiscussingFilm","nofilmschool"]:
    tw=raw.get(h) or []
    inw=[t for t in tw if not t.get("retweeted_tweet") and pdt(t["createdAt"])>=CUT]
    print(f"\n-- @{h}: {len(inw)} in-window originals, {len(tw)} fetched")
    for t in inw:
        body=re.sub(r"https?://\S+"," ",t["text"])
        hits=re.findall(STRONG, body, re.I)
        if hits:
            print(f"  HIT {hits} {t['createdAt'][:16]} {t['likeCount']}L/{t['viewCount']}V :: {t['text'][:220].replace(chr(10),' ')}")
print("\nSTRONG_TERMS total hits:", sum(1 for h in ["DiscussingFilm","nofilmschool"] for t in (raw.get(h) or []) if not t.get("retweeted_tweet") and pdt(t["createdAt"])>=CUT and re.search(STRONG, re.sub(r"https?://\S+"," ",t["text"]), re.I)))

print("\n=== BOARD-WIDE LAUNCH SWEEP over",len(orig),"in-window originals ===")
KW=["miracle","viggle remake","h3 flex","gemini omni","gpt image 2.6","gpt-image-2.6","seedance 2.5","kling 4.0","kling 4",
    "sora","veo","seedance 3","kling 5","nano banana","dreamina","chroma award","perplexity computer","teammates",
    "glif","modeling agency","open weights","lyra","cosmos","nemotron"]
for k in KW:
    hits=[(h,t) for h,t in orig if k.lower() in t["text"].lower()]
    if hits:
        print(f"{k}: {len(hits)} -> " + ", ".join(f"@{h}" for h,_ in hits[:8]))
    else:
        print(f"{k}: 0")

print("\n=== TOP 12 CORE ROWS TRIMMED (for report) ===")
import re as r2
CORE={"higgsfield","higgsfield_ai","Flovaai","pika_labs","Kling_ai","EHuanglu","AIWarper","heyglif","c_valenzuelab","runwayml","venturetwins","CharaspowerAI","azed_ai","ElsaSofia__AI","ItsMaryamAi","CuriousRefuge","openart_ai","ViggleAI","perplexity_ai","AI__TSUBAKI","WuxiaRocks"}
q=[x for x in d["qualifying"] if x["handle"] in CORE]
def sc(x): return x["likes"]+x["rt"]*2+x["replies"]*3+x["views"]/100
for x in sorted(q,key=sc,reverse=True)[:12]:
    print(f"@{x['handle']}|{x['likes']}L|{x['rt']}RT|{x['replies']}R|{x['views']}V|{x['date']}|rate={x['likes']/max(x['views'],1)*100:.2f}%|{x['url']}")
