#!/usr/bin/env python3
"""2026-10-24 check: Flova offer status in raw, new-term scan, per-handle chronology, handle diff vs 10-23."""
import json, re
from datetime import datetime, timezone

RAW = "/home/kiro/trend-engine/trends/x/raw/"
d = json.load(open(RAW + "scan_results_2026-10-24_FULL.json"))
prev = json.load(open(RAW + "scan_results_2026-10-23_FULL.json"))
raw, qual = d["raw"], d["qualifying"]

def pdate(s): return datetime.strptime(s, "%a %b %d %H:%M:%S %z %Y")
now = datetime(2026, 10, 24, 6, 0, tzinfo=timezone.utc)

print("=== HANDLE DIFF vs 10-23 ===")
cur, old = set(raw), set(prev["raw"])
print("count now=%d prev=%d | ADDED=%s REMOVED=%s" % (len(cur), len(old), sorted(cur-old) or "none", sorted(old-cur) or "none"))
qc = {q["handle"] for q in qual}; qo = {q["handle"] for q in prev["qualifying"]}
print("coverage now=%d prev=%d" % (len(qc), len(qo)))
print("newly qualifying (had 0 yesterday):", sorted(qc - qo))
print("dropped to 0 today:", sorted(qo - qc))

print("\n=== FLOVA OFFER GREP (raw, all fetched) ===")
for h in ("Flovaai",):
    for t in sorted(raw[h], key=lambda t: pdate(t["createdAt"])):
        txt = t.get("text","")
        if re.search(r"(80%|October 27|Oct 27|\.022|\.044|365|extended|price)", txt, re.I):
            print("%s | %dL/%dV | %s" % (pdate(t["createdAt"]).strftime("%m-%d %H:%M"),
                  t.get("likeCount",0), t.get("viewCount",0), txt[:400].replace("\n"," ")))
print("--- Flovaai full chronology ---")
for t in sorted(raw["Flovaai"], key=lambda t: pdate(t["createdAt"])):
    print("%s | %dL/%dV | %s" % (pdate(t["createdAt"]).strftime("%m-%d %H:%M"),
          t.get("likeCount",0), t.get("viewCount",0), t.get("text","")[:150].replace("\n"," ")))

print("\n=== NEW-TERM SCAN (terms absent from 10-23 dataset) ===")
prevtext = " ".join(q["text"] for q in prev["qualifying"]).lower()
for t in prev["raw"].values():
    prevtext += " " + " ".join(x.get("text","") for x in t).lower()
terms = ["ideogram vector","content factory","claude mythos","pippit","creative agent canvas",
         "starlight precise","minimax h3","a2o lite","four character","character swap","seedance 2.5 in 4k",
         "kling 4","topview t1","dreamina","flova","genjutsu","nano banana pro","claude duet","grok bot",
         "blender blockout","native 4k","open source","onyx","veo","sora","gpt sunburst","flux 3 max"]
for term in terms:
    hits = [q for q in qual if term in q["text"].lower()]
    new = len(hits) > 0 and term not in prevtext
    print("%-24s hits=%2d %s" % (term, len(hits), "<< NEW THIS RUN" if new else ""))
    for q in hits[:4]:
        print("      %-14s %dL/%dV %s" % (q["handle"], q["likes"], q["views"], q["text"][:110].replace("\n"," ")))

print("\n=== KEY HANDLE CHRONOLOGY (in-window) ===")
for h in ("higgsfield_ai","higgsfield","azed_ai","Kling_ai","runwayml","pika_labs","EHuanglu","venturetwins"):
    print("--- @%s ---" % h)
    for t in sorted(raw.get(h, []), key=lambda t: pdate(t["createdAt"]), reverse=True)[:12]:
        dt = pdate(t["createdAt"])
        print("  %s %dd | %5dL/%9dV | rt=%-5s | %s" % (dt.strftime("%m-%d %H:%M"), (now-dt).days,
              t.get("likeCount",0), t.get("viewCount",0), bool(t.get("retweeted_tweet")), t.get("text","")[:120].replace("\n"," ")))
