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.delete_team_keys_user_name_response_200 import DeleteTeamKeysUserNameResponse200
from ...models.delete_team_keys_user_name_response_400 import DeleteTeamKeysUserNameResponse400
from ...models.delete_team_keys_user_name_response_401 import DeleteTeamKeysUserNameResponse401
from ...models.delete_team_keys_user_name_response_500 import DeleteTeamKeysUserNameResponse500
from ...types import Response


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

    _kwargs: dict[str, Any] = {
        "method": "delete",
        "url": "/team/keys/{user}/{name}".format(
            user=quote(str(user), safe=""),
            name=quote(str(name), safe=""),
        ),
    }

    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> (
    DeleteTeamKeysUserNameResponse200
    | DeleteTeamKeysUserNameResponse400
    | DeleteTeamKeysUserNameResponse401
    | DeleteTeamKeysUserNameResponse500
    | None
):
    if response.status_code == 200:
        response_200 = DeleteTeamKeysUserNameResponse200.from_dict(response.json())

        return response_200

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

        return response_400

    if response.status_code == 401:
        response_401 = DeleteTeamKeysUserNameResponse401.from_dict(response.json())

        return response_401

    if response.status_code == 500:
        response_500 = DeleteTeamKeysUserNameResponse500.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[
    DeleteTeamKeysUserNameResponse200
    | DeleteTeamKeysUserNameResponse400
    | DeleteTeamKeysUserNameResponse401
    | DeleteTeamKeysUserNameResponse500
]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    user: str,
    name: str,
    *,
    client: AuthenticatedClient,
) -> Response[
    DeleteTeamKeysUserNameResponse200
    | DeleteTeamKeysUserNameResponse400
    | DeleteTeamKeysUserNameResponse401
    | DeleteTeamKeysUserNameResponse500
]:
    """Remove API key

     Remove an API key by user ID and key name (admin only)

    Args:
        user (str):
        name (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[DeleteTeamKeysUserNameResponse200 | DeleteTeamKeysUserNameResponse400 | DeleteTeamKeysUserNameResponse401 | DeleteTeamKeysUserNameResponse500]
    """

    kwargs = _get_kwargs(
        user=user,
        name=name,
    )

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

    return _build_response(client=client, response=response)


def sync(
    user: str,
    name: str,
    *,
    client: AuthenticatedClient,
) -> (
    DeleteTeamKeysUserNameResponse200
    | DeleteTeamKeysUserNameResponse400
    | DeleteTeamKeysUserNameResponse401
    | DeleteTeamKeysUserNameResponse500
    | None
):
    """Remove API key

     Remove an API key by user ID and key name (admin only)

    Args:
        user (str):
        name (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:
        DeleteTeamKeysUserNameResponse200 | DeleteTeamKeysUserNameResponse400 | DeleteTeamKeysUserNameResponse401 | DeleteTeamKeysUserNameResponse500
    """

    return sync_detailed(
        user=user,
        name=name,
        client=client,
    ).parsed


async def asyncio_detailed(
    user: str,
    name: str,
    *,
    client: AuthenticatedClient,
) -> Response[
    DeleteTeamKeysUserNameResponse200
    | DeleteTeamKeysUserNameResponse400
    | DeleteTeamKeysUserNameResponse401
    | DeleteTeamKeysUserNameResponse500
]:
    """Remove API key

     Remove an API key by user ID and key name (admin only)

    Args:
        user (str):
        name (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[DeleteTeamKeysUserNameResponse200 | DeleteTeamKeysUserNameResponse400 | DeleteTeamKeysUserNameResponse401 | DeleteTeamKeysUserNameResponse500]
    """

    kwargs = _get_kwargs(
        user=user,
        name=name,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    user: str,
    name: str,
    *,
    client: AuthenticatedClient,
) -> (
    DeleteTeamKeysUserNameResponse200
    | DeleteTeamKeysUserNameResponse400
    | DeleteTeamKeysUserNameResponse401
    | DeleteTeamKeysUserNameResponse500
    | None
):
    """Remove API key

     Remove an API key by user ID and key name (admin only)

    Args:
        user (str):
        name (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:
        DeleteTeamKeysUserNameResponse200 | DeleteTeamKeysUserNameResponse400 | DeleteTeamKeysUserNameResponse401 | DeleteTeamKeysUserNameResponse500
    """

    return (
        await asyncio_detailed(
            user=user,
            name=name,
            client=client,
        )
    ).parsed
