import requests
import json

TELNYX_API_KEY = "KEY019BE7A3A2D749FCA8681CFF8448A7F0_vTMM1n77CtQxLDT2ra3P1z"
AIVA_ID = "assistant-696799a5-e994-4ac1-8f26-7b0923aee682"

def main():
    print(f"Fixing AIVA (ID: {AIVA_ID})...")
    
    # We remove the broken external_llm and point to stable Gemini 2.5 Flash
    # We also add the tools mentioned in her instructions
    payload = {
        "model": "google/gemini-2.5-flash",
        "llm_api_key_ref": "google_gemini_key",
        "external_llm": None, # Remove the broken external link
        "tools": [
            {
                "type": "webhook",
                "webhook": {
                    "name": "relay_directive_to_claude",
                    "description": "Relays a voice command from Kinan to the Claude AI terminal.",
                    "url": "https://api.sunaivadigital.com/widget-api/webhooks/aiva/directive",
                    "method": "POST",
                    "async": True,
                    "body_parameters": {
                        "type": "object",
                        "properties": {
                            "directive": {"type": "string", "description": "The command to relay"},
                            "priority": {"type": "string", "enum": ["low", "normal", "high", "urgent"]}
                        },
                        "required": ["directive"]
                    }
                }
            },
            {
                "type": "webhook",
                "webhook": {
                    "name": "check_claude_status",
                    "description": "Checks the current status and progress of the Claude AI terminal.",
                    "url": "https://api.sunaivadigital.com/widget-api/webhooks/aiva/status",
                    "method": "GET",
                    "async": False
                }
            },
            {
                "type": "webhook",
                "webhook": {
                    "name": "log_conversation_summary",
                    "description": "Logs a summary of the conversation for memory and learning.",
                    "url": "https://api.sunaivadigital.com/widget-api/webhooks/aiva/log",
                    "method": "POST",
                    "async": True,
                    "body_parameters": {
                        "type": "object",
                        "properties": {
                            "summary": {"type": "string"},
                            "outcome": {"type": "string"},
                            "caller_mood": {"type": "string"}
                        }
                    }
                }
            }
        ]
    }

    try:
        resp = requests.patch(
            f"https://api.telnyx.com/v2/ai/assistants/{AIVA_ID}",
            headers={
                "Authorization": f"Bearer {TELNYX_API_KEY}",
                "Content-Type": "application/json",
            },
            json=payload,
            timeout=30.0
        )
        print("Update Status:", resp.status_code)
        if resp.status_code != 200:
            print("Error Response:", resp.text)
        else:
            print("Successfully restored AIVA connection and tools.")
            
    except Exception as e:
        print("Error during request:", str(e))

if __name__ == "__main__":
    main()
