#!/usr/bin/env python3
"""Dormancy + breakdown check for 2026-10-12 run.

Reads raw[handle][] (keys: userName, likeCount, retweetCount, viewCount, createdAt,
text, retweeted_tweet) and compares newest IN-WINDOW ORIGINAL tweet against the cutoff.
Per the 2026-10-09 rule: an in-window RT reads as a post in the raw store, so the RT
filter must be applied before calling anything a threshold artefact.
"""
import json, re
from datetime import datetime, timezone, timedelta

DATE = "2026-10-12"
p = f"/home/kiro/trend-engine/trends/x/raw/scan_results_{DATE}_FULL.json"
d = json.load(open(p))
q = d["qualifying"]
raw = d["raw"]

now = datetime.now(timezone.utc)
cutoff = now.replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=3)
print(f"cutoff = {cutoff.isoformat()}  (now {now.isoformat()})")

IRRELEVANT = {"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"}
FILM_INDUSTRY = {"DiscussingFilm", "nofilmschool"}
AI_NEWS = {"OpenAI", "AnthropicAI"}

covered = {x["handle"] for x in q}
print("\n=== ZERO-QUALIFYING HANDLES: dormancy vs threshold artefact ===")
for h in d["handles"]:
    if h in covered:
        continue
    tw = raw.get(h) or []
    best_orig = None
    inwin_orig = 0
    inwin_rt = 0
    for t in tw:
        dt = None
        try:
            dt = datetime.strptime(t.get("createdAt", ""), "%a %b %d %H:%M:%S %z %Y")
        except Exception:
            pass
        is_rt = bool(t.get("retweeted_tweet")) or (t.get("text") or "").startswith("RT @")
        if dt and dt >= cutoff:
            if is_rt:
                inwin_rt += 1
            else:
                inwin_orig += 1
                if best_orig is None or dt > best_orig[0]:
                    best_orig = (dt, t)
        if dt and (best_orig is None and not is_rt) and (best_orig is None or dt > best_orig[0]):
            pass
    # newest ORIGINAL regardless of window
    newest_orig = None
    for t in tw:
        if t.get("retweeted_tweet") or (t.get("text") or "").startswith("RT @"):
            continue
        try:
            dt = datetime.strptime(t.get("createdAt", ""), "%a %b %d %H:%M:%S %z %Y")
        except Exception:
            continue
        if newest_orig is None or dt > newest_orig[0]:
            newest_orig = (dt, t)
    age = (now - newest_orig[0]).days if newest_orig else -1
    tier = "CORE" if h not in IRRELEVANT and h not in FILM_INDUSTRY and h not in AI_NEWS else ("FILM" if h in FILM_INDUSTRY else ("LAB" if h in AI_NEWS else "IRREL"))
    verdict = ""
    if inwin_orig > 0:
        verdict = "!!! IN-WINDOW ORIGINAL -> genuine threshold artefact"
    elif inwin_rt > 0:
        verdict = f"RT-ONLY IN WINDOW ({inwin_rt} rt, 0 originals) -> RT artefact, NOT threshold"
    else:
        verdict = f"OUT-OF-WINDOW ({age}d, {tier}) -> dormant/boundary flip"
    detail = ""
    if best_orig:
        t = best_orig[1]
        detail = f" best in-win orig {t.get('likeCount')}L/{t.get('viewCount')}V"
    print(f"@{h:<18} {tier:<6} newest_orig={newest_orig[0].strftime('%Y-%m-%d') if newest_orig else 'none'} age={age}d  inwin_orig={inwin_orig}{detail}  {verdict}")

# --- core top-10 cutoff ---
core = [x for x in q if x["handle"] not in IRRELEVANT and x["handle"] not in FILM_INDUSTRY and x["handle"] not in AI_NEWS]
core.sort(key=lambda x: x["score"], reverse=True)
print(f"\ncore qualifiers={len(core)}  top10 cutoff={core[9]['score']:.0f}  rank11={core[10]['score']:.0f}")
print("core top 10:", ", ".join(f"@{x['handle']}({x['score']:.0f})" for x in core[:10]))

# --- filtered AI-relevant full list ---
AI_TERMS = re.compile(r"\b(ai\b|a\.i\.|seedance|kling|runway|veo|sora|minimax|hailuo|higgsfield|"
                      r"midjourney|nano ?banana|gpt-?[0-9.]*|chatgpt|gemini|generative|"
                      r"render|vfx|ai film|aifilm|grokbot|motion capture|deepfake|davinci|"
                      r"blender|autocad|storyboard|(ai|3d|generated) ?animation|diffusion|comfyui|"
                      r"(?<!\bfilm )(?<!\bfilm-)(?<!\bscreenplay )(?<!\bscreenplay-)\bprompt)", re.I)
print("\n=== ALL FILTERED AI-TERM MATCHES ===")
for x in q:
    if x["handle"] in IRRELEVANT:
        m = AI_TERMS.search(x["text"])
        if m:
            print(f"@{x['handle']}|term={m.group(0)}|{x['likes']}L/{x['views']}V|{x['text'][:120]}")

print("\n=== FILM-INDUSTRY TIER COUNTS ===")
from collections import Counter
c = Counter(x["handle"] for x in q)
for h in FILM_INDUSTRY:
    print(f"@{h}: {c.get(h,0)} qualifying")
