#!/usr/bin/env python3
"""Offer/arbitrage grep + accounting + key-post full text, 2026-11-01."""
import json, re
from datetime import datetime
from collections import Counter

D = "/home/kiro/trend-engine/trends/x/raw/"
d = json.load(open(D + "scan_results_2026-11-01_FULL.json"))
q = d["qualifying"]

IRRELEVANT = {"AIBuzzNews","AIGuideHQ","AITECHio","AIFrontliner","TheAIColony","AITOPIAai",
 "GoogleLabs","GoogleAI","DonutAI","cladlabs","FaithJaiye2187","KammelaBala","MyStageAI",
 "AlexStudio44","AI_with_jasmin","Ai_Vaidehi","ChrisLaubAI","CINEMA505","AIScottAdams",
 "AISuperDomain","AI_Jasonyu","AIByLeo"}
FILM = {"DiscussingFilm","nofilmschool"}

# --- exact accounting ---
core   = [x for x in q if x["handle"] not in IRRELEVANT|FILM|{"OpenAI","AnthropicAI","interesting_aIl","hubermanlab","AIsaOneHQ","AiWithTariq"}]
offtop = [x for x in q if x["handle"]=="interesting_aIl"]
health = [x for x in q if x["handle"]=="hubermanlab"]
film   = [x for x in q if x["handle"] in FILM]
irrel  = [x for x in q if x["handle"] in IRRELEVANT]
labs   = [x for x in q if x["handle"] in {"OpenAI","AnthropicAI"}]
new    = [x for x in q if x["handle"] in {"AIsaOneHQ","AiWithTariq"}]
print("ACCOUNTING:", {k:len(v) for k,v in [("core",core),("offtopic",offtop),("health",health),
      ("film",film),("irrelevant",irrel),("labs",labs),("new",new)]})
print("SUM", len(core)+len(offtop)+len(health)+len(film)+len(irrel)+len(labs)+len(new), "vs", len(q))

# --- offer grep over IN-WINDOW ORIGINALS only ---
def pd(s):
    try: return datetime.strptime(s,"%a %b %d %H:%M:%S %z %Y").replace(tzinfo=None)
    except Exception: return None
cutoff = datetime(2026,10,29,0,0,0)
orig=[]
for h,tw in d["raw"].items():
    for t in tw:
        if t.get("retweeted_tweet") or (t.get("text") or "").startswith("RT @"): continue
        dt = pd(t.get("createdAt",""))
        if dt and dt>=cutoff: orig.append((h,t,dt))
print(f"\nIN-WINDOW ORIGINALS: {len(orig)}")

PATS = ["free","deal","discount","% off","coupon","promo","giveaway","beta","waitlist","early access",
 "unlimited","credit","launch","introducing","announc","open weight","open-source","open source",
 "\\$[0-9]","price","pricing","per second","/sec","subscription","trial","referral","affiliate",
 "contest","challenge","nov 30","november","black friday","cyber","lifetime","ltd","pre-order",
 "hire","hiring","job","api","sdk","github","huggingface","comfyui","workflow"]
seen=set()
print("\n===== OFFER / ARBITRAGE GREP =====")
for p in PATS:
    rx = re.compile(p, re.I)
    hits=[(h,t,dt) for h,t,dt in orig if rx.search(t.get("text") or "")]
    if not hits: 
        print(f"  [{p}] 0 hits")
        continue
    print(f"  [{p}] {len(hits)} hits")
    for h,t,dt in hits[:6]:
        key=t.get("id")
        if key in seen: continue
        seen.add(key)
        print(f"     @{h} {dt} {t.get('likeCount')}L/{t.get('viewCount')}V :: {(t.get('text') or '')[:190].replace(chr(10),' ')}")

# --- key posts full text ---
print("\n===== KEY POST FULL TEXT =====")
KEYS = [("pika_labs", 30), ("higgsfield", 30), ("AIsaOneHQ", 40), ("AiWithTariq", 30),
        ("NVIDIAAI", 25), ("CharaspowerAI", 25), ("EHuanglu", 25), ("venturetwins", 25),
        ("c_valenzuelab", 25), ("runwayml", 25), ("Kling_ai", 25), ("azed_ai", 25),
        ("Flovaai", 25), ("AIWarper", 25), ("openart_ai", 20), ("ItsMaryamAi", 20),
        ("ElsaSofia__AI", 20), ("Soaima_Ai", 20), ("numerai", 20), ("Teamily_AI", 20)]
for h,n in KEYS:
    tw = d["raw"].get(h,[])
    o=[t for t in tw if not t.get("retweeted_tweet") and pd(t.get("createdAt",""))and pd(t.get("createdAt",""))>=cutoff]
    o.sort(key=lambda t: pd(t.get("createdAt","")))
    print(f"\n--@{h}  ({len(o)} in-window originals)")
    for t in o:
        print(f"  {pd(t.get('createdAt',''))} | {t.get('likeCount')}L/{t.get('retweetCount')}RT/{t.get('viewCount')}V | med={[m.get('type') for m in (t.get('media') or [])]}")
        print(f"    {(t.get('text') or '')[:n+200].replace(chr(10),' ')}")
        if t.get("quoted_tweet"): print(f"    QUOTED(@{(t['quoted_tweet'].get('author') or {}).get('userName')}): {((t['quoted_tweet'].get('text') or '')[:400]).replace(chr(10),' ')}")
