#!/usr/bin/env python3
"""Sunra + trend history + core top-10 organic check 2026-10-29."""
import json, glob, re, os
from datetime import datetime, timezone, timedelta
RAW="/home/kiro/trend-engine/trends/x/raw"
D=json.load(open(f"{RAW}/scan_results_2026-10-29_FULL.json"))
raw=D["raw"]

print("=== SUNRA API GREP (all raw, any date) ===")
for h,tw in raw.items():
    for t in tw:
        txt=(t.get("text") or "")
        qt=t.get("quoted_tweet") or {}
        blob = txt + " " + (qt.get("text") or "")
        if re.search(r"sunra", blob, re.I):
            print(f"@{h} {t['createdAt']} {t.get('likeCount')}L/{t.get('viewCount')}V reply={bool(t.get('isReply'))}")
            print("  TEXT:", txt.replace("\n"," ")[:400])
            if qt: print("  QUOTED:", ((qt.get('author') or {}).get('userName')), (qt.get("text") or "").replace("\n"," ")[:800])

print("\n=== QUALIFYING TREND (all saved scans) ===")
for f in sorted(glob.glob(f"{RAW}/scan_results_*_FULL.json")):
    d=json.load(open(f))
    if "qualifying" not in d:
        continue
    print(f"  {d['date']}: qualifying {len(d['qualifying'])} handles {len(d['handles'])} failures {len(d.get('failures',[]))}")

print("\n=== CORE TOP 10 with like-rate / organic flag ===")
IRR={"AIBuzzNews","AIGuideHQ","AITECHio","AIFrontliner","TheAIColony","AITOPIAai","GoogleLabs","GoogleAI",
 "DonutAI","cladlabs","FaithJaiye2187","KammelaBala","MyStageAI","AlexStudio44","AI_with_jasmin","Ai_Vaidehi",
 "ChrisLaubAI","CINEMA505","OpenAI","AnthropicAI","AIScottAdams","AISuperDomain","AI_Jasonyu","AIByLeo"}
FILM={"DiscussingFilm","nofilmschool"}; LABS={"OpenAI","AnthropicAI","NVIDIAAI"}; OFF={"interesting_aIl","hubermanlab"}
def b(x):
    h=x["handle"]
    if h in LABS: return "labs"
    if h in OFF: return "off"
    if h in FILM: return "film"
    if h in IRR: return "irr"
    return "core"
core=sorted([x for x in D["qualifying"] if b(x)=="core"], key=lambda x:x["score"], reverse=True)
for i,x in enumerate(core[:14],1):
    lr = (x["likes"]/x["views"]*100) if x["views"] else 0
    flag = "PAID-AMPLIFIED" if lr < 0.1 else "organic"
    print(f"{i}|@{x['handle']}|{x['likes']}L|{x['views']}V|like_rate {lr:.3f}% {flag}|{x['date']}|score {x['score']:.0f}")

# organic core top 10 (like_rate >= 0.15%)
org=[x for x in core if x["views"] and (x["likes"]/x["views"]*100)>=0.15]
print("\n=== ORGANIC CORE TOP 10 (like_rate >= 0.15%) ===")
for i,x in enumerate(org[:10],1):
    lr=(x["likes"]/x["views"]*100)
    print(f"{i}|@{x['handle']}|{x['likes']}L|{x['rt']}RT|{x['views']}V|{lr:.2f}%|{x['date']}|{x['text'][:110]}|{x['url']}")
print("organic core count", len(org), "of", len(core))
