#!/usr/bin/env python3
"""Get all Telnyx AI assistants and their phone numbers."""
import json
import urllib.request

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"}
    )
    with urllib.request.urlopen(req) as resp:
        return json.loads(resp.read())

# Get all assistants
print("=== All AI Assistants ===")
data = telnyx_get("ai/assistants")
assistants = data.get("data", [])
print(f"Total: {len(assistants)}")
for a in assistants:
    print(f"\nID: {a['id']}")
    print(f"Name: {a.get('name', '?')}")
    print(f"Model: {a.get('model', '?')}")
    instructions = a.get('instructions', '')
    if instructions:
        print(f"Instructions (first 100): {instructions[:100]}")
    # Check phone numbers
    phone_numbers = a.get('phone_numbers', [])
    if phone_numbers:
        print(f"Phone numbers: {phone_numbers}")

# Also get phone numbers list
print("\n\n=== Phone Numbers ===")
try:
    pn_data = telnyx_get("phone_numbers?page[size]=20")
    for pn in pn_data.get("data", []):
        print(f"  {pn.get('phone_number')} | ID: {pn.get('id')} | Status: {pn.get('status')}")
except Exception as e:
    print(f"Error fetching phone numbers: {e}")
