# sdkStart: Start a session against an agent and receive its session token.
# Credential: API key (WETEL_API_KEY). Python 3.8+ with "requests".
import json
import os

import requests

API_URL = os.environ.get("WETEL_API_URL", "https://api.wetel.dev/graphql")

QUERY = """
mutation StartSession($input: SdkStartInput!) {
  sdkStart(input: $input) {
    sessionId
    avatarToken
    agentId
    graphqlEndpoint
  }
}
"""

VARIABLES = {
    "input": {
        "agentId": 7,
        "clientId": "ticket-1001",
        "context": {
            "ticketId": "T-1001",
            "product": "laptop",
        },
    },
}

response = requests.post(
    API_URL,
    json={"query": QUERY, "variables": VARIABLES},
    headers={
        "x-huat-platform": "customer",
        "X-Api-Key": os.environ["WETEL_API_KEY"],
    },
    timeout=30,
)
body = response.json()
if body.get("errors"):
    error = body["errors"][0]
    raise SystemExit(f"{error.get('extensions', {}).get('code')}: {error['message']}")
print(json.dumps(body["data"]["sdkStart"], indent=2))
