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

D = "/home/kiro/trend-engine/trends/x/raw/scan_results_2026-10-23_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, 23, 6, 0, tzinfo=timezone.utc)
cutoff = datetime(2026, 10, 20, 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)))

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"]))

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

# fetch windows per handle (parsed datetimes)
print("\n=== FETCH WINDOWS (min/max createdAt parsed) ===")
for h in sorted(raw):
    ds = [pdate(t["createdAt"]) for t in raw[h]]
    n_in = sum(1 for x in ds if x >= cutoff)
    hi = max(ds).strftime("%m-%d %H:%M")
    lo = min(ds).strftime("%m-%d %H:%M")
    age = (now - max(ds)).total_seconds() / 3600
    flag = "  <<STALE" if age > 24 else ""
    if h in ("higgsfield_ai", "higgsfield", "runwayml", "Flovaai", "Kling_ai", "pika_labs", "venturetwins", "AnthropicAI", "Flovaai", "azed_ai", "AIWarper", "EHuanglu") or age > 24:
        print("%-16s n=%2d in=%2d %s -> %s  age=%.1fh%s" % (h, len(ds), n_in, lo, hi, age, flag))

# dormancy: originals only for zero-qualifying
zero = [h for h in raw if not any(q["handle"] == h for q in qual)]
print("\n=== DORMANCY (zero qualifying) originals-only ===")
for h in 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))

# outlier verify for top 10 core
print("\n=== TOP-10 CORE VERIFY (author/media/baseline) ===")
for q in core[:10]:
    tid = q["url"].rstrip("/").split("/")[-1]
    rec = None
    for t in raw.get(q["handle"], []):
        if str(t.get("id")) == tid:
            rec = t
            break
    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
    base = max([t.get("likeCount", 0) for t in raw[q["handle"]]] or [0])
    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 ratio_vs_own2nd=%.1fx  %s" % (
        q["handle"], auth, mtype, bool(rec.get("retweeted_tweet")), bool(rec.get("quoted_tweet")),
        (q["likes"] / b2 if b2 else 0), q["text"][:60].replace("\n", " ")))
