#!/usr/bin/env python3
"""Detail pulls for the 2026-10-04 report."""
import json
from datetime import datetime, timezone, timedelta

d = json.load(open("/home/kiro/trend-engine/trends/x/raw/scan_results_2026-10-04_FULL.json"))
q = d["qualifying"]
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

print("=== paultrillo raw tweets (in-window engagement) ===")
for t in d["raw"].get("paultrillo", []):
    dt = pdate(t.get("createdAt", ""))
    if dt and dt >= cutoff:
        print(f"  {dt.strftime('%m-%d %H:%M')} | {t.get('likeCount')}L {t.get('viewCount')}V | {(t.get('text') or '')[:110]}")

print("\n=== CuriousRefuge / Chandan_Perla newest in-window ===")
for h in ("CuriousRefuge", "Chandan_Perla", "Magiermogul", "JiamingSuen", "ai_filmmaker"):
    tw = d["raw"].get(h, [])
    dts = [(pdate(t.get("createdAt","")), t) for t in tw]
    dts = [(x, t) for x, t in dts if x]
    dts.sort(key=lambda z: z[0], reverse=True)
    if dts:
        x, t = dts[0]
        print(f"  @{h}: newest {x.strftime('%m-%d %H:%M')} | {t.get('likeCount')}L {t.get('viewCount')}V | {(t.get('text') or '')[:100]}")

print("\n=== Relevant-handle top posts (core, non-Higgsfield) ===")
for h in ("CharaspowerAI", "AI__TSUBAKI", "pika_labs", "WuxiaRocks", "Flovaai", "Flovaai", "azed_ai", "Kling_ai", "EHuanglu", "runwayml", "c_valenzuelab"):
    rows = [x for x in q if x["handle"] == h]
    for x in rows[:3]:
        print(f"  @{x['handle']} | {x['likes']}L {x['rt']}RT {x['views']}V | {x['date']} | {x['text'][:150]}")
        print(f"     {x['url']}")

print("\n=== Arbitrage / free-credit / discount term scan across all qualifying ===")
import re
pat = re.compile(r"(free|discount|cashback|credit|promo|%\s*off|giveaway|open.?source|annual|yearly)", re.I)
for x in q:
    if pat.search(x["text"] or ""):
        print(f"  @{x['handle']} | {x['likes']}L {x['views']}V | {x['date']} | {x['text'][:170]}")
