#!/usr/bin/env python3
"""2026-10-16 supplementary: t.co resolution, offer full text, in-window audits."""
import json, re, subprocess
DATE = "2026-10-16"
d = json.load(open(f"/home/kiro/trend-engine/trends/x/raw/scan_results_{DATE}_FULL.json"))
q, raw = d["qualifying"], d["raw"]
by = {x["url"]: x for x in q}

def resolve(u):
    try:
        out = subprocess.run(["curl", "-sIL", "-o", "/dev/null", "-w", "%{url_effective}",
                              "--max-time", "20", u], capture_output=True, text=True)
        return out.stdout.strip()
    except Exception as e:
        return f"ERR {e}"

print("### T.CO RESOLUTION -- top-10 posts with no context")
for x in q[:60]:
    if re.fullmatch(r"https://t\.co/\w+", x["text"].strip()):
        print(f"@{x['handle']} | {x['likes']}L/{x['views']}V | {x['date']} | {x['text']}")
        print(f"   -> {resolve(x['text'].strip())}")

print("\n### OFFER FULL TEXT")
pats = {
    "FLOVA 80%": r"80%|365",
    "MAGNIFIC": r"magnific",
    "WAN 3.0 / API": r"wan ?3\.0|60% off|50% off",
    "CAPCUT BF": r"black friday",
    "KLING 4.0": r"kling ?4\.0",
    "FLUX 3": r"flux ?3",
}
for name, p in pats.items():
    rx = re.compile(p, re.I)
    hits = [x for x in q if rx.search(x["text"])]
    print(f"\n-- {name} ({len(hits)}) ")
    for x in hits[:6]:
        print(f"@{x['handle']} {x['likes']}L/{x['rt']}RT/{x['views']}V {x['date']} score={x['score']:.0f}")
        print(f"   {x['text'][:400]}")
        print(f"   {x['url']}")

print("\n### TIER-2 / WATCH in-window ORIGINALS with 0 qualifying")
for h in ["JeffSynthesized", "WuxiaRocks", "CuriousRefuge", "Chandan_Perla", "paultrillo"]:
    tw = raw.get(h, [])
    origs = [t for t in tw if not t.get("retweeted_tweet") and t.get("type") != "retweet"
             and not (t.get("text") or "").startswith("RT @")]
    print(f"\n@{h}: {len(origs)} originals fetched")
    for t in origs[:4]:
        print(f"   {t.get('createdAt','')[:16]} | {t.get('likeCount')}L/"
              f"{t.get('viewCount')}V | {(t.get('text') or '')[:150]}")

print("\n### AI-LABS (OpenAI / AnthropicAI) top posts")
for x in sorted([y for y in q if y["handle"] in ("OpenAI", "AnthropicAI")],
                key=lambda z: -z["score"]):
    print(f"@{x['handle']} {x['likes']}L/{x['views']}V {x['date']} :: {x['text'][:150]}")
