#!/usr/bin/env python3
"""Glif / UGC-ads grep + a few remaining checks for 2026-10-31."""
import json, re
from datetime import datetime, timezone, timedelta

RAW = "/home/kiro/trend-engine/trends/x/raw"
D = json.load(open(f"{RAW}/scan_results_2026-10-31_FULL.json"))
raw = D["raw"]
cutoff = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=3)

def pd(s):
    return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")

inwin = []
for h, tw in raw.items():
    for t in tw:
        try:
            d = pd(t.get("createdAt", ""))
        except Exception:
            continue
        if d >= cutoff:
            inwin.append((h, t, d))

print(f"IN-WINDOW (all incl RTs) {len(inwin)}")
for name, pat in {
    "glif": r"\bglif\b",
    "ugc": r"\bugc\b|user.generated|unboxing|grwm",
    "modeling_agency": r"modeling agency|ai model agency|ai models",
    "ugc_ads": r"ugc ad",
    "ip_safety": r"ip safety|copyright",
    "runway_agent": r"runway agent|runway agent",
    "kling_originals": r"kling ai originals",
    "halloween": r"halloween",
}.items():
    rx = re.compile(pat, re.I)
    hits = [(h, t) for h, t, d in inwin if rx.search(t.get("text") or "")]
    print(f"\n{name}: {len(hits)}")
    for h, t in hits[:6]:
        print(f"  @{h} {pd(t['createdAt']).strftime('%m-%d %H:%M')} {t.get('likeCount')}L/{t.get('viewCount')}V "
              f"rt={bool(t.get('retweeted_tweet'))} | {(t.get('text') or '')[:320].replace(chr(10),' ')}")

print("\n=== FULL: venturetwins Glif RT + quote ===")
for t in raw.get("venturetwins", []):
    if "glif" in (t.get("text") or "").lower():
        print(f"--- {t['createdAt']} {t.get('likeCount')}L/{t.get('viewCount')}V")
        print(t.get("text"))
        qt = t.get("quoted_tweet")
        if qt:
            print("QUOTED:", json.dumps(qt, ensure_ascii=False)[:1200])
