from http import HTTPStatus
from typing import Any

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.pkg_routes_sys_email_request import PkgRoutesSysEmailRequest
from ...models.pkg_routes_sys_email_response import PkgRoutesSysEmailResponse
from ...models.post_sys_email_response_400 import PostSysEmailResponse400
from ...models.post_sys_email_response_401 import PostSysEmailResponse401
from ...models.post_sys_email_response_404 import PostSysEmailResponse404
from ...models.post_sys_email_response_500 import PostSysEmailResponse500
from ...types import Response


def _get_kwargs(
    *,
    body: PkgRoutesSysEmailRequest,
    x_internal_key: str,
) -> dict[str, Any]:
    headers: dict[str, Any] = {}
    headers["x-internal-key"] = x_internal_key

    _kwargs: dict[str, Any] = {
        "method": "post",
        "url": "/sys/_email",
    }

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

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

    _kwargs["headers"] = headers
    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> (
    PkgRoutesSysEmailResponse
    | PostSysEmailResponse400
    | PostSysEmailResponse401
    | PostSysEmailResponse404
    | PostSysEmailResponse500
    | None
):
    if response.status_code == 200:
        response_200 = PkgRoutesSysEmailResponse.from_dict(response.json())

        return response_200

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

        return response_400

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

        return response_401

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

        return response_404

    if response.status_code == 500:
        response_500 = PostSysEmailResponse500.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[
    PkgRoutesSysEmailResponse
    | PostSysEmailResponse400
    | PostSysEmailResponse401
    | PostSysEmailResponse404
    | PostSysEmailResponse500
]:
    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: AuthenticatedClient | Client,
    body: PkgRoutesSysEmailRequest,
    x_internal_key: str,
) -> Response[
    PkgRoutesSysEmailResponse
    | PostSysEmailResponse400
    | PostSysEmailResponse401
    | PostSysEmailResponse404
    | PostSysEmailResponse500
]:
    """Send email (internal)

     Sends an email to a user or client. Requires internal authentication via x-internal-key header. If
    team is provided, sends a client email with team branding.

    Args:
        x_internal_key (str):
        body (PkgRoutesSysEmailRequest):

    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[PkgRoutesSysEmailResponse | PostSysEmailResponse400 | PostSysEmailResponse401 | PostSysEmailResponse404 | PostSysEmailResponse500]
    """

    kwargs = _get_kwargs(
        body=body,
        x_internal_key=x_internal_key,
    )

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

    return _build_response(client=client, response=response)


def sync(
    *,
    client: AuthenticatedClient | Client,
    body: PkgRoutesSysEmailRequest,
    x_internal_key: str,
) -> (
    PkgRoutesSysEmailResponse
    | PostSysEmailResponse400
    | PostSysEmailResponse401
    | PostSysEmailResponse404
    | PostSysEmailResponse500
    | None
):
    """Send email (internal)

     Sends an email to a user or client. Requires internal authentication via x-internal-key header. If
    team is provided, sends a client email with team branding.

    Args:
        x_internal_key (str):
        body (PkgRoutesSysEmailRequest):

    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:
        PkgRoutesSysEmailResponse | PostSysEmailResponse400 | PostSysEmailResponse401 | PostSysEmailResponse404 | PostSysEmailResponse500
    """

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


async def asyncio_detailed(
    *,
    client: AuthenticatedClient | Client,
    body: PkgRoutesSysEmailRequest,
    x_internal_key: str,
) -> Response[
    PkgRoutesSysEmailResponse
    | PostSysEmailResponse400
    | PostSysEmailResponse401
    | PostSysEmailResponse404
    | PostSysEmailResponse500
]:
    """Send email (internal)

     Sends an email to a user or client. Requires internal authentication via x-internal-key header. If
    team is provided, sends a client email with team branding.

    Args:
        x_internal_key (str):
        body (PkgRoutesSysEmailRequest):

    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[PkgRoutesSysEmailResponse | PostSysEmailResponse400 | PostSysEmailResponse401 | PostSysEmailResponse404 | PostSysEmailResponse500]
    """

    kwargs = _get_kwargs(
        body=body,
        x_internal_key=x_internal_key,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    *,
    client: AuthenticatedClient | Client,
    body: PkgRoutesSysEmailRequest,
    x_internal_key: str,
) -> (
    PkgRoutesSysEmailResponse
    | PostSysEmailResponse400
    | PostSysEmailResponse401
    | PostSysEmailResponse404
    | PostSysEmailResponse500
    | None
):
    """Send email (internal)

     Sends an email to a user or client. Requires internal authentication via x-internal-key header. If
    team is provided, sends a client email with team branding.

    Args:
        x_internal_key (str):
        body (PkgRoutesSysEmailRequest):

    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:
        PkgRoutesSysEmailResponse | PostSysEmailResponse400 | PostSysEmailResponse401 | PostSysEmailResponse404 | PostSysEmailResponse500
    """

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