from http import HTTPStatus
from typing import Any
from urllib.parse import quote

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.get_desktop_session_code_response_400 import GetDesktopSessionCodeResponse400
from ...models.get_desktop_session_code_response_404 import GetDesktopSessionCodeResponse404
from ...models.get_desktop_session_code_response_410 import GetDesktopSessionCodeResponse410
from ...models.get_desktop_session_code_response_500 import GetDesktopSessionCodeResponse500
from ...models.types_session import TypesSession
from ...types import Response


def _get_kwargs(
    code: str,
) -> dict[str, Any]:

    _kwargs: dict[str, Any] = {
        "method": "get",
        "url": "/desktop/session/{code}".format(
            code=quote(str(code), safe=""),
        ),
    }

    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> (
    GetDesktopSessionCodeResponse400
    | GetDesktopSessionCodeResponse404
    | GetDesktopSessionCodeResponse410
    | GetDesktopSessionCodeResponse500
    | TypesSession
    | None
):
    if response.status_code == 200:
        response_200 = TypesSession.from_dict(response.json())

        return response_200

    if response.status_code == 400:
        response_400 = GetDesktopSessionCodeResponse400.from_dict(response.json())

        return response_400

    if response.status_code == 404:
        response_404 = GetDesktopSessionCodeResponse404.from_dict(response.json())

        return response_404

    if response.status_code == 410:
        response_410 = GetDesktopSessionCodeResponse410.from_dict(response.json())

        return response_410

    if response.status_code == 500:
        response_500 = GetDesktopSessionCodeResponse500.from_dict(response.json())

        return response_500

    if client.raise_on_unexpected_status:
        raise errors.UnexpectedStatus(response.status_code, response.content)
    else:
        return None


def _build_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> Response[
    GetDesktopSessionCodeResponse400
    | GetDesktopSessionCodeResponse404
    | GetDesktopSessionCodeResponse410
    | GetDesktopSessionCodeResponse500
    | TypesSession
]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    code: str,
    *,
    client: AuthenticatedClient | Client,
) -> Response[
    GetDesktopSessionCodeResponse400
    | GetDesktopSessionCodeResponse404
    | GetDesktopSessionCodeResponse410
    | GetDesktopSessionCodeResponse500
    | TypesSession
]:
    """Get desktop session

     Retrieves an active desktop login session by code. Marks the session as exchanged. Returns session
    data including user and team information. The desktop app should poll this endpoint after initiating
    login.

    Args:
        code (str):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Response[GetDesktopSessionCodeResponse400 | GetDesktopSessionCodeResponse404 | GetDesktopSessionCodeResponse410 | GetDesktopSessionCodeResponse500 | TypesSession]
    """

    kwargs = _get_kwargs(
        code=code,
    )

    response = client.get_httpx_client().request(
        **kwargs,
    )

    return _build_response(client=client, response=response)


def sync(
    code: str,
    *,
    client: AuthenticatedClient | Client,
) -> (
    GetDesktopSessionCodeResponse400
    | GetDesktopSessionCodeResponse404
    | GetDesktopSessionCodeResponse410
    | GetDesktopSessionCodeResponse500
    | TypesSession
    | None
):
    """Get desktop session

     Retrieves an active desktop login session by code. Marks the session as exchanged. Returns session
    data including user and team information. The desktop app should poll this endpoint after initiating
    login.

    Args:
        code (str):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        GetDesktopSessionCodeResponse400 | GetDesktopSessionCodeResponse404 | GetDesktopSessionCodeResponse410 | GetDesktopSessionCodeResponse500 | TypesSession
    """

    return sync_detailed(
        code=code,
        client=client,
    ).parsed


async def asyncio_detailed(
    code: str,
    *,
    client: AuthenticatedClient | Client,
) -> Response[
    GetDesktopSessionCodeResponse400
    | GetDesktopSessionCodeResponse404
    | GetDesktopSessionCodeResponse410
    | GetDesktopSessionCodeResponse500
    | TypesSession
]:
    """Get desktop session

     Retrieves an active desktop login session by code. Marks the session as exchanged. Returns session
    data including user and team information. The desktop app should poll this endpoint after initiating
    login.

    Args:
        code (str):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Response[GetDesktopSessionCodeResponse400 | GetDesktopSessionCodeResponse404 | GetDesktopSessionCodeResponse410 | GetDesktopSessionCodeResponse500 | TypesSession]
    """

    kwargs = _get_kwargs(
        code=code,
    )

    response = await client.get_async_httpx_client().request(**kwargs)

    return _build_response(client=client, response=response)


async def asyncio(
    code: str,
    *,
    client: AuthenticatedClient | Client,
) -> (
    GetDesktopSessionCodeResponse400
    | GetDesktopSessionCodeResponse404
    | GetDesktopSessionCodeResponse410
    | GetDesktopSessionCodeResponse500
    | TypesSession
    | None
):
    """Get desktop session

     Retrieves an active desktop login session by code. Marks the session as exchanged. Returns session
    data including user and team information. The desktop app should poll this endpoint after initiating
    login.

    Args:
        code (str):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        GetDesktopSessionCodeResponse400 | GetDesktopSessionCodeResponse404 | GetDesktopSessionCodeResponse410 | GetDesktopSessionCodeResponse500 | TypesSession
    """

    return (
        await asyncio_detailed(
            code=code,
            client=client,
        )
    ).parsed
