#!/usr/bin/env python3
"""Offers / arbitrage / novelty sweep 2026-11-11."""
import os, re, json
from datetime import datetime, timezone

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

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

cur, prev, prev2 = load(TODAY), load(PREV), load(PREV2)
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

CUTOFF = datetime(2026, 11, 8, tzinfo=timezone.utc)

# in-window originals
orig = []
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 not d or d < CUTOFF: continue
        orig.append(dict(handle=h, text=t.get("text") or "", created=d,
                         likes=t.get("likeCount") or 0, views=t.get("viewCount") or 0,
                         rt=t.get("retweetCount") or 0,
                         media=(t.get("media") or [{}])[0].get("type") if t.get("media") else None,
                         isReply=bool(t.get("isReply") or t.get("inReplyToId")),
                         qt=t.get("quoted_tweet"), id=t.get("id")))
print(f"IN-WINDOW ORIGINALS: {len(orig)}")

PATS = {
 "free_trial": r"\b(free|FREE|no cost|zero credit|unlimited|UNLIMITED|trial|gratis)\b",
 "discount": r"\b(\d{1,2}%\s*(off|discount)|half (the )?price|BOGO|buy 1 get 1|price of 1|deal|sale|promo|coupon|code [A-Z0-9]{3,})\b",
 "price_rate": r"\$\s?\d+(\.\d+)?\s*(/|per\s*)(sec|second|min|minute|clip|video|image|task|gen)",
 "date_bounded": r"\b(through|until|ends?|before|expires?|last day|closes?)\b.{0,40}\b(\d{1,2}\s*(Nov|December|Dec)|Nov(ember)? \d{1,2}|\d{1,2}(st|nd|rd|th))\b",
 "credits": r"\b(\d+\s*(free\s*)?(credits?|generations?|gens?)|credit pack|bonus credits)\b",
 "contest": r"\b(contest|competition|challenge|hackathon|awards?|prize|bounty|grant)\b",
 "open_weights": r"\b(open[\s-]?(weights?|sourc\w+)|github|hugging\s?face|huggingface|comfyui|diffusers|apache|MIT licen\w+)\b",
 "launch": r"\b(now available|is LIVE|launch\w*|introduc\w+|announc\w+|ships?|released?|out of (early access|beta)|beta now|new in)\b",
}
print("\n===== OFFER / ARBITRAGE GREP =====")
for name, pat in PATS.items():
    hits = [o for o in orig if re.search(pat, o["text"], re.I)]
    hits.sort(key=lambda x: -x["likes"])
    print(f"\n-- {name}: {len(hits)} hits --")
    for o in hits[:12]:
        print(f"  @{o['handle']:16s} {o['created'].strftime('%m-%d %H:%M')} {o['likes']:>5}L/{o['views']:>8}V "
              f"| {re.sub(r'\\s+',' ',o['text'])[:190]}")

# --- new since yesterday (id diff) ---
pm = {t["url"].rsplit("/", 1)[-1] for t in prev["qualifying"]}
p2m = {t["url"].rsplit("/", 1)[-1] for t in prev2["qualifying"]}
print("\n\n===== NEW IN QUALIFYING SET (not present 11-10) =====")
new = [t for t in cur["qualifying"] if t["url"].rsplit("/", 1)[-1] not in pm]
new.sort(key=lambda x: -x["score"])
print(f"count {len(new)}")
for t in new[:30]:
    print(f"  @{t['handle']:16s} {t['date']} {t['likes']:>6}L/{t['views']:>9}V "
          f"| {re.sub(r'\\s+',' ',t['text'])[:170]}")
    print(f"      {t['url']}")

# --- key story posts full text ---
KEY = {
 "2119875089974104489": "perplexity Fable5.2 WANDR",
 "2120209857622253674": "perplexity Gemini4Carbon",
 "2120187753329791341": "GoogleAI Gemini 4 Carbon",
 "2120117722230968385": "Anthropic IPO roadshow",
 "2120216592395174382": "OpenAI Businesses in ChatGPT",
 "2119863091894898920": "AIWarper MiniMax H3",
 "2119878523750162775": "TSUBAKI MiniMax H3 poster",
}
print("\n\n===== KEY POST FULL TEXT (raw) =====")
for h, tw in cur["raw"].items():
    for t in tw:
        tid = str(t.get("id"))
        if tid in KEY:
            print(f"\n[{KEY[tid]}] @{h} {t.get('createdAt')} {t.get('likeCount')}L/{t.get('viewCount')}V "
                  f"qt={bool(t.get('quoted_tweet'))}")
            print("  TEXT:", re.sub(r"\s+", " ", (t.get("text") or ""))[:1500])
            qt = t.get("quoted_tweet")
            if qt:
                print("  QUOTED:", re.sub(r"\s+", " ", (qt.get("text") or ""))[:600])

# --- MiniMax H3 Image cluster (out of early access?) ---
print("\n\n===== MINIMAX H3 CLUSTER =====")
for o in sorted([o for o in orig if re.search(r"minimax|h3 image|H3Image", o["text"], re.I)],
                key=lambda x: x["created"]):
    print(f"  @{o['handle']:16s} {o['created'].strftime('%m-%d %H:%M')} {o['likes']:>5}L/{o['views']:>8}V "
          f"| {re.sub(r'\\s+',' ',o['text'])[:180]}")

# --- Gemini 4 Carbon cluster ---
print("\n\n===== GEMINI 4 CARBON CLUSTER =====")
for o in sorted([o for o in orig if re.search(r"gemini\s*4|carbon", o["text"], re.I)],
                key=lambda x: -x["likes"])[:14]:
    print(f"  @{o['handle']:16s} {o['created'].strftime('%m-%d %H:%M')} {o['likes']:>5}L/{o['views']:>8}V "
          f"| {re.sub(r'\\s+',' ',o['text'])[:200]}")

# --- per-vendor freshness (newest ORIGINAL) ---
VEND = ["higgsfield", "higgsfield_ai", "runwayml", "pika_labs", "Kling_ai", "Flovaai",
        "openart_ai", "perplexity_ai", "AIWarper", "CharaspowerAI", "ElsaSofia__AI",
        "AI__TSUBAKI", "EHuanglu", "azed_ai", "c_valenzuelab", "heyglif", "ItsMaryamAi",
        "CuriousRefuge", "WuxiaRocks", "venturetwins"]
print("\n\n===== VENDOR FRESHNESS (newest ORIGINAL, hours) =====")
for v in VEND:
    tw = cur["raw"].get(v) or []
    o = [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")
         and not t.get("inReplyToId")]
    ds = [pdate(t.get("createdAt", "")) for t in o]
    ds = [d for d in ds if d]
    if not ds:
        print(f"  @{v:16s} NO ORIGINALS n={len(tw)}"); continue
    hrs = (now - max(ds)).total_seconds() / 3600
    inw = len([d for d in ds if d >= CUTOFF])
    print(f"  @{v:16s} newest {max(ds).strftime('%m-%d %H:%M')} {hrs:7.1f}h  origs_in_window={inw}")

# --- 15-run trend ---
print("\n\n===== RUN TREND =====")
import glob
for f in sorted(glob.glob(f"{RAW}/scan_results_*_FULL.json"))[-15:]:
    d = json.load(open(f))
    print(f"  {d['date']}: {len(d['qualifying']):>4} qualifying / {len(d['handles'])} scanned / {len(d['failures'])} fail")
