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.post_contacts_contact_deactivate_body import PostContactsContactDeactivateBody
from ...models.post_contacts_contact_deactivate_response_200 import PostContactsContactDeactivateResponse200
from ...models.post_contacts_contact_deactivate_response_400 import PostContactsContactDeactivateResponse400
from ...models.post_contacts_contact_deactivate_response_401 import PostContactsContactDeactivateResponse401
from ...models.post_contacts_contact_deactivate_response_500 import PostContactsContactDeactivateResponse500
from ...types import UNSET, Response, Unset


def _get_kwargs(
    contact: str,
    *,
    body: PostContactsContactDeactivateBody | Unset = UNSET,
) -> dict[str, Any]:
    headers: dict[str, Any] = {}

    _kwargs: dict[str, Any] = {
        "method": "post",
        "url": "/contacts/{contact}/_deactivate".format(
            contact=quote(str(contact), safe=""),
        ),
    }

    if not isinstance(body, Unset):
        _kwargs["json"] = body.to_dict()

    headers["Content-Type"] = "application/json"

    _kwargs["headers"] = headers
    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> (
    PostContactsContactDeactivateResponse200
    | PostContactsContactDeactivateResponse400
    | PostContactsContactDeactivateResponse401
    | PostContactsContactDeactivateResponse500
    | None
):
    if response.status_code == 200:
        response_200 = PostContactsContactDeactivateResponse200.from_dict(response.json())

        return response_200

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

        return response_400

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

        return response_401

    if response.status_code == 500:
        response_500 = PostContactsContactDeactivateResponse500.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[
    PostContactsContactDeactivateResponse200
    | PostContactsContactDeactivateResponse400
    | PostContactsContactDeactivateResponse401
    | PostContactsContactDeactivateResponse500
]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    contact: str,
    *,
    client: AuthenticatedClient,
    body: PostContactsContactDeactivateBody | Unset = UNSET,
) -> Response[
    PostContactsContactDeactivateResponse200
    | PostContactsContactDeactivateResponse400
    | PostContactsContactDeactivateResponse401
    | PostContactsContactDeactivateResponse500
]:
    """Deactivate contact

     Deactivate a contact by marking all associations as inactive, removing portal/collaborator access,
    and optionally transferring relationships to another contact (admin or clients/w scope required)

    Args:
        contact (str):
        body (PostContactsContactDeactivateBody | Unset):

    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[PostContactsContactDeactivateResponse200 | PostContactsContactDeactivateResponse400 | PostContactsContactDeactivateResponse401 | PostContactsContactDeactivateResponse500]
    """

    kwargs = _get_kwargs(
        contact=contact,
        body=body,
    )

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

    return _build_response(client=client, response=response)


def sync(
    contact: str,
    *,
    client: AuthenticatedClient,
    body: PostContactsContactDeactivateBody | Unset = UNSET,
) -> (
    PostContactsContactDeactivateResponse200
    | PostContactsContactDeactivateResponse400
    | PostContactsContactDeactivateResponse401
    | PostContactsContactDeactivateResponse500
    | None
):
    """Deactivate contact

     Deactivate a contact by marking all associations as inactive, removing portal/collaborator access,
    and optionally transferring relationships to another contact (admin or clients/w scope required)

    Args:
        contact (str):
        body (PostContactsContactDeactivateBody | Unset):

    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:
        PostContactsContactDeactivateResponse200 | PostContactsContactDeactivateResponse400 | PostContactsContactDeactivateResponse401 | PostContactsContactDeactivateResponse500
    """

    return sync_detailed(
        contact=contact,
        client=client,
        body=body,
    ).parsed


async def asyncio_detailed(
    contact: str,
    *,
    client: AuthenticatedClient,
    body: PostContactsContactDeactivateBody | Unset = UNSET,
) -> Response[
    PostContactsContactDeactivateResponse200
    | PostContactsContactDeactivateResponse400
    | PostContactsContactDeactivateResponse401
    | PostContactsContactDeactivateResponse500
]:
    """Deactivate contact

     Deactivate a contact by marking all associations as inactive, removing portal/collaborator access,
    and optionally transferring relationships to another contact (admin or clients/w scope required)

    Args:
        contact (str):
        body (PostContactsContactDeactivateBody | Unset):

    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[PostContactsContactDeactivateResponse200 | PostContactsContactDeactivateResponse400 | PostContactsContactDeactivateResponse401 | PostContactsContactDeactivateResponse500]
    """

    kwargs = _get_kwargs(
        contact=contact,
        body=body,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    contact: str,
    *,
    client: AuthenticatedClient,
    body: PostContactsContactDeactivateBody | Unset = UNSET,
) -> (
    PostContactsContactDeactivateResponse200
    | PostContactsContactDeactivateResponse400
    | PostContactsContactDeactivateResponse401
    | PostContactsContactDeactivateResponse500
    | None
):
    """Deactivate contact

     Deactivate a contact by marking all associations as inactive, removing portal/collaborator access,
    and optionally transferring relationships to another contact (admin or clients/w scope required)

    Args:
        contact (str):
        body (PostContactsContactDeactivateBody | Unset):

    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:
        PostContactsContactDeactivateResponse200 | PostContactsContactDeactivateResponse400 | PostContactsContactDeactivateResponse401 | PostContactsContactDeactivateResponse500
    """

    return (
        await asyncio_detailed(
            contact=contact,
            client=client,
            body=body,
        )
    ).parsed
