#!/usr/bin/env python3
"""Arbitrage / free-credit grep over the day's qualifying tweets.

Prints every qualifying tweet whose text matches the offer keyword set, so the
report states confirmed-live offers from THIS window's data instead of carrying
stale ones forward. Distinguish re-confirmed (hit today) from unconfirmed.
"""
import json, re, sys
BASE = "/home/kiro/trend-engine/trends/x/raw"
DATE = sys.argv[1]
d = json.load(open(f"{BASE}/scan_results_{DATE}_FULL.json"))
q = d["qualifying"]
pat = re.compile(r"(arbitrage|free credit|free creds|unlimited|referral|% off|discount|cashback"
                 r"|promo|credits|free tier|open.?sourc|waitlist|free to try|giveaway|rate lock"
                 r"|free generation|free course|now free|for free)", re.I)
hits = [t for t in q if pat.search(t.get("text") or "")]
print(f"=== ARB/FREE-CREDIT GREP HITS ({len(hits)}) for {DATE} ===")
for t in sorted(hits, key=lambda x: -(x.get("viewCount") or 0)):
    who = t.get("userName") or t.get("handle") or "?"
    ca = (t.get("createdAt") or "")[:16]
    txt = (t.get("text") or "").replace("\n", " ")[:300]
    print(f"{who} | {t.get('likeCount')}L/{t.get('viewCount')}V | {ca}")
    print(f"    {txt}")
    print(f"    https://x.com/{who}/status/{t.get('id')}")
