#!/usr/bin/env python3
"""Final pass for x-account-monitor: bucket breakdown, core top 10, arb grep.

The `qualifying` records use these keys:
  handle, score, date, likes, rt, replies, views, text, url
(NOT userName/likeCount — that is the `raw` store's shape.)
"""
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"]

IRRELEVANT = set("""AIBuzzNews AIGuideHQ AITECHio AIFrontliner TheAIColony AITOPIAai AISuperDomain
GoogleLabs GoogleAI DonutAI cladlabs FaithJaiye2187 KammelaBala MyStageAI AlexStudio44
AI_with_jasmin Ai_Vaidehi ChrisLaubAI OpenAI AnthropicAI CINEMA505 AIScottAdams AI_Jasonyu AIByLeo""".split())
FILM_INDUSTRY = {"DiscussingFilm", "nofilmschool"}
LABS = {"OpenAI", "AnthropicAI"}

def tier(h):
    if h in LABS: return "labs"
    if h in FILM_INDUSTRY: return "film"
    if h in IRRELEVANT: return "filtered"
    return "core"

buckets = {"core": [], "film": [], "filtered": [], "labs": []}
for t in q:
    buckets[tier(t["handle"])].append(t)

for k, v in buckets.items():
    v.sort(key=lambda x: -float(x["score"]))
print(f"DATE {DATE} total_qualifying={len(q)} failures={len(d['failures'])} handles={len(d['handles'])}")
print("buckets:", {k: len(v) for k, v in buckets.items()})
core = buckets["core"]
print(f"core top-10 cutoff={float(core[9]['score']):.0f}  rank11={float(core[10]['score']):.0f}")
print(f"coverage (handles with >=1 qualifier): {len({t['handle'] for t in q})}")

print("\n=== CORE TOP 10 ===")
for i, t in enumerate(core[:10], 1):
    print(f"{i}|@{t['handle']}|{t['likes']}L|{t['rt']}RT|{t['replies']}R|{t['views']}V|{t['date']}|{float(t['score']):.0f}")
    print(f"   {(t['text'] or '')[:260]}")
    print(f"   {t['url']}")

print("\n=== FILM-INDUSTRY (all) ===")
for t in buckets["film"]:
    print(f"@{t['handle']}|{t['likes']}L|{t['views']}V|{t['date']}|{(t['text'] or '')[:140]}")

print("\n=== ARB / FREE-CREDIT GREP ===")
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|% off)", re.I)
hits = [t for t in q if pat.search(t["text"] or "")]
for t in sorted(hits, key=lambda x: -float(x["views"] or 0)):
    print(f"@{t['handle']}|{t['likes']}L/{t['views']}V|{t['date']}")
    print(f"    {(t['text'] or '').replace(chr(10),' ')[:320]}")
    print(f"    {t['url']}")
