#!/usr/bin/env python3
"""Full text of key posts + 24h deltas on shared ids + quoted-tweet expansion."""
import json
from datetime import datetime

RAW = "/home/kiro/trend-engine/trends/x/raw/"
d = json.load(open(RAW + "scan_results_2026-11-09_FULL.json"))
p = json.load(open(RAW + "scan_results_2026-11-08_FULL.json"))
def pdt(s): return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")

# ---- 24h delta on ids present in both snapshots
prev = {}
for h, tw in p["raw"].items():
    for t in tw: prev[t["id"]] = (h, t)
print("=== 24H DELTAS (top movers by new views) ===")
rows = []
for h, tw in d["raw"].items():
    for t in tw:
        if t["id"] not in prev: continue
        ph, pt = prev[t["id"]]
        dv = (t.get("viewCount") or 0) - (pt.get("viewCount") or 0)
        dl = (t.get("likeCount") or 0) - (pt.get("likeCount") or 0)
        if abs(dv) < 500 and abs(dl) < 20: continue
        rows.append((dv, dl, h, t))
for dv, dl, h, t in sorted(rows, reverse=True)[:14]:
    v = t.get("viewCount") or 1
    print(f"@{h:16s} +{dv:>9}V +{dl:>6}L  now {t.get('likeCount')}L/{v}V rate={t.get('likeCount',0)/max(v,1)*100:.3f}% :: {(t.get('text') or '')[:90]}")
neg = [r for r in rows if r[0] < 0]
print(f"\nNEGATIVE view deltas (impression corrections): {len(neg)}")
for dv, dl, h, t in sorted(neg)[:5]:
    print(f"  @{h} {dv}V {dl}L :: {(t.get('text') or '')[:70]}")

# ---- full text of key posts
KEYS = {"Ai_Vaidehi": ["Same ad concept"], "EHuanglu": ["claude let you EDIT", "ep 02"],
        "Kling_ai": ["energy drink", "Redline"], "azed_ai": ["Celebrity Caricature"],
        "ElsaSofia__AI": ["next generation"], "CharaspowerAI": ["TERRIBLE", "Minimax Design"],
        "higgsfield": ["Miracle"], "venturetwins": ["wife guy", "Threads"]}
print("\n\n=== KEY POST FULL TEXT ===")
for h, needles in KEYS.items():
    for t in d["raw"].get(h, []):
        tx = (t.get("text") or "")
        if not any(n.lower() in tx.lower() for n in needles): continue
        med = t.get("media") or []
        mt = med[0].get("type") if med and isinstance(med[0], dict) else None
        print(f"\n--- @{h} {t['createdAt'][:19]} {t.get('likeCount')}L/{t.get('retweetCount')}RT/"
              f"{t.get('replyCount')}R/{t.get('viewCount')}V media={mt} isReply={t.get('isReply')}")
        print(f"    url https://x.com/{h}/status/{t['id']}")
        print("   ", tx[:1400].replace("\n", "\n    "))
        qt = t.get("quoted_tweet")
        if qt and isinstance(qt, dict):
            print(f"    QUOTED @{(qt.get('author') or {}).get('userName')}: {(qt.get('text') or '')[:500]}")
        rt = t.get("retweeted_tweet")
        if rt and isinstance(rt, dict):
            print(f"    RT-OF @{(rt.get('author') or {}).get('userName')}: {(rt.get('text') or '')[:400]}")
