import requests
import json
import os

TELNYX_API_KEY = "KEY019BE7A3A2D749FCA8681CFF8448A7F0_vTMM1n77CtQxLDT2ra3P1z"
ASSISTANT_ID = "assistant-9c42d3ce-e05a-4e34-8083-c91081917637"

def main():
    print(f"Updating Telnyx Assistant {ASSISTANT_ID}...")
    
    payload = {
        "tools": [
            {
                "type": "webhook",
                "name": "Lead Capture",
                "url": "https://services.leadconnectorhq.com/hooks/73q7bKDm2d6hsCtHuz1m/webhook-trigger/lead-capture"
            }
        ]
    }

    try:
        resp = requests.patch(
            f"https://api.telnyx.com/v2/ai/assistants/{ASSISTANT_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 updated assistant.")
            # Write UUID to file as confirmed
            with open("/mnt/e/genesis-system/config/telnyx_agent_uuid.txt", "w") as f:
                f.write(ASSISTANT_ID)
            print(f"UUID {ASSISTANT_ID} written to config.")
            
    except Exception as e:
        print("Error during request:", str(e))

if __name__ == "__main__":
    main()
