#!/usr/bin/env python3
"""2026-10-19 part 3: freshest posts (10-18/10-19) from CORE handles, core ranks 11-30,
and any new offer language. raw -> author.userName."""
import json, re
from datetime import datetime, timezone

DATE = "2026-10-19"
d = json.load(open(f"/home/kiro/trend-engine/trends/x/raw/scan_results_{DATE}_FULL.json"))
raw, q = d["raw"], d["qualifying"]
FMT = "%a %b %d %H:%M:%S %z %Y"
def dt(s):
    try: return datetime.strptime(s, FMT)
    except Exception: return None
IRR = {"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"}
FILM = {"DiscussingFilm","nofilmschool"}
core_q = sorted([t for t in q if t["handle"] not in IRR and t["handle"] not in FILM], key=lambda t: -t["score"])
print(f"core qualifying={len(core_q)}  core rank10 score={core_q[9]['score']:.0f}  rank11={core_q[10]['handle']} {core_q[10]['score']:.0f}  rank12={core_q[11]['score']:.0f}")
print("\n===== CORE RANKS 11-30 =====")
for i, t in enumerate(core_q[10:30], 11):
    print(f"{i:2d}|@{t['handle']:15s} {t['likes']:5d}L {t['rt']:4d}RT {t['views']:8d}V score={t['score']:7.0f} {t['date']} :: {' '.join(str(t['text']).split())[:110]}")

print("\n===== FRESHEST CORE POSTS (>= 2026-10-18 00:00), any engagement =====")
cut = datetime(2026,10,18,tzinfo=timezone.utc)
rows = []
for h, tw in raw.items():
    if h in IRR or h in FILM: continue
    for t in tw:
        c = dt(t.get("createdAt",""))
        if c and c >= cut and t.get("retweeted_tweet") is None:
            rows.append((c, h, t))
rows.sort(key=lambda r: r[0], reverse=True)
for c, h, t in rows[:30]:
    print(f"{c.strftime('%m-%d %H:%M')} @{h:15s} {t.get('likeCount',0):5d}L/{t.get('viewCount',0):8d}V :: {' '.join(str(t.get('text'))[:150].split())}")

print("\n===== OFFER-WORD SCAN over freshest (10-18+) all tiers =====")
OFF = re.compile(r"(off\b|% off|discount|free|credits|unlimited|promo|trial|launch|now available|waitlist|beta|sign ?up|deadline|prize|challenge)", re.I)
for c, h, t in rows[:40]:
    m = OFF.findall(str(t.get("text","")))
    if m:
        print(f"{c.strftime('%m-%d %H:%M')} @{h:15s} {t.get('viewCount',0):8d}V {set(x.lower() for x in m)} :: {' '.join(str(t.get('text'))[:150].split())}")
