#!/usr/bin/env python3
"""
Check Telnyx AI model pricing and capabilities.
Pulls from Telnyx models API + known pricing data.
"""
import json
import urllib.request
import urllib.error

TELNYX_API_KEY = "KEY019BE7A3A2D749FCA8681CFF8448A7F0_vTMM1n77CtQxLDT2ra3P1z"

def telnyx_get(path):
    req = urllib.request.Request(
        f"https://api.telnyx.com/v2/{path}",
        headers={"Authorization": f"Bearer {TELNYX_API_KEY}", "Content-Type": "application/json"}
    )
    try:
        with urllib.request.urlopen(req) as r:
            return json.loads(r.read()), r.status
    except urllib.error.HTTPError as e:
        return json.loads(e.read() or b"{}"), e.code

# Get full model details
data, status = telnyx_get("ai/models?page[size]=100")
models = data.get("data", [])

print(f"=== Telnyx AI Models — Full Detail ({len(models)} models) ===\n")
for m in models:
    name = m.get("name") or m.get("id", "?")
    # Look for pricing fields
    pricing = m.get("pricing") or m.get("cost") or {}
    input_cost = m.get("input_cost") or m.get("price_per_input_token") or pricing.get("input")
    output_cost = m.get("output_cost") or m.get("price_per_output_token") or pricing.get("output")
    context = m.get("context_length") or m.get("max_tokens") or m.get("context_window")
    speed = m.get("speed") or m.get("tokens_per_second")
    provider = name.split("/")[0] if "/" in name else "unknown"
    
    print(f"Model: {name}")
    if input_cost: print(f"  Input:   ${input_cost}")
    if output_cost: print(f"  Output:  ${output_cost}")
    if context: print(f"  Context: {context}")
    if speed: print(f"  Speed:   {speed} tok/s")
    # Print ALL fields to see what's available
    other = {k: v for k, v in m.items() if k not in ["name","id","input_cost","output_cost","context_length","max_tokens","speed","pricing","cost","context_window","price_per_input_token","price_per_output_token"]}
    if other:
        print(f"  Other:   {json.dumps(other)[:200]}")
    print()
