#!/usr/bin/env python3
"""Proper id-diff: which posts found by advanced_search are ABSENT from the user/tweets
fetched set, and are they replies (isReply) or genuine missed originals?"""
import os, time, json
import requests
from datetime import datetime, timezone

ENV = os.path.expanduser("~/.hermes/.env")
API_KEY = [l.strip().split("=",1)[1] for l in open(ENV) if l.startswith("GETXAPI_KEY=")][0]
H = {"Authorization": f"Bearer {API_KEY}"}
def pdt(s):
    try: return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")
    except Exception: return None

d = json.load(open("/home/kiro/trend-engine/trends/x/raw/scan_results_2026-11-09_FULL.json"))
raw = d["raw"]
CHECK = ["AI__TSUBAKI","c_valenzuelab","ItsMaryamAi","KammelaBala","WuxiaRocks","heyglif",
         "dai2noyuyuta","Ai_Vaidehi","AidenChaoyangHe","Teamily_AI","ElsaSofia__AI",
         "azed_ai","EHuanglu","CharaspowerAI","Kling_ai","higgsfield","runwayml"]

for h in CHECK:
    cached = {t.get("id"): t for t in (raw.get(h) or [])}
    try:
        r = requests.get("https://api.getxapi.com/twitter/tweet/advanced_search", headers=H,
                         params={"q": f"from:{h} since:2026-11-06", "count": 40}, timeout=60)
        tw = r.json().get("tweets", []) if r.status_code == 200 else []
    except Exception as e:
        print(h, "ERR", e); continue
    missing = [t for t in tw if t.get("id") not in cached]
    orig_missing = [t for t in missing if not t.get("isReply")]
    print(f"\n@{h}: search={len(tw)} cached={len(cached)} missing={len(missing)} "
          f"missing_ORIGINALS={len(orig_missing)}")
    for t in sorted(orig_missing, key=lambda x: pdt(x["createdAt"]))[:6]:
        print(f"   MISSED-ORIGINAL {t['createdAt'][:19]} {t.get('likeCount')}L/{t.get('viewCount')}V :: {(t.get('text') or '')[:130]}")
    time.sleep(1.0)
