#!/usr/bin/env python3
"""Quoted-tweet expansion + remaining checks 2026-11-11."""
import re, json
from datetime import datetime, timezone
RAW = "/home/kiro/trend-engine/trends/x/raw"
cur = json.load(open(f"{RAW}/scan_results_2026-11-11_FULL.json"))
CUT = datetime(2026, 11, 8, tzinfo=timezone.utc)
def pdate(s):
    try: return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")
    except Exception: return None

print("===== ALL QUOTED / RT SOURCES IN WINDOW (unique, non-empty) =====")
seen = {}
for h, tw in cur["raw"].items():
    for t in tw:
        d = pdate(t.get("createdAt", "") or "")
        if not d or d < CUT: continue
        qt = t.get("quoted_tweet") or None
        if not qt: continue
        qtext = re.sub(r"\s+", " ", (qt.get("text") or ""))
        key = qtext[:80]
        if key in seen: continue
        seen[key] = 1
        au = (qt.get("author") or {}).get("userName") or "?"
        print(f"\n  CARRIER @{h} {d.strftime('%m-%d %H:%M')} ({t.get('likeCount') or 0}L) quotes @{au}")
        print(f"    {qtext[:600]}")

print("\n\n===== HYPIT / AD-CLONER MENTIONS =====")
for h, tw in cur["raw"].items():
    for t in tw:
        txt = (t.get("text") or "") + " " + ((t.get("quoted_tweet") or {}).get("text") or "")
        if re.search(r"hypit|ad multipl|viral ad clon|competitor.s (best|ad)", txt, re.I):
            d = pdate(t.get("createdAt", "") or "")
            print(f"  @{h} {d.strftime('%m-%d %H:%M') if d else '?'} {t.get('likeCount')}L/{t.get('viewCount')}V")
            print(f"    {re.sub(r'\\s+',' ',txt)[:900]}")

print("\n\n===== RUNWAY PLUGIN-IN-CODEX / PRODUCT PHOTO TO VIDEO AD =====")
for h, tw in cur["raw"].items():
    for t in tw:
        txt = (t.get("text") or "") + " " + ((t.get("quoted_tweet") or {}).get("text") or "")
        if re.search(r"product photo into a video|runway.*(plugin|codex)|codex.*runway", txt, re.I):
            d = pdate(t.get("createdAt", "") or "")
            print(f"  @{h} {d.strftime('%m-%d %H:%M') if d else '?'} {t.get('likeCount')}L/{t.get('viewCount')}V")
            print(f"    {re.sub(r'\\s+',' ',txt)[:800]}")

print("\n\n===== OPENAI BUSINESS AGENT / BUSINESS HUB FULL =====")
for h, tw in cur["raw"].items():
    if h != "OpenAI": continue
    for t in tw:
        txt = re.sub(r"\s+", " ", (t.get("text") or ""))
        if re.search(r"business|agent|feed", txt, re.I):
            d = pdate(t.get("createdAt", "") or "")
            if d and d >= CUT:
                print(f"  {d.strftime('%m-%d %H:%M')} {t.get('likeCount')}L/{t.get('viewCount')}V  {txt[:700]}")

print("\n\n===== HIGGSFIELD TRIAL REPLY FULL =====")
for h in ["higgsfield", "higgsfield_ai"]:
    for t in cur["raw"].get(h) or []:
        txt = re.sub(r"\s+", " ", (t.get("text") or ""))
        if "72-hour" in txt or "trial" in txt.lower():
            d = pdate(t.get("createdAt", "") or "")
            print(f"  @{h} {d.strftime('%m-%d %H:%M') if d else '?'} {t.get('likeCount')}L  {txt[:900]}")

print("\n\n===== MINIMAX H3-IMAGE LAUNCH POST (quoted source) =====")
for h, tw in cur["raw"].items():
    for t in tw:
        qt = t.get("quoted_tweet") or {}
        qt_txt = (qt.get("text") or "")
        if re.search(r"H3-Image|H3Image", qt_txt, re.I):
            print(f"  carrier @{h} quotes @{(qt.get('author') or {}).get('userName')}: "
                  f"{re.sub(r'\\s+',' ',qt_txt)[:800]}")
            print()
            break
