#!/usr/bin/env python3
"""Deep pass for 2026-10-25: freshness, dormancy (originals only), core top 10 verify,
tier-2, new-since-yesterday, offer grep."""
import json, re, sys
from datetime import datetime, timezone, timedelta

RAW = "/home/kiro/trend-engine/trends/x/raw"
D = json.load(open(f"{RAW}/scan_results_2026-10-25_FULL.json"))
P = json.load(open(f"{RAW}/scan_results_2026-10-24_FULL.json"))

IRRELEVANT = {
    "AIBuzzNews","AIGuideHQ","AITECHio","AIFrontliner","TheAIColony","AITOPIAai","GoogleLabs",
    "GoogleAI","DonutAI","cladlabs","FaithJaiye2187","KammelaBala","MyStageAI","AlexStudio44",
    "AI_with_jasmin","Ai_Vaidehi","ChrisLaubAI","CINEMA505","OpenAI","AnthropicAI","AIScottAdams",
    "AISuperDomain","AI_Jasonyu","AIByLeo",
}
FILM = {"DiscussingFilm","nofilmschool"}

now = datetime.now(timezone.utc)
cutoff = now.replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=3)

def pd(s):
    try:
        return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")
    except Exception:
        return None

print("=== FRESHNESS (newest fetched per handle, hours old) ===")
fresh = []
for h, tw in D["raw"].items():
    dts = [pd(t.get("createdAt","")) for t in tw]
    dts = [d for d in dts if d]
    if not dts:
        print(f"  @{h}: no parseable dates"); continue
    mx, mn = max(dts), min(dts)
    inw = sum(1 for d in dts if d >= cutoff)
    age = (now - mx).total_seconds()/3600
    fresh.append((age, h, len(tw), inw, mn, mx))
fresh.sort()
print("  -- freshest 8 --")
for age,h,n,inw,mn,mx in fresh[:8]:
    print(f"  @{h}: {age:.1f}h old, n={n}, {inw} in-window, {mn:%m-%d %H:%M} -> {mx:%m-%d %H:%M}")
print("  -- stalest 10 --")
for age,h,n,inw,mn,mx in fresh[-10:]:
    print(f"  @{h}: {age:.1f}h old, n={n}, {inw} in-window, {mn:%m-%d %H:%M} -> {mx:%m-%d %H:%M}")

print("\n=== DORMANCY (originals only, RT filtered) ===")
qual_handles = {q["handle"] for q in D["qualifying"]}
for h in sorted(D["raw"]):
    origs = [t for t in D["raw"][h] if not t.get("retweeted_tweet")
             and not (t.get("text") or "").startswith("RT @") and t.get("type") != "retweet"]
    if not origs:
        print(f"  @{h}: 0 originals of {len(D['raw'][h])} fetched"); continue
    o = sorted(origs, key=lambda t: pd(t.get("createdAt","")) or now)
    newest = pd(o[-1].get("createdAt",""))
    best = max(o, key=lambda t: (t.get("likeCount") or 0) + 2*(t.get("retweetCount") or 0) + 3*(t.get("replyCount") or 0) + (t.get("viewCount") or 0)/100)
    inw_o = [t for t in o if (pd(t.get("createdAt","")) or now) >= cutoff]
    if h in qual_handles and inw_o:
        continue
    age = (now-newest).total_seconds()/86400 if newest else -1
    print(f"  @{h}: {len(o)} originals/{len(D['raw'][h])}, newest {newest:%Y-%m-%d %H:%M} ({age:.1f}d), "
          f"in-window originals {len(inw_o)}, best-in-window "
          f"{max([(t.get('likeCount') or 0) for t in inw_o], default=0)}L/"
          f"{max([(t.get('viewCount') or 0) for t in inw_o], default=0)}V")

print("\n=== CORE TOP 12 VERIFY (raw author/media/baseline) ===")
core = [q for q in D["qualifying"] if q["handle"] not in IRRELEVANT and q["handle"] not in FILM]
core.sort(key=lambda x: x["score"], reverse=True)
raw_index = {}
for h, tw in D["raw"].items():
    for t in tw:
        raw_index[str(t.get("id"))] = (h, t)
for i, q in enumerate(core[:12], 1):
    tid = q["url"].rstrip("/").split("/")[-1]
    rec = raw_index.get(tid)
    base = [(t.get("likeCount") or 0) for t in D["raw"].get(q["handle"], [])]
    bmax = max([b for b in base if b != q["likes"]], default=0) or 1
    if not rec:
        print(f"  #{i} @{q['handle']} {q['likes']}L/{q['views']}V -- record NOT FOUND in raw")
        continue
    h, t = rec
    media = t.get("media") or []
    mt = media[0].get("type") if media else "none"
    ratio = q["likes"]/bmax
    print(f"  #{i} @{q['handle']} {q['likes']}L/{q['rt']}RT/{q['replies']}R/{q['views']}V score={q['score']:.0f} "
          f"author={t.get('author',{}).get('userName')} media={mt} RT={bool(t.get('retweeted_tweet'))} "
          f"reply={t.get('isReply')} quote={bool(t.get('quoted_tweet'))} ratio={ratio:.1f}x")

print("\n=== TIER-2 (as defined in runner) ===")
TIER2 = ["CuriousRefuge","paultrillo","JeffSynthesized","ai_filmmaker","Magiermogul","JiamingSuen","Chandan_Perla"]
silent = 0
for h in TIER2:
    qs = [x for x in D["qualifying"] if x["handle"] == h]
    if not qs:
        silent += 1
        origs = [t for t in D["raw"].get(h,[]) if not t.get("retweeted_tweet") and not (t.get("text") or "").startswith("RT @")]
        dts = sorted([pd(t.get("createdAt","")) for t in origs if pd(t.get("createdAt",""))])
        newest = dts[-1] if dts else None
        inw = [t for t in origs if (pd(t.get("createdAt","")) or now) >= cutoff]
        best_in = max([(t.get("likeCount") or 0) for t in inw], default=0)
        best_in_v = max([(t.get("viewCount") or 0) for t in inw], default=0)
        nstr = f"{newest:%Y-%m-%d %H:%M}" if newest else "none"
        print(f"  @{h}: SILENT, {len(origs)} originals/{len(D['raw'].get(h,[]))}, newest {nstr}, in-window {len(inw)}, best {best_in}L/{best_in_v}V")
    else:
        b = max(qs, key=lambda x: x["score"])
        print(f"  @{h}: {len(qs)} qualifying, best {b['likes']}L/{b['views']}V {b['date']} {b['text'][:90]}")
print(f"  silent {silent}/{len(TIER2)}")

print("\n=== NEW SINCE YESTERDAY (all in-window qualifying) ===")
prev_ids = {q["url"].rstrip("/").split("/")[-1] for q in P["qualifying"]}
new = [q for q in D["qualifying"] if q["url"].rstrip("/").split("/")[-1] not in prev_ids]
new.sort(key=lambda x: x["score"], reverse=True)
print(f"  {len(new)} posts not in yesterday's qualifying set")
for q in new[:30]:
    tag = "CORE" if q["handle"] not in IRRELEVANT and q["handle"] not in FILM else ("FILM" if q["handle"] in FILM else "FILT")
    print(f"  [{tag}] @{q['handle']} {q['likes']}L/{q['views']}V {q['date']} :: {q['text'][:150]}")
    print(f"      {q['url']}")
