#!/usr/bin/env python3
"""2026-10-24 sanity: why some zero-qualifying handles carry big all-time bests; in-window best per zero handle;
also scan raw (incl under-threshold) for offer keywords missed by the qualifying filter."""
import json, re
from datetime import datetime, timezone

D = "/home/kiro/trend-engine/trends/x/raw/scan_results_2026-10-24_FULL.json"
d = json.load(open(D))
raw, qual = d["raw"], d["qualifying"]
def pdate(s): return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")
cutoff = datetime(2026, 10, 21, 0, 0, tzinfo=timezone.utc)

zero = [h for h in raw if not any(q["handle"] == h for q in qual)]
print("=== ZERO-QUALIFYING: in-window best (explains all-time-best confusion) ===")
for h in sorted(zero):
    inw = [t for t in raw[h] if pdate(t["createdAt"]) >= cutoff and not t.get("retweeted_tweet")]
    if not inw:
        print("%-16s no in-window originals" % h); continue
    b = max(inw, key=lambda t: t.get("likeCount", 0))
    print("%-16s in-window originals=%2d best=%4dL/%7dV %s | %s" % (
        h, len(inw), b.get("likeCount",0), b.get("viewCount",0),
        pdate(b["createdAt"]).strftime("%m-%d %H:%M"), b.get("text","")[:70].replace("\n"," ")))

print("\n=== RAW-ONLY OFFER SCAN (below threshold, in-window) ===")
OFF = re.compile(r"(\d{1,3}% ?off|\bthrough (oct|nov)\b|\bdeadline\b|free credits|\bcoupon\b|\bpromo code\b|\$\d+(\.\d+)?/?(sec|month|mo)?\b|giveaway)", re.I)
qids = {q["url"].rstrip("/").split("/")[-1] for q in qual}
for h, ts in sorted(raw.items()):
    for t in ts:
        dt = pdate(t["createdAt"])
        if dt < cutoff: continue
        if str(t.get("id")) in qids: continue
        txt = t.get("text","")
        if OFF.search(txt) and not t.get("retweeted_tweet"):
            print("%-14s %s %dL/%dV | %s" % (h, dt.strftime("%m-%d %H:%M"), t.get("likeCount",0), t.get("viewCount",0), txt[:200].replace("\n"," ")))

print("\n=== WATCHLIST SANITY ===")
print("handles=%d qualifying=%d" % (len(raw), len(qual)))
for k in ("nofilmschool","CuriousRefuge","CharaspowerAI","AIWarper"):
    print("%-16s present=%s" % (k, k in raw))
