import json
from datetime import datetime, timezone, timedelta

d = json.load(open('/home/kiro/trend-engine/trends/x/raw/scan_results_2026-09-29_FULL.json'))
raw = d['raw']
qual = d['qualifying']
qh = {x['handle'].lower() for x in qual}
cutoff = datetime.now(timezone.utc) - timedelta(days=3)
print("now UTC:", datetime.now(timezone.utc).isoformat())
print("3-day cutoff:", cutoff.isoformat())
print()

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

rows = []
for h, tweets in raw.items():
    if not tweets:
        rows.append((h, "NO DATA", 0, 0))
        continue
    dates = [parse(t.get('createdAt', '')) for t in tweets]
    dates = [x for x in dates if x]
    newest = max(dates) if dates else None
    inside = sum(1 for x in dates if x >= cutoff)
    rows.append((h, newest.strftime('%Y-%m-%d %H:%M') if newest else 'n/a', inside, len(tweets)))

print("HANDLES WITH ZERO QUALIFYING -- newest fetched tweet + in-window count")
for h, newest, inside, n in sorted(rows, key=lambda r: r[1]):
    if h.lower() not in qh:
        print(f"  @{h:20s} newest={newest:16s} in-3day-window={inside}/{n}")
