#!/usr/bin/env python3
"""Offers + key-post expansion + vendor blackout + trend, 2026-11-10."""
import os, re, json
from datetime import datetime, timezone, timedelta

RAW = "/home/kiro/trend-engine/trends/x/raw"
TODAY = "2026-11-10"

def load(d):
    with open(f"{RAW}/scan_results_{d}_FULL.json") as f:
        return json.load(f)

cur = load(TODAY)
now = datetime.now(timezone.utc)
def pdate(s):
    try: return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")
    except Exception: return None

# --- in-window ORIGINALS corpus ---
cutoff = datetime(2026, 11, 7, tzinfo=timezone.utc)
corpus = []
for h, tw in cur["raw"].items():
    for t in tw:
        if t.get("retweeted_tweet") or t.get("type") == "retweet": continue
        if (t.get("text") or "").startswith("RT @"): continue
        d = pdate(t.get("createdAt",""))
        if d and d >= cutoff:
            corpus.append((h, t, d))
print(f"IN-WINDOW ORIGINALS (incl replies): {len(corpus)}")

# --- offer patterns ---
PATS = {
 "percent_off": r"(\d{1,2})\s?%\s?(off|discount)",
 "free": r"\bfree\b|\bFREE\b",
 "free_credits": r"free (credits?|generations?|trial|access)",
 "unlimited": r"unlimited",
 "days_free": r"\d+\s?days? of (free|unlimited)",
 "discount": r"\bdiscount\b|\bpromo\b|\bcoupon\b",
 "launch_special": r"launch special|2 years|buy 1|get 1 free|price of 1",
 "pricing_rate": r"\$\s?0?\.\d{2,3}\s?/?\s?(sec|second|s\b)",
 "ends": r"(through|until|ends?|runs? (to|until)|closes?)\s+\w+\s+\d{1,2}|Nov(ember)? \d{1,2}|Dec(ember)? \d{1,2}",
 "topaz": r"topaz", "resolve": r"davinci|resolve", "topview": r"topview",
 "nebius": r"nebius", "magnific": r"magnific", "chroma": r"chroma",
 "openart": r"openart", "glif": r"glif", "kling_event": r"kling.{0,20}(event|christmas|black)",
 "sora": r"sora", "veo": r"\bveo\b", "seedance3": r"seedance\s?[34]",
 "kling5": r"kling\s?5", "higgsfield_offer": r"higgsfield.{0,40}(free|unlimited|offer|beta)",
}
print("\n== OFFER GREP ==")
for name, pat in PATS.items():
    hits = [(h,t,d) for h,t,d in corpus if re.search(pat, t.get("text") or "", re.I)]
    if not hits: print(f"  {name:16s} 0"); continue
    print(f"  {name:16s} {len(hits)}")
    for h,t,d in sorted(hits, key=lambda x: -(x[1].get("likeCount") or 0))[:6]:
        print(f"     @{h:16s} {t.get('likeCount')}L/{t.get('viewCount')}V {d.strftime('%m-%d %H:%M')} "
              f"{(t.get('text') or '')[:150].replace(chr(10),' ')}")

# --- key posts full text + quoted/RT expansion ---
KEYS = {
 "2119867707546071125": "AnthropicAI Fable 5.2 + Mythos 5.2",
 "2119884452054937656": "higgsfield Fable 5.2 free unlimited",
 "2119975721909301684": "higgsfield_ai da Vinci",
 "2119875089974104489": "perplexity_ai Fable 5.2 WANDR",
 "2119947980400976267": "EHuanglu fable 5.2 disney",
 "2119090260894515273": "EHuanglu Disney ORIGINAL",
 "2119846806066733206": "venturetwins AI interviewers",
 "2119955378209484817": "venturetwins realtime avatars",
 "2119863091894898920": "AIWarper MiniMax H3 Image",
 "2119752259248722135": "AI__TSUBAKI Seedream 5.0 vs 5.5",
 "2119913172039868579": "OpenAI Control Room",
 "2119251540334776448": "ElsaSofia next gen Seedance",
}
print("\n== KEY POSTS FULL TEXT ==")
idx = {}
for h, tw in cur["raw"].items():
    for t in tw: idx[str(t.get("id"))] = (h, t)
for tid, label in KEYS.items():
    if tid not in idx:
        print(f"\n-- {label} [{tid}] NOT IN FETCHED SET --"); continue
    h, t = idx[tid]
    d = pdate(t.get("createdAt",""))
    print(f"\n-- {label} | @{h} | {d.strftime('%Y-%m-%d %H:%M') if d else '?'} | "
          f"{t.get('likeCount')}L/{t.get('retweetCount')}RT/{t.get('replyCount')}R/{t.get('viewCount')}V | "
          f"https://x.com/{h}/status/{tid}")
    for m in (t.get("media") or []):
        print(f"   media: {m.get('type')}")
    print("   " + (t.get("text") or "").replace("\n", " ")[:1200])
    for fld in ("quoted_tweet", "retweeted_tweet"):
        q = t.get(fld)
        if q:
            qa = (q.get("author") or {}).get("userName")
            print(f"   [{fld}] @{qa} {q.get('likeCount')}L/{q.get('viewCount')}V: "
                  f"{(q.get('text') or '')[:500]}")

# --- vendor blackout / resumption ---
VEND = ["higgsfield","higgsfield_ai","runwayml","pika_labs","Kling_ai","Flovaai","openart_ai",
        "perplexity_ai","heyglif","AIWarper","CharaspowerAI","EHuanglu","azed_ai","ElsaSofia__AI",
        "venturetwins","c_valenzuelab","AI__TSUBAKI","ItsMaryamAi"]
print("\n== VENDOR FRESHNESS (newest ORIGINAL, hours) ==")
for h in VEND:
    tw = cur["raw"].get(h) or []
    origs = [t for t in tw if not t.get("retweeted_tweet") and t.get("type")!="retweet"
             and not (t.get("text") or "").startswith("RT @") and not t.get("isReply")]
    ds = [pdate(t.get("createdAt","")) for t in origs]
    ds = [d for d in ds if d]
    if not ds:
        print(f"  @{h:16s} NO ORIGINALS fetched (n={len(tw)})"); continue
    nw = max(ds)
    print(f"  @{h:16s} newest orig {nw.strftime('%m-%d %H:%M')}  {(now-nw).total_seconds()/3600:6.1f}h  n={len(tw)}")

# --- trend ---
print("\n== TREND (qualifying per run) ==")
for f in sorted(os.listdir(RAW)):
    m = re.match(r"scan_results_(\d{4}-\d{2}-\d{2})_FULL\.json$", f)
    if not m: continue
    if m.group(1) < "2026-10-27": continue
    try:
        d = json.load(open(f"{RAW}/{f}"))
        print(f"  {m.group(1)}  q={len(d['qualifying']):3d}  scanned={len(d['handles'])}  fails={len(d['failures'])}")
    except Exception as e:
        print(f"  {m.group(1)}  ERR {e}")
