import json, re, datetime
from collections import Counter
D="2026-10-17"; P="2026-10-16"
base="/home/kiro/trend-engine/trends/x/raw/"
cur=json.load(open(base+f"scan_results_{D}_FULL.json"))
prev=json.load(open(base+f"scan_results_{P}_FULL.json"))
q=cur["qualifying"]; raw=cur["raw"]; fails=cur.get("failures",[])
print("TOTAL qualifying:", len(q), "| raw handles:", len(raw), "| failures:", fails)
print("qualifying keys:", sorted(q[0].keys()))
rk=[]
for h,v in raw.items():
    if v: rk=sorted(v[0].keys()); break
print("raw keys:", rk)

CORE = set("""runwayml CuriousRefuge venturetwins Kling_ai paultrillo c_valenzuelab karenxcheng higgsfield pika_labs EHuanglu azed_ai JeffSynthesized ai_filmmaker Magiermogul WuxiaRocks JiamingSuen higgsfield_ai Chandan_Perla CharaspowerAI AIWarper nofilmschool""".split())
FILM = {"DiscussingFilm","nofilmschool"}
LABS = {"OpenAI","AnthropicAI"}
IRREL = set("""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""".split())

def bucket(h):
    if h in LABS: return "labs"
    if h in FILM: return "film"
    if h in IRREL: return "irrel"
    if h in CORE: return "core"
    return "OTHER:"+h

c=Counter(bucket(t["handle"]) for t in q)
print("\nBUCKETS:", dict(c))

hc=set(raw.keys()); hp=set(prev["raw"].keys())
print("\nWatchlist now:", len(hc), "| prev:", len(hp))
print("ADDED:", sorted(hc-hp), "| REMOVED:", sorted(hp-hc))

print("\n=== FETCH WINDOW (min/max createdAt, top-volume handles) ===")
def dt(s): return datetime.datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")
for h in sorted(raw, key=lambda x: -len(raw[x]))[:14]:
    ts=[t.get("createdAt") for t in raw[h] if t.get("createdAt")]
    if ts:
        print(f"{h:18s} n={len(raw[h]):3d} oldest={min(ts)[:16]} newest={max(ts)[:16]}")

now=datetime.datetime.now(datetime.timezone.utc)
cutoff=datetime.datetime(now.year,now.month,now.day,tzinfo=datetime.timezone.utc)-datetime.timedelta(days=3)
print("\n3-day cutoff (exclusive):", cutoff.isoformat())

qual_handles=set(t["handle"] for t in q)
zq=[h for h in raw if h not in qual_handles]
print("\n=== ZERO-QUALIFYING HANDLES (originals-only) ===")
def is_orig(t):
    return t.get("retweeted_tweet") is None and not (t.get("text") or "").startswith("RT @")
for h in sorted(zq):
    ts=raw[h]
    origs=[t for t in ts if is_orig(t)]
    newest = max((t["createdAt"] for t in origs), default=None)
    if newest:
        age=(now-dt(newest)).days
        inw = dt(newest) >= cutoff
        inw_o=[t for t in origs if dt(t["createdAt"])>=cutoff]
        best = max((t.get("likeCount",0) for t in inw_o), default=None)
        bestv = max((t.get("viewCount",0) for t in inw_o), default=None)
        print(f"{h:18s} orig={len(origs):2d}/{len(ts):2d} newestORIG={newest[:16]} age={age:4d}d {'IN-WINDOW' if inw else 'out      '} bestL={best} bestV={bestv}")
    else:
        print(f"{h:18s} orig=0/{len(ts)} NO ORIGINALS")

print("\n=== CORE TOP 10 (auto-discovered arrival check) ===")
core_q=[t for t in q if bucket(t["handle"])=="core"]
for t in core_q[:10]:
    print(f"{t['handle']:16s} {t['likes']}L {t['views']}V {t['date'][:16]}")

print("\n=== CORE rank 11-15 cutoff ===")
for t in core_q[10:15]:
    print(f"rank {core_q.index(t)+1}: {t['handle']:16s} score={t['score']} {t['likes']}L {t['views']}V {t['date'][:16]}")

print("\n=== CHARASPOWERAI RAW (outlier 70kL) ===")
for t in sorted(raw.get("CharaspowerAI",[]), key=lambda x: -(x.get("likeCount") or 0))[:5]:
    print(t.get("createdAt"), "|", t.get("likeCount"),"L", t.get("retweetCount"),"RT", t.get("viewCount"),"V | rt=", t.get("retweeted_tweet") is not None)
    print("   ", (t.get("text") or "")[:300].replace("\n"," "))
