#!/usr/bin/env python3
"""Final verification 2026-11-10: organic vs paid split, aged-out check, new arrivals,
watchlist count, Elsa/Charaspower detail, Fable 5.2 sweep."""
import os, re, json
from datetime import datetime, timezone, timedelta

RAW = "/home/kiro/trend-engine/trends/x/raw"
cur = json.load(open(f"{RAW}/scan_results_2026-11-10_FULL.json"))
prev = json.load(open(f"{RAW}/scan_results_2026-11-09_FULL.json"))

CORE = set("""runwayml CuriousRefuge nofilmschool venturetwins Kling_ai paultrillo c_valenzuelab
karenxcheng higgsfield pika_labs EHuanglu azed_ai JeffSynthesized ai_filmmaker Magiermogul
WuxiaRocks JiamingSuen higgsfield_ai Chandan_Perla CharaspowerAI AIWarper ItsMaryamAi
ElsaSofia__AI AI__TSUBAKI Flovaai openart_ai perplexity_ai heyglif""".split())

def lr(t): return (t["likes"]/t["views"]*100) if t["views"] else 0
core = sorted([t for t in cur["qualifying"] if t["handle"] in CORE], key=lambda x: -x["score"])

print("== ORGANIC vs PAID SPLIT (core) ==")
for t in core:
    flag = "PAID?" if lr(t) < 0.30 and t["views"] > 20000 else "org "
    print(f"  {flag} @{t['handle']:16s} {t['likes']:>6}L/{t['views']:>9}V = {lr(t):6.3f}%  {t['date']}  {t['text'][:70]}")

print("\n== ORGANIC-ONLY CORE TOP 10 ==")
org = [t for t in core if lr(t) >= 0.30 or t["views"] <= 20000]
for i, t in enumerate(org[:10], 1):
    print(f"{i}. @{t['handle']} | {t['likes']}L {t['rt']}RT {t['replies']}R | {t['views']}V | {lr(t):.3f}% | {t['date']}")
    print(f"   {t['url']}")
    print(f"   {t['text'][:170]}")
print(f"  organic count {len(org)} of {len(core)}")

print("\n== MIRACLE POST STATUS (11-06, aged out?) ==")
for d in ("2026-11-09","2026-11-10"):
    j = json.load(open(f"{RAW}/scan_results_{d}_FULL.json"))
    found = None
    for h, tw in j["raw"].items():
        for t in tw:
            if str(t.get("id")) == "2119460000000000000" or (t.get("likeCount") or 0) > 15000 and "Miracle" in (t.get("text") or ""):
                found = (h, t)
                break
        if found: break
    print(f"  {d}: {found[0] if found else 'NOT FETCHED'} {(found[1].get('likeCount'), found[1].get('viewCount')) if found else ''}")

print("\n== FABLE 5.2 SWEEP (all in-window originals) ==")
cutoff = datetime(2026,11,7,tzinfo=timezone.utc)
def pdate(s):
    try: return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")
    except Exception: return None
n = 0
for h, tw in cur["raw"].items():
    for t in tw:
        if t.get("retweeted_tweet") or t.get("type")=="retweet": continue
        d = pdate(t.get("createdAt",""))
        if not d or d < cutoff: continue
        if re.search(r"fable|mythos", t.get("text") or "", re.I):
            n += 1
            print(f"  @{h:16s} {t.get('likeCount')}L/{t.get('viewCount')}V {d.strftime('%m-%d %H:%M')} "
                  f"{(t.get('text') or '')[:160].replace(chr(10),' ')}")
print(f"  TOTAL fable/mythos mentions: {n}")

print("\n== NEW ARRIVALS vs prev (handles that qualified today, not yesterday) ==")
ph = {t["handle"] for t in prev["qualifying"]}
for t in sorted(cur["qualifying"], key=lambda x: -x["score"]):
    if t["handle"] not in ph:
        print(f"  NEW @{t['handle']} {t['likes']}L/{t['views']}V {t['date']}: {t['text'][:110]}")

print("\n== WATCHLIST ==")
txt = open("/home/kiro/trend-engine/trends/x/raw/rising-accounts.md").read()
hs = set(x.lower() for x in re.findall(r"@(\w+)", txt))
print(f"  md unique handles {len(hs)} | scanned {len(cur['handles'])}")

print("\n== ElsaSofia__AI / CharaspowerAI / AIWarper detail ==")
for h in ("ElsaSofia__AI","CharaspowerAI","AIWarper","azed_ai","AI__TSUBAKI"):
    tw = cur["raw"].get(h) or []
    print(f"\n @{h} (n={len(tw)})")
    origs = [t for t in tw if not t.get("retweeted_tweet") and t.get("type")!="retweet" and not t.get("isReply")]
    inw = [t for t in origs if (pdate(t.get("createdAt","")) or cutoff) >= cutoff]
    print(f"   originals {len(origs)} of {len(tw)}, in-window {len(inw)}")
    for t in sorted(inw, key=lambda x: x.get("createdAt","")):
        print(f"   {pdate(t.get('createdAt','')).strftime('%m-%d %H:%M')} {t.get('likeCount')}L/{t.get('viewCount')}V "
              f"{(t.get('text') or '')[:120].replace(chr(10),' ')}")
