#!/usr/bin/env python3
"""X monitor verify 2026-10-31: new-since-yesterday diff + full text of key posts."""
import json, re
from datetime import datetime, timezone, timedelta

RAW = "/home/kiro/trend-engine/trends/x/raw"
D = json.load(open(f"{RAW}/scan_results_2026-10-31_FULL.json"))
Y = json.load(open(f"{RAW}/scan_results_2026-10-30_FULL.json"))
raw = D["raw"]
cutoff = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=3)

def pd(s):
    return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")

def ids(day):
    out = {}
    for h, tw in day["raw"].items():
        for t in tw:
            out[str(t.get("id"))] = (h, t)
    return out

today_ids, yest_ids = ids(D), ids(Y)
new = {k: v for k, v in today_ids.items() if k not in yest_ids}
print(f"NEW tweets not in yesterday's fetched set: {len(new)}")

# only in-window originals, sorted by engagement
def show(t, h):
    media = t.get("media") or []
    return (f"@{h} {pd(t['createdAt']).strftime('%m-%d %H:%M')} {t.get('likeCount')}L/{t.get('retweetCount')}RT/"
            f"{t.get('replyCount')}R/{t.get('viewCount')}V media={[m.get('type') for m in media]} "
            f"reply={bool(t.get('isReply'))} quote={bool(t.get('quoted_tweet'))}")

newin = [(h, t) for h, t in new.values()
         if not t.get("retweeted_tweet") and pd(t["createdAt"]) >= cutoff
         and (t.get("likeCount") or 0) >= 30 and not (t.get("text") or "").startswith("RT @")]
newin.sort(key=lambda r: -(r[1].get("likeCount") or 0))
print(f"NEW in-window originals with >=30 likes: {len(newin)}")
for h, t in newin[:25]:
    print(f"  {show(t,h)}")
    print(f"     {(t.get('text') or '')[:260]}")
    print(f"     https://x.com/{h}/status/{t.get('id')}")

print("\n=== KEY POST FULL TEXT ===")
KEYS = [("runwayml", "Introducing new capabilities in Runway Agent"),
        ("Kling_ai", "Kling AI Originals"),
        ("azed_ai", "Pixar-Style 3D Character"),
        ("venturetwins", "completely impractical"),
        ("EHuanglu", "people still think AI is not ready"),
        ("runwayml", "Turn your short films into full-on video games"),
        ("pika_labs", "A little deep dive")]
for h, needle in KEYS:
    for t in raw.get(h, []):
        if needle.lower() in (t.get("text") or "").lower():
            print(f"\n--- {show(t,h)}")
            print(t.get("text"))
            print(f"https://x.com/{h}/status/{t.get('id')}")
            break

print("\n=== EHuanglu / azed_ai / ItsMaryamAi IN-WINDOW ===")
for h in ["EHuanglu", "azed_ai", "ItsMaryamAi", "Soaima_Ai", "ElsaSofia__AI", "venturetwins", "c_valenzuelab"]:
    rows = [t for t in raw.get(h, [])
            if not t.get("retweeted_tweet") and pd(t["createdAt"]) >= cutoff]
    rows.sort(key=lambda t: pd(t["createdAt"]))
    print(f"\n-- @{h} ({len(rows)}) --")
    for t in rows:
        print(f"  {pd(t['createdAt']).strftime('%m-%d %H:%M')} {t.get('likeCount')}L/{t.get('viewCount')}V "
              f"reply={bool(t.get('isReply'))} | {(t.get('text') or '')[:170]}")

print("\n=== 30-RUN QUALIFYING TREND ===")
import glob, os
rows = []
for p in sorted(glob.glob(f"{RAW}/scan_results_*_FULL.json")):
    try:
        dd = json.load(open(p))
        rows.append((dd.get("date", os.path.basename(p)), len(dd["qualifying"]), len(dd["handles"])))
    except Exception:
        pass
for d, n, hn in rows[-20:]:
    print(f"  {d}: {n} qualifying / {hn} handles")
