#!/usr/bin/env python3
"""X monitor 2026-10-19 deep checks: dormancy (originals-only), fetch-window roll-off,
offer grep, top-10 arrival check, outlier/author verification."""
import json, re, os
from datetime import datetime, timezone, timedelta

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

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

now = datetime.now(timezone.utc)
cutoff = datetime(now.year, now.month, now.day, tzinfo=timezone.utc) - timedelta(days=3)
print(f"NOW {now.isoformat()}  CUTOFF {cutoff.isoformat()}  raw_handles={len(raw)} qualifying={len(q)} failures={d.get('failures')!r}")

IRRELEVANT = {"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"}
FILM = {"DiscussingFilm","nofilmschool"}

# ---- TZ-ONLY counts per bucket
core_n = film_n = irr_n = labs_n = 0
for t in q:
    h = t["handle"]
    if h in FILM: film_n += 1
    elif h in IRRELEVANT:
        if h in {"OpenAI","AnthropicAI"}: labs_n += 1
        else: irr_n += 1
    else: core_n += 1
print(f"BUCKETS core={core_n} film={film_n} irrelevant={irr_n} labs={labs_n}")

# ---- fetch window per handle (datetime parse, NOT string sort)
print("\n===== FETCH WINDOW (min/max createdAt, datetime-parsed) =====")
rows = []
for h, tw in raw.items():
    ds = [dt(t.get("createdAt","")) for t in tw]
    ds = [x for x in ds if x]
    if not ds: continue
    rows.append((h, len(tw), min(ds), max(ds), min(ds) >= cutoff))
rows.sort(key=lambda r: r[2])
for h, n, mn, mx, allin in rows:
    flag = "ALL-IN-WINDOW" if allin else f"ROLLED ({mn.strftime('%Y-%m-%d %H:%M')} < cutoff)"
    print(f"{h:18s} n={n:2d} oldest={mn.strftime('%Y-%m-%d %H:%M')} newest={mx.strftime('%Y-%m-%d %H:%M')}  {flag}")

# ---- dormancy: originals only inside window
print("\n===== DORMANCY (originals only, RT-filtered) =====")
qual_handles = {t["handle"] for t in q}
zero = [h for h in raw if h not in qual_handles]
for h in sorted(zero):
    tw = raw[h]
    orig = [t for t in tw if t.get("retweeted_tweet") is None and not str(t.get("text","")).startswith("RT @")]
    ods = sorted([dt(t.get("createdAt","")) for t in orig if dt(t.get("createdAt",""))])
    inwin = [t for t in orig if dt(t.get("createdAt","")) and dt(t.get("createdAt","")) >= cutoff]
    newest = ods[-1].strftime("%Y-%m-%d %H:%M") if ods else "no original"
    age = (now - ods[-1]).days if ods else None
    if inwin:
        best = max(inwin, key=lambda t: t.get("likeCount",0))
        verdict = (f"IN-WINDOW THRESHOLD ARTEFACT n={len(inwin)} "
                   f"best {best.get('likeCount')}L/{best.get('viewCount')}V :: {str(best.get('text'))[:90]!r}")
    else:
        verdict = f"OUT-OF-WINDOW (newest orig {newest}, {age}d)"
    print(f"{h:18s} fetched={len(tw):2d} originals={len(orig):2d} | {verdict}")

# ---- offer grep
print("\n===== OFFER / ARBITRAGE GREP =====")
OFFERS = {
 "Flova Seedance 80%": [r"seedance 2\.5", r"80% ?off|save up to 80|up to 80%"],
 "Higgsfield Genjutsu API": [r"genjutsu"],
 "Higgsfield Wan 3.0": [r"wan 3\.0"],
 "Higgsfield API cashback": [r"cashback"],
 "Higgsfield Ads Studio": [r"ads studio|ad multiplier"],
 "Higgsfield AI Influencer": [r"ai influencer"],
 "FLUX 3 Upscaler": [r"flux 3 upscal|upscaler"],
 "Higgsfield Academy": [r"higgsfield academy|higgsfield\.ai/academy"],
 "CapCut Black Friday": [r"capcut", r"60% off pro|40% off ultra"],
 "Dreamina $1.50": [r"\$1\.50", r"dreamina.{0,40}basic"],
 "Dreamina Originals": [r"dreamina originals"],
 "Dolphin AI referral": [r"XX-AYCMR8|dolphin ai"],
 "Air unlimited": [r"air unlimited|unlimited credits"],
 "Kling 4.0": [r"kling 4\.0"],
 "DeepSeek V4.1 Flash": [r"deepseek v4\.1 flash"],
 "Seedance 2.5": [r"seedance 2\.5"],
 "freebeat lyric challenge": [r"freebeat|lyric video challenge"],
 "AITECHio ACN/GPU credits": [r"\bacn\b", r"gpu credits"],
}
alltext = [(t["handle"], t.get("text",""), t.get("viewCount",0), t.get("likeCount",0), dt(t.get("createdAt",""))) for h,tw in raw.items() for t in tw]
for name, pats in OFFERS.items():
    hits = []
    for h, txt, v, l, c in alltext:
        low = txt.lower()
        if all(re.search(p, low, re.I) for p in pats):
            hits.append((h, l, v, c, txt))
    if hits:
        print(f"\n[{name}] {len(hits)} hit(s)")
        for h,l,v,c,txt in sorted(hits, key=lambda x:-x[2])[:6]:
            print(f"   {h} {l}L/{v}V {c.strftime('%m-%d %H:%M') if c else '?'} :: {' '.join(str(txt).split())[:220]}")
    else:
        print(f"\n[{name}] 0 hits")

# ---- outlier / high-baseline check on core top 10
print("\n===== CORE TOP 10 -- author + media + personal baseline =====")
core_q = [t for t in q if t["handle"] not in IRRELEVANT and t["handle"] not in FILM]
core_q.sort(key=lambda t: -t["score"])
for i, t in enumerate(core_q[:10], 1):
    h = t["handle"]
    tid = t["url"].rsplit("/",1)[-1]
    match = [x for x in raw.get(h,[]) if str(x.get("id")) == tid]
    m = match[0] if match else {}
    author = (m.get("author") or {}).get("userName") if isinstance(m.get("author"), dict) else m.get("author")
    media = m.get("media")
    rt = m.get("retweeted_tweet")
    others = [x.get("likeCount",0) for x in raw.get(h,[]) if str(x.get("id")) != tid]
    base = max(others) if others else 0
    ratio = (t["likes"]/base) if base else float('inf')
    print(f"{i}. @{h} {t['likes']}L/{t['views']}V score={t['score']:.0f} {t['date']}")
    print(f"   author={author!r} media={media} retweeted={'yes' if rt else 'no'} acct_maxOther={base}L ratio={ratio:.1f}x")
    print(f"   {' '.join(str(t['text']).split())[:150]}")

# ---- arrival check: any auto-discovered handle inside core top 10
print("\n===== ARRIVAL CHECK =====")
print("core top-10 handles:", [t["handle"] for t in core_q[:10]])
for h in sorted(IRRELEVANT):
    n = sum(1 for t in q if t["handle"]==h)
    if n: print(f"  filtered-tier {h}: {n} qualifying (correctly excluded)")
