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.patch_cp_client_body import PatchCpClientBody
from ...models.patch_cp_client_response_400 import PatchCpClientResponse400
from ...models.patch_cp_client_response_403 import PatchCpClientResponse403
from ...models.patch_cp_client_response_500 import PatchCpClientResponse500
from ...models.pkg_routes_cp_client_update_client_response import PkgRoutesCpClientUpdateClientResponse
from ...types import Response


def _get_kwargs(
    client_path: str,
    *,
    body: PatchCpClientBody,
) -> dict[str, Any]:
    headers: dict[str, Any] = {}

    _kwargs: dict[str, Any] = {
        "method": "patch",
        "url": "/cp/{client_path}".format(
            client_path=quote(str(client_path), safe=""),
        ),
    }

    _kwargs["json"] = body.to_dict()

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

    _kwargs["headers"] = headers
    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> (
    PatchCpClientResponse400
    | PatchCpClientResponse403
    | PatchCpClientResponse500
    | PkgRoutesCpClientUpdateClientResponse
    | None
):
    if response.status_code == 200:
        response_200 = PkgRoutesCpClientUpdateClientResponse.from_dict(response.json())

        return response_200

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

        return response_400

    if response.status_code == 403:
        response_403 = PatchCpClientResponse403.from_dict(response.json())

        return response_403

    if response.status_code == 500:
        response_500 = PatchCpClientResponse500.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[
    PatchCpClientResponse400
    | PatchCpClientResponse403
    | PatchCpClientResponse500
    | PkgRoutesCpClientUpdateClientResponse
]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    client_path: str,
    *,
    client: AuthenticatedClient,
    body: PatchCpClientBody,
) -> Response[
    PatchCpClientResponse400
    | PatchCpClientResponse403
    | PatchCpClientResponse500
    | PkgRoutesCpClientUpdateClientResponse
]:
    """Update client (client portal)

     Update client information from client portal including name, phone, business type, EIN, FYE, NAICS
    code, address, contacts, external contacts, and locations. Sends notifications to related users.

    Args:
        client_path (str):
        body (PatchCpClientBody):

    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[PatchCpClientResponse400 | PatchCpClientResponse403 | PatchCpClientResponse500 | PkgRoutesCpClientUpdateClientResponse]
    """

    kwargs = _get_kwargs(
        client_path=client_path,
        body=body,
    )

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

    return _build_response(client=client, response=response)


def sync(
    client_path: str,
    *,
    client: AuthenticatedClient,
    body: PatchCpClientBody,
) -> (
    PatchCpClientResponse400
    | PatchCpClientResponse403
    | PatchCpClientResponse500
    | PkgRoutesCpClientUpdateClientResponse
    | None
):
    """Update client (client portal)

     Update client information from client portal including name, phone, business type, EIN, FYE, NAICS
    code, address, contacts, external contacts, and locations. Sends notifications to related users.

    Args:
        client_path (str):
        body (PatchCpClientBody):

    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:
        PatchCpClientResponse400 | PatchCpClientResponse403 | PatchCpClientResponse500 | PkgRoutesCpClientUpdateClientResponse
    """

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


async def asyncio_detailed(
    client_path: str,
    *,
    client: AuthenticatedClient,
    body: PatchCpClientBody,
) -> Response[
    PatchCpClientResponse400
    | PatchCpClientResponse403
    | PatchCpClientResponse500
    | PkgRoutesCpClientUpdateClientResponse
]:
    """Update client (client portal)

     Update client information from client portal including name, phone, business type, EIN, FYE, NAICS
    code, address, contacts, external contacts, and locations. Sends notifications to related users.

    Args:
        client_path (str):
        body (PatchCpClientBody):

    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[PatchCpClientResponse400 | PatchCpClientResponse403 | PatchCpClientResponse500 | PkgRoutesCpClientUpdateClientResponse]
    """

    kwargs = _get_kwargs(
        client_path=client_path,
        body=body,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    client_path: str,
    *,
    client: AuthenticatedClient,
    body: PatchCpClientBody,
) -> (
    PatchCpClientResponse400
    | PatchCpClientResponse403
    | PatchCpClientResponse500
    | PkgRoutesCpClientUpdateClientResponse
    | None
):
    """Update client (client portal)

     Update client information from client portal including name, phone, business type, EIN, FYE, NAICS
    code, address, contacts, external contacts, and locations. Sends notifications to related users.

    Args:
        client_path (str):
        body (PatchCpClientBody):

    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:
        PatchCpClientResponse400 | PatchCpClientResponse403 | PatchCpClientResponse500 | PkgRoutesCpClientUpdateClientResponse
    """

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