#!/usr/bin/env python3
import json, re
from datetime import datetime, timezone

D="2026-11-08"; PREV="2026-11-07"
RAW="/home/kiro/trend-engine/trends/x/raw/"
d=json.load(open(RAW+f"scan_results_{D}_FULL.json"))
p=json.load(open(RAW+f"scan_results_{PREV}_FULL.json"))
raw=d["raw"]; qual=d["qualifying"]
def pdt(s): return datetime.strptime(s,"%a %b %d %H:%M:%S %z %Y")
now=datetime.now(timezone.utc)
CUT=now.replace(hour=0,minute=0,second=0,microsecond=0)  # 11-08 00:00
CUT=CUT.replace(day=5)
print("cutoff:",CUT)

# in-window originals
orig=[]
for h,tw in raw.items():
    for t in tw:
        if t.get("retweeted_tweet"): continue
        if t.get("type")!="tweet": continue
        if pdt(t["createdAt"]) < CUT: continue
        orig.append((h,t))
print("in-window originals:",len(orig))

PATTERNS={
 "free_credits":r"free (?:generations|credits|trial|access)|credits? free|zero credit|no credit cost|unlimited",
 "discount":r"\b\d{1,2}% off|half price|half the price|\bBOGO\b|buy 1 get 1|2 for 1|2 years",
 "blackfriday":r"black friday|#capcutblackfriday|cyber monday",
 "launch_special":r"launch special|launch offer|early access|waitlist|private beta|coming soon",
 "contest":r"contest|challenge|award|prize|giveaway|submit|submissions",
 "pricing_rate":r"\$\s?0?\.\d{2,3}\s*/?\s*(?:per\s*)?sec|\$\d+(?:\.\d+)?/sec|per second|/sec\b",
 "api":r"\bAPI\b|open weights|hugging ?face|github|comfyui|diffusers|weights",
 "deadline":r"through (?:nov|dec|jan)|until (?:nov|dec|jan)|ends? (?:nov|dec|jan)|deadline|closes",
 "resolve":r"davinci|resolve 2\d",
 "topaz":r"topaz",
 "topview":r"topview",
 "nebius":r"nebius",
 "magnific":r"magnific",
 "higgsfield_offer":r"genjutsu|content factory|ads studio|cashback|miracle",
 "runway_unlimited":r"runway unlimited|unlimited kling",
 "pixverse":r"pixverse",
 "glif":r"glif",
 "chroma":r"chroma award|chromaawards",
 "sora":r"sora",
 "veo":r"\bveo\b",
 "seedance3":r"seedance (?:3|4)",
 "kling5":r"kling 5",
 "dreamina":r"dreamina",
 "capcut":r"capcut",
 "openart":r"openart",
 "kling_event":r"christmas creator|kling.*event",
}
print("\n=== OFFER/EARN PATTERN HITS ===")
for name,pat in PATTERNS.items():
    hits=[(h,t) for h,t in orig if re.search(pat,t["text"],re.I)]
    if not hits: print(f"{name}: 0"); continue
    print(f"\n{name}: {len(hits)}")
    for h,t in sorted(hits,key=lambda x:-x[1]["likeCount"])[:6]:
        print(f"  @{h} {t['createdAt'][:16]} {t['likeCount']}L/{t['viewCount']}V :: {t['text'][:200].replace(chr(10),' ')}")
