#!/usr/bin/env python3
"""2026-10-24 deep pass: buckets, proper core ranking, fetch windows, dormancy (originals only), outlier verify."""
import json, re, sys
from datetime import datetime, timezone, timedelta

D = "/home/kiro/trend-engine/trends/x/raw/scan_results_2026-10-24_FULL.json"
d = json.load(open(D))
raw, qual = d["raw"], d["qualifying"]

IRRELEVANT = 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())
FILM = {"DiscussingFilm", "nofilmschool"}
LABS = {"OpenAI", "AnthropicAI"}

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

now = datetime(2026, 10, 24, 6, 0, tzinfo=timezone.utc)
cutoff = datetime(2026, 10, 21, 0, 0, tzinfo=timezone.utc)

def qdt(q):
    return datetime.strptime(q["date"].replace(" UTC", ""), "%Y-%m-%d %H:%M").replace(tzinfo=timezone.utc)

core = [q for q in qual if q["handle"] not in IRRELEVANT and q["handle"] not in FILM]
film = [q for q in qual if q["handle"] in FILM]
irr = [q for q in qual if q["handle"] in IRRELEVANT and q["handle"] not in LABS]
labs = [q for q in qual if q["handle"] in LABS]
print("BUCKETS total=%d core=%d film=%d irrelevant=%d labs=%d" % (len(qual), len(core), len(film), len(irr), len(labs)))
print("HANDLES scanned=%d failed=%d" % (len(raw), len(d.get("failed", []))))
print("FAILED:", d.get("failed"))

core.sort(key=lambda q: -q["score"])
print("\n=== CORE TOP 15 (proper split) ===")
for i, q in enumerate(core[:15], 1):
    print("%2d|%s|%dL|%dRT|%dR|%dV|%s|score=%d|%s" % (
        i, q["handle"], q["likes"], q["rt"], q["replies"], q["views"],
        q["date"], q["score"], q["url"]))
print("cutoff rank10=%d rank11=%d" % (core[9]["score"], core[10]["score"]))
print("\n=== CORE TOP 15 TEXT ===")
for i, q in enumerate(core[:15], 1):
    print("%2d|%s|%s" % (i, q["handle"], q["text"][:300].replace("\n", " ")))

print("\n=== CORE RANKS 16-45 (brief) ===")
for i, q in enumerate(core[15:45], 16):
    print("%2d|%s|%dL|%dV|%s|sc=%d|%s" % (i, q["handle"], q["likes"], q["views"], q["date"], q["score"], q["text"][:100].replace("\n", " ")))

print("\n=== FETCH WINDOWS (parsed, all handles >=20h stale or watched) ===")
WATCH = {"higgsfield_ai","higgsfield","runwayml","Flovaai","Kling_ai","pika_labs","venturetwins",
         "AnthropicAI","azed_ai","AIWarper","EHuanglu","CharaspowerAI","JeffSynthesized","OpenAI",
         "DiscussingFilm","nofilmschool","c_valenzuelab","WuxiaRocks","CuriousRefuge"}
for h in sorted(raw):
    ds = [pdate(t["createdAt"]) for t in raw[h]]
    if not ds: continue
    n_in = sum(1 for x in ds if x >= cutoff)
    hi, lo = max(ds), min(ds)
    age = (now - hi).total_seconds() / 3600
    if h in WATCH or age > 20:
        print("%-16s n=%2d in=%2d %s -> %s  age=%.1fh%s" % (h, len(ds), n_in,
              lo.strftime("%m-%d %H:%M"), hi.strftime("%m-%d %H:%M"), age, "  <<STALE" if age > 24 else ""))

print("\n=== DORMANCY (zero qualifying) originals-only ===")
zero = [h for h in raw if not any(q["handle"] == h for q in qual)]
for h in sorted(zero):
    orig, rt = [], 0
    for t in raw[h]:
        if t.get("retweeted_tweet") or t.get("type") != "tweet":
            rt += 1
            continue
        orig.append(t)
    if orig:
        news = max(pdate(t["createdAt"]) for t in orig)
        best = max(orig, key=lambda t: t.get("likeCount", 0))
        print("%-16s originals=%2d rt=%2d newest=%s (%dd ago) best=%dL/%dV" % (
            h, len(orig), rt, news.strftime("%Y-%m-%d %H:%M"), (now - news).days,
            best.get("likeCount", 0), best.get("viewCount", 0)))
    else:
        print("%-16s NO ORIGINALS (rt=%d)" % (h, rt))

print("\n=== TOP-10 CORE VERIFY (author/media/baseline) ===")
for q in core[:10]:
    tid = q["url"].rstrip("/").split("/")[-1]
    rec = next((t for t in raw.get(q["handle"], []) if str(t.get("id")) == tid), None)
    if not rec:
        print("%s %s -> record NOT FOUND" % (q["handle"], tid)); continue
    auth = (rec.get("author") or {}).get("userName")
    med = rec.get("media")
    mtype = med[0].get("type") if isinstance(med, list) and med and isinstance(med[0], dict) else med
    others = sorted([t.get("likeCount", 0) for t in raw[q["handle"]] if str(t.get("id")) != tid])
    b2 = others[-1] if others else 0
    print("%-14s auth=%-14s media=%-6s rt=%s quote=%s reply=%s ratio=%.1fx  %s" % (
        q["handle"], auth, mtype, bool(rec.get("retweeted_tweet")), bool(rec.get("quoted_tweet")),
        bool(rec.get("isReply")), (q["likes"] / b2 if b2 else 0), q["text"][:80].replace("\n", " ")))
