import json, re
from datetime import datetime, timezone, timedelta
RAW="/home/kiro/trend-engine/trends/x/raw"
d=json.load(open(f"{RAW}/scan_results_2026-11-12_FULL.json"))
prev=json.load(open(f"{RAW}/scan_results_2026-11-11_FULL.json"))
CUT=datetime(2026,11,9,tzinfo=timezone.utc)
FMT="%a %b %d %H:%M:%S %z %Y"
def dt(s): return datetime.strptime(s,FMT)
now=datetime.now(timezone.utc)

IRRELEVANT={"AIBuzzNews","AIGuideHQ","AITECHio","AIFrontliner","TheAIColony","AITOPIAai","AISuperDomain",
 "GoogleLabs","GoogleAI","DonutAI","cladlabs","FaithJaiye2187","KammelaBala","MyStageAI","AlexStudio44",
 "AI_with_jasmin","Ai_Vaidehi","ChrisLaubAI","OpenAI","AnthropicAI","CINEMA505","AIScottAdams","AI_Jasonyu","AIByLeo",
 "interesting_aIl","hubermanlab","NVIDIAAI","Teamily_AI","EinsiaAI","numerai","openart_ai","different_ai","via_ai_",
 "AIsaOneHQ","AiWithTariq","Lab_for_Men","dai2noyuyuta","uns__nstudio","Nevermined_ai","AidenChaoyangHe","MysticTrioAi"}
FILM={"DiscussingFilm","nofilmschool"}

qual=d['qualifying']
print("=== BUCKETS (disjoint) ===")
core=[q for q in qual if q['handle'] not in IRRELEVANT and q['handle'] not in FILM]
print("core",len(core),"| total",len(qual))
core.sort(key=lambda x:-x['score'])
print("\n=== CORE TOP 15 with like rate + marginal delta ===")
prevq={}
for q in prev['qualifying']:
    tid=q['url'].rstrip('/').split('/')[-1]; prevq[tid]=q
for i,q in enumerate(core[:15],1):
    tid=q['url'].rstrip('/').split('/')[-1]
    lr=q['likes']/q['views']*100 if q['views'] else 0
    p=prevq.get(tid)
    marg=""
    if p:
        dl=q['likes']-p['likes']; dv=q['views']-p['views']
        marg=f" | d+{dl}L/+{dv}V = {dv/dl:.0f}v/like" if dl>0 else f" | d+{dl}L/+{dv}V = NO LIKE GROWTH"
    print(f"{i}|@{q['handle']}|{q['likes']}L/{q['views']}V|{lr:.3f}%|score {q['score']:.0f}|{q['date']}{marg}")
    print(f"   {q['text'][:180].replace(chr(10),' ')}")
    print(f"   {q['url']}")

print("\n=== ORGANIC vs PAID (core) ===")
paid=[q for q in core if q['views'] and q['likes']/q['views']*100 < 0.10]
print(f"organic {len(core)-len(paid)} of {len(core)}; paid-reach (<0.10%) {len(paid)}:")
for q in paid: print(f"  @{q['handle']} {q['likes']}L/{q['views']}V {q['likes']/q['views']*100:.3f}% | {q['text'][:90]}")

print("\n=== FRESHNESS (max-based, newest fetched) ===")
rows=[]
for h,t in d['raw'].items():
    ts=[dt(x['createdAt']) for x in t if x.get('createdAt')]
    if not ts: continue
    newest=max(ts); n=len(ts); inw=sum(1 for x in ts if x>=CUT)
    rows.append(((now-newest).total_seconds()/3600,h,n,inw,newest))
rows.sort()
for hrs,h,n,inw,newest in rows[:22]:
    print(f"  @{h} {hrs:.1f}h n={n} inwin={inw}")
print("  --- stalest ---")
for hrs,h,n,inw,newest in rows[-12:]:
    print(f"  @{h} {hrs:.1f}h n={n} inwin={inw} newest={newest:%Y-%m-%d %H:%M}")

print("\n=== DORMANCY (originals only, RT filtered) ===")
for h,t in d['raw'].items():
    ors=[dt(x['createdAt']) for x in t if x.get('createdAt') and not x.get('retweeted_tweet') and not x.get('isReply')]
    if not ors:
        print(f"  @{h}: NO ORIGINALS in fetched set"); continue
    newest=max(ors); hrs=(now-newest).total_seconds()/3600
    inw=[x for x in ors if x>=CUT]
    qualh=[q for q in qual if q['handle']==h]
    if hrs>72 and not qualh:
        print(f"  @{h}: DORMANT {hrs:.1f}h newest={newest:%Y-%m-%d %H:%M} best={max([x['likes'] for x in t]):,}L")

print("\n=== THRESHOLD ARTEFACTS (in-window originals, 0 qualifying) ===")
for h,t in d['raw'].items():
    ors=[x for x in t if x.get('createdAt') and not x.get('retweeted_tweet') and not x.get('isReply') and dt(x['createdAt'])>=CUT]
    if ors and not [q for q in qual if q['handle']==h]:
        print(f"  @{h}: {len(ors)} in-window originals, best {max(x['likeCount'] for x in ors)}L")
