#!/usr/bin/env python3
"""X monitor deep audit 2026-10-28: corrected buckets, top verification, freshness, dormancy."""
import json, re, os
from datetime import datetime, timezone, timedelta

RAW = "/home/kiro/trend-engine/trends/x/raw"
D = json.load(open(f"{RAW}/scan_results_2026-10-28_FULL.json"))
Y = json.load(open(f"{RAW}/scan_results_2026-10-27_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_INDUSTRY = {"DiscussingFilm","nofilmschool"}
LABS = {"OpenAI","AnthropicAI","NVIDIAAI"}
# corrected off-topic exclusions (open item -- not yet in scripts)
OFFTOPIC = {"interesting_aIl","hubermanlab"}

cutoff = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=3)
q = D["qualifying"]
raw = D["raw"]

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

print(f"CUTOFF {cutoff.isoformat()}   qualifying {len(q)}   handles {len(D['handles'])}")

# ---- bucket counts ----
core  = [x for x in q if x["handle"] not in IRRELEVANT and x["handle"] not in FILM_INDUSTRY
         and x["handle"] not in OFFTOPIC and x["handle"] not in LABS]
off   = [x for x in q if x["handle"] in OFFTOPIC]
film  = [x for x in q if x["handle"] in FILM_INDUSTRY]
irr   = [x for x in q if x["handle"] in IRRELEVANT]
labs  = [x for x in q if x["handle"] in LABS]
print(f"BUCKETS core {len(core)} | offtopic-excluded {len(off)} | film {len(film)} | irrelevant {len(irr)} | labs {len(labs)}")

core.sort(key=lambda x: x["score"], reverse=True)
print("\n=== CORRECTED CORE TOP 20 ===")
for i, x in enumerate(core[:20], 1):
    print(f"{i}|@{x['handle']}|{x['likes']}L|{x['rt']}RT|{x['replies']}R|{x['views']}V|{x['date']}|{x['score']:.0f}|{x['text'][:150]}")
print("cutoff rank10", f"{core[9]['score']:.0f}", " rank11", f"{core[10]['score']:.0f}", f"@{core[10]['handle']}")

# ---- verify top 12 against raw ----
print("\n=== TOP-12 RAW VERIFICATION ===")
for i, x in enumerate(core[:12], 1):
    tid = x["url"].rsplit("/",1)[-1]
    h = x["handle"]
    match = None
    for r in raw.get(h, []):
        if str(r.get("id")) == tid:
            match = r; break
    if not match:
        print(f"{i} @{h} NOT FOUND IN RAW {tid}"); continue
    media = match.get("media") or []
    mtype = media[0].get("type") if media else "none"
    lr = (x["likes"]/x["views"]*100) if x["views"] else 0
    print(f"{i} @{h} author={match.get('author',{}).get('userName')} media={mtype} "
          f"rt_flag={bool(match.get('retweeted_tweet'))} quote={bool(match.get('quoted_tweet'))} "
          f"reply={bool(match.get('isReply'))} like_rate={lr:.2f}%")

# ---- freshness ----
print("\n=== FRESHNESS (hours since newest fetched) ===")
now = datetime.now(timezone.utc)
fresh = []
for h, tws in raw.items():
    if not tws: continue
    ds = [parse_d(t["createdAt"]) for t in tws if t.get("createdAt")]
    if not ds: continue
    newest = max(ds); oldest = min(ds)
    inwin = sum(1 for d in ds if d >= cutoff)
    fresh.append((h, (now-newest).total_seconds()/3600, inwin, len(tws),
                  newest.astimezone(timezone.utc).strftime("%m-%d %H:%M"),
                  oldest.astimezone(timezone.utc).strftime("%m-%d %H:%M")))
fresh.sort(key=lambda z: z[1])
print("FRESHEST:", ", ".join(f"@{h} {hr:.1f}h" for h,hr,_,_,_,_ in fresh[:12]))
print("STALEST:", ", ".join(f"@{h} {hr:.1f}h" for h,hr,_,_,_,_ in fresh[-18:]))
for h,hr,iw,n,nw,od in fresh:
    if h in ("higgsfield_ai","higgsfield","Flovaai","runwayml","venturetwins","interesting_aIl"):
        print(f"  key @{h}: newest {nw} ({hr:.1f}h) n={n} in-window={iw} span {od}->{nw}")

# ---- new handle profiling ----
print("\n=== NEW HANDLES ===")
yest = set(Y["handles"])
for h in sorted(set(D["handles"]) - yest):
    tws = raw.get(h, [])
    print(f"@{h}: fetched {len(tws)}")
    for t in tws:
        dt = parse_d(t["createdAt"])
        med = (t.get("media") or [{}])[0].get("type","none")
        print(f"   {dt.astimezone(timezone.utc):%m-%d %H:%M} {t.get('likeCount',0)}L/{t.get('viewCount',0)}V "
              f"media={med} rt={bool(t.get('retweeted_tweet'))} | {(t.get('text') or '')[:160]}")

# ---- tier-2 / zero-qualifying dormancy: originals only ----
print("\n=== ZERO-QUALIFYING DORMANCY (originals only) ===")
active = {x["handle"] for x in q}
for h in D["handles"]:
    if h in active: continue
    tws = raw.get(h, [])
    origs = [t for t in tws if not t.get("retweeted_tweet") and t.get("type") != "retweet"
             and not (t.get("text") or "").startswith("RT @")]
    if not origs:
        print(f"@{h}: ZERO originals in {len(tws)} fetched"); continue
    newest = max(parse_d(t["createdAt"]) for t in origs)
    hrs = (now-newest).total_seconds()/3600
    inwin = [t for t in origs if parse_d(t["createdAt"]) >= cutoff]
    best = max((t.get("likeCount",0) for t in inwin), default=None)
    verdict = "DORMANT" if hrs > (now-cutoff).total_seconds()/3600 else ("IN-WINDOW under bar" if best is not None else "in-window, 0 orig")
    print(f"@{h}: newest original {newest.astimezone(timezone.utc):%m-%d %H:%M} ({hrs:.1f}h) "
          f"orig={len(origs)}/{len(tws)} inwin={len(inwin)} bestW={best} -> {verdict}")

# ---- prompt-reply harvest ----
print("\n=== PROMPT-REPLY HARVEST (isReply records) ===")
for h in ("ItsMaryamAi","Soaima_Ai","ElsaSofia__AI","AI__TSUBAKI","CharaspowerAI"):
    reps = [t for t in raw.get(h, []) if t.get("isReply")]
    print(f"@{h}: {len(reps)} replies")
    for t in sorted(reps, key=lambda z: z.get("likeCount",0), reverse=True)[:2]:
        print(f"   {parse_d(t['createdAt']).astimezone(timezone.utc):%m-%d %H:%M} {t.get('likeCount',0)}L | {(t.get('text') or '')[:220]}")
