# Check that an agent can place calls. Exits 1 when it is not ready. Python 3.9+ with requests.
import os
import sys

import requests

base = os.environ.get("RELATIONSAI_API_BASE", "https://voice-api.relationsai.com/v1")
# Defaults to the test-mode Sample agent; set AGENT_ID to use your own agent.
agent_id = os.environ.get("AGENT_ID", "agent_1ZltiWzxrXmMprGD6q63")

r = requests.get(
    f"{base}/agents/{agent_id}/readiness",
    headers={"Authorization": f"Bearer {os.environ['RELATIONSAI_API_KEY']}"},
    timeout=30,
)
body = r.json()
if not r.ok:
    err = body["error"]
    sys.exit(f"{err['code']}: {err['message']} (request {err['request_id']})")
if body["ready"]:
    print(f"{agent_id} is ready to place calls")
else:
    print(f"{agent_id} is not ready:")
    for reason in body["reasons"]:
        print(f"  {reason['code']}: {reason['message']}")
    sys.exit(1)
