#!/usr/bin/env python3
"""Dormancy check for 2026-10-14, originals-only (RT filtered against raw)."""
import json, re
from datetime import datetime, timezone, timedelta

DATE = "2026-10-14"
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)

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

has = {x["handle"] for x in q}
zero = [h for h in d["handles"] if h not in has]

print(f"cutoff = {cutoff.isoformat()}")
print(f"zero-qualifying handles: {len(zero)}\n")

for h in zero:
    tw = raw.get(h, [])
    origs = []
    for t in tw:
        if t.get("retweeted_tweet") or t.get("type") == "retweet":
            continue
        if (t.get("text") or "").startswith("RT @"):
            continue
        dt = pdate(t.get("createdAt", ""))
        if dt:
            origs.append((dt, t))
    origs.sort(key=lambda x: x[0])
    if not origs:
        print(f"@{h}: NO ORIGINALS in raw store ({len(tw)} fetched, all RTs)")
        continue
    newest_dt, newest = origs[-1]
    age = (now - newest_dt).days
    inwin = [o for o in origs if o[0] >= cutoff]
    tag = "IN-WINDOW" if inwin else "out-of-window"
    print(f"@{h}: newest ORIGINAL {newest_dt.strftime('%Y-%m-%d %H:%M')} ({age}d) {tag} "
          f"| {newest.get('likeCount')}L/{newest.get('retweetCount')}RT/{newest.get('viewCount')}V "
          f"| origins={len(origs)}/{len(tw)}")
    if inwin:
        for dt, t in inwin:
            print(f"      IN-WIN {dt.strftime('%Y-%m-%d %H:%M')} {t.get('likeCount')}L/"
                  f"{t.get('retweetCount')}RT/{t.get('viewCount')}V | {(t.get('text') or '')[:110]}")
