#!/usr/bin/env python3
"""2026-10-22 check: core ranks 13-30, freshest in-window posts, freshness of key handles."""
import json
from datetime import datetime, timezone, timedelta

DATE = "2026-10-22"
d = json.load(open(f"/home/kiro/trend-engine/trends/x/raw/scan_results_{DATE}_FULL.json"))
q, raw = d["qualifying"], d["raw"]
now = datetime.now(timezone.utc)

FILM = {"DiscussingFilm", "nofilmschool"}
IRR = {"AIBuzzNews","AIGuideHQ","AITECHio","AIFrontliner","TheAIColony","AITOPIAai","GoogleLabs",
       "GoogleAI","DonutAI","cladlabs","FaithJaiye2187","KammelaBala","MyStageAI","AlexStudio44",
       "AI_with_jasmin","Ai_Vaidehi","ChrisLaubAI","CINEMA505","AIScottAdams","AISuperDomain",
       "AI_Jasonyu","AIByLeo"}
LABS = {"OpenAI", "AnthropicAI"}
core = sorted([x for x in q if x["handle"] not in FILM | IRR | LABS], key=lambda x: -x["score"])

print("=== CORE RANKS 13-32 ===")
for i, x in enumerate(core[12:32], 13):
    print(f"{i}|@{x['handle']}|{x['likes']}L/{x['views']}V|{x['date']}|{x['score']:.0f}| {x['text'][:120]}")

print("\n=== FRESHNESS: key handles ===")
def pdate(s):
    return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")
for h in ["higgsfield_ai", "higgsfield", "Flovaai", "Kling_ai", "runwayml", "pika_labs",
          "azed_ai", "AIWarper", "EHuanglu", "CharaspowerAI", "c_valenzuelab", "venturetwins",
          "JeffSynthesized", "CuriousRefuge", "Chandan_Perla", "WuxiaRocks"]:
    rs = raw.get(h) or []
    ds = [pdate(r["createdAt"]) for r in rs]
    if not ds:
        print(f"  @{h}: no raw"); continue
    mn, mx = min(ds), max(ds)
    inwin = sum(1 for x in ds if x >= now - timedelta(days=3))
    print(f"  @{h}: n={len(rs)} {mn:%m-%d %H:%M} -> {mx:%m-%d %H:%M} ({inwin} in-window, newest {(now-mx).total_seconds()/3600:.1f}h)")

print("\n=== FRESHEST QUALIFYING POSTS (newest 15) ===")
def qd(s):
    return datetime.strptime(s.replace(" UTC",""), "%Y-%m-%d %H:%M").replace(tzinfo=timezone.utc)
for x in sorted(q, key=lambda t: -qd(t["date"]).timestamp())[:15]:
    print(f"@{x['handle']}|{x['likes']}L/{x['views']}V|{x['date']}| {x['text'][:110]}")

print("\n=== @higgsfield_ai all 20 in-window posts (chronological) ===")
rs = sorted(raw.get("higgsfield_ai", []), key=lambda r: pdate(r["createdAt"]))
for r in rs:
    print(f"  {pdate(r['createdAt']):%m-%d %H:%M} {r.get('likeCount')}L/{r.get('viewCount')}V :: {r['text'][:110]}")
