#!/usr/bin/env python3
"""2026-10-30 chk: AIWarper copyright post, 30-run trend, interesting_aIl profile."""
import json, os, re
from collections import Counter
from datetime import datetime, timezone, timedelta

RAW = "/home/kiro/trend-engine/trends/x/raw"
D = json.load(open(f"{RAW}/scan_results_2026-10-30_FULL.json"))
raw = D["raw"]

def parse_d(s):
    return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")

print("=== AIWARPER COPYRIGHT POST (full) ===")
for t in raw.get("AIWarper", []):
    txt = t.get("text") or ""
    if "copyright" in txt.lower() or "irony" in txt.lower() or "Wz6JQsXCsf" in txt:
        print(f"\n--- {t.get('createdAt')} {t.get('likeCount')}L/{t.get('viewCount')}V reply={bool(t.get('isReply'))}")
        print(txt)
        q = t.get("quoted_tweet")
        if q:
            print("QUOTED:", (q.get("text") or "")[:500])
        print(f"https://x.com/AIWarper/status/{t.get('id')}")

print("\n\n=== QUALIFYING TREND (last 30 scan files) ===")
files = sorted([f for f in os.listdir(RAW) if f.startswith("scan_results_") and f.endswith("_FULL.json")])
for f in files[-30:]:
    try:
        d = json.load(open(f"{RAW}/{f}"))
        date = f.replace("scan_results_","").replace("_FULL.json","")
        print(f"{date} qualifying={len(d['qualifying'])} handles={len(d['handles'])} failures={len(d.get('failures',[]))}")
    except Exception as e:
        print(f, "ERR", e)

print("\n\n=== interesting_aIl PROFILE ===")
tw = raw.get("interesting_aIl", [])
orig = [t for t in tw if not t.get("retweeted_tweet")]
inw = [t for t in orig if parse_d(t["createdAt"]) >= datetime.now(timezone.utc).replace(hour=0,minute=0,second=0,microsecond=0)-timedelta(days=3)]
print(f"fetched {len(tw)} originals {len(orig)} in-window {len(inw)}")
print("media mix:", Counter(((t.get("media") or [{}])[0].get("type","none")) for t in inw))
likes = sorted([t.get("likeCount") or 0 for t in inw], reverse=True)
print("top likes:", likes[:12])

print("\n\n=== HUBERMANLAB ===")
tw = raw.get("hubermanlab", [])
inw = [t for t in tw if not t.get("retweeted_tweet") and parse_d(t["createdAt"]) >= datetime.now(timezone.utc).replace(hour=0,minute=0,second=0,microsecond=0)-timedelta(days=3)]
print(f"in-window originals {len(inw)}")
for t in sorted(inw, key=lambda z:-(z.get('likeCount') or 0)):
    print(f"  {t.get('createdAt')} {t.get('likeCount')}L/{t.get('viewCount')}V | {(t.get('text') or '')[:150]}")

print("\n\n=== TODAY TOP 10 (deliverable board) ===")
q = D["qualifying"]
EXCL = {"interesting_aIl","hubermanlab","NVIDIAAI","OpenAI","AnthropicAI","DiscussingFilm","nofilmschool",
        "CINEMA505","Teamily_AI","AIBuzzNews","AIGuideHQ","AITECHio","AIFrontliner","TheAIColony","AITOPIAai",
        "GoogleLabs","GoogleAI","DonutAI","cladlabs","FaithJaiye2187","KammelaBala","MyStageAI","AlexStudio44",
        "AI_with_jasmin","Ai_Vaidehi","ChrisLaubAI","AIScottAdams","AISuperDomain","AI_Jasonyu","AIByLeo","Magiermogul"}
core = [x for x in q if x["handle"] not in EXCL]
core.sort(key=lambda x: x["score"], reverse=True)
n=0
for x in core:
    lr = x["likes"]/x["views"]*100 if x["views"] else 0
    n+=1
    print(f"{n}|@{x['handle']}|{x['likes']}L|{x['views']}V|{x['date']}|{x['score']:.0f}|lr={lr:.2f}%|{x['text'][:110]}|{x['url']}")
    if n>=16: break
