#!/usr/bin/env python3
"""Upgrade Genesis Command Centre voice assistant to claude-opus-4-6."""
import json, urllib.request, urllib.error, sys

TELNYX_API_KEY = "KEY019BE7A3A2D749FCA8681CFF8448A7F0_vTMM1n77CtQxLDT2ra3P1z"
ASSISTANT_ID = "assistant-cffc79bc-fd3b-4f96-a8e1-31a360100eb5"

def telnyx_request(method, path, data=None):
    url = f"https://api.telnyx.com/v2/{path}"
    body = json.dumps(data).encode() if data else None
    req = urllib.request.Request(url, data=body,
        headers={"Authorization": f"Bearer {TELNYX_API_KEY}", "Content-Type": "application/json"},
        method=method)
    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

# Read current
data, _ = telnyx_request("GET", f"ai/assistants/{ASSISTANT_ID}")
a = data.get("data", data)
print(f"Current: {a.get('model')} | llm_api_key_ref: {a.get('llm_api_key_ref')}")

# Patch with key ref included
patch = {
    "model": "anthropic/claude-opus-4-6",
    "name": "Claude Opus — Genesis Command Centre",
    "llm_api_key_ref": "anthropic_key",   # <-- required for Anthropic models
}

result, status = telnyx_request("PATCH", f"ai/assistants/{ASSISTANT_ID}", patch)
if status in (200, 201):
    u = result.get("data", result)
    print(f"SUCCESS (HTTP {status})")
    print(f"New model: {u.get('model')}")
    print(f"New name:  {u.get('name')}")
    print()
    print("Call +61 7 3130 4226 — now running claude-opus-4-6")
else:
    print(f"ERROR (HTTP {status})")
    print(json.dumps(result, indent=2))
    sys.exit(1)
