#!/usr/bin/env python3
"""Test all saved SQL explorer queries against the live API."""
import json
import subprocess

API = "https://stonks-api.celestium.life"

result = subprocess.run(
    ["curl", "-sk", f"{API}/api/analytics/saved-queries"],
    capture_output=True, text=True, timeout=15,
)
queries = json.loads(result.stdout)
print(f"Found {len(queries)} saved queries\n")

passed, failed, errors = 0, 0, []

for i, q in enumerate(queries, 1):
    name = q["name"]
    sql = q["sql_text"]
    payload = json.dumps({"sql": sql, "limit": 5})
    r = subprocess.run(
        ["curl", "-sk", "-X", "POST", f"{API}/api/analytics/query",
         "-H", "Content-Type: application/json", "-d", payload],
        capture_output=True, text=True, timeout=30,
    )
    try:
        resp = json.loads(r.stdout)
        if "error" in resp or "detail" in resp:
            err = str(resp.get("detail", resp.get("error", "?")))[:120]
            print(f"  [{i:2d}/24] FAIL  {name}")
            print(f"          {err}")
            failed += 1
            errors.append((name, err))
        else:
            rows = len(resp.get("rows", []))
            cols = len(resp.get("columns", []))
            ms = resp.get("elapsed_ms", "?")
            print(f"  [{i:2d}/24] OK    {name}  ({rows} rows, {cols} cols, {ms}ms)")
            passed += 1
    except json.JSONDecodeError:
        print(f"  [{i:2d}/24] FAIL  {name}  (not JSON)")
        failed += 1
        errors.append((name, r.stdout[:100]))

print(f"\n{'='*60}")
print(f"Results: {passed} passed, {failed} failed out of {len(queries)}")
if errors:
    print("\nFailed:")
    for n, e in errors:
        print(f"  - {n}: {e}")
