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_inbox_draft_message_response_400 import PostInboxDraftMessageResponse400
from ...models.post_inbox_draft_message_response_401 import PostInboxDraftMessageResponse401
from ...models.post_inbox_draft_message_response_500 import PostInboxDraftMessageResponse500
from ...models.types_message import TypesMessage
from ...types import Response


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

    _kwargs: dict[str, Any] = {
        "method": "post",
        "url": "/inbox/draft/{message}".format(
            message=quote(str(message), safe=""),
        ),
    }

    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> (
    PostInboxDraftMessageResponse400
    | PostInboxDraftMessageResponse401
    | PostInboxDraftMessageResponse500
    | TypesMessage
    | None
):
    if response.status_code == 200:
        response_200 = TypesMessage.from_dict(response.json())

        return response_200

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

        return response_400

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

        return response_401

    if response.status_code == 500:
        response_500 = PostInboxDraftMessageResponse500.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[
    PostInboxDraftMessageResponse400
    | PostInboxDraftMessageResponse401
    | PostInboxDraftMessageResponse500
    | TypesMessage
]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    message: str,
    *,
    client: AuthenticatedClient,
) -> Response[
    PostInboxDraftMessageResponse400
    | PostInboxDraftMessageResponse401
    | PostInboxDraftMessageResponse500
    | TypesMessage
]:
    """Send draft

     Send an email draft. Handles secure send with attachments (creates client portal folders and
    invitations), validates recipient limits, manages private email settings, and processes the message
    after sending. Supports both Gmail and Outlook providers.

    Args:
        message (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[PostInboxDraftMessageResponse400 | PostInboxDraftMessageResponse401 | PostInboxDraftMessageResponse500 | TypesMessage]
    """

    kwargs = _get_kwargs(
        message=message,
    )

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

    return _build_response(client=client, response=response)


def sync(
    message: str,
    *,
    client: AuthenticatedClient,
) -> (
    PostInboxDraftMessageResponse400
    | PostInboxDraftMessageResponse401
    | PostInboxDraftMessageResponse500
    | TypesMessage
    | None
):
    """Send draft

     Send an email draft. Handles secure send with attachments (creates client portal folders and
    invitations), validates recipient limits, manages private email settings, and processes the message
    after sending. Supports both Gmail and Outlook providers.

    Args:
        message (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:
        PostInboxDraftMessageResponse400 | PostInboxDraftMessageResponse401 | PostInboxDraftMessageResponse500 | TypesMessage
    """

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


async def asyncio_detailed(
    message: str,
    *,
    client: AuthenticatedClient,
) -> Response[
    PostInboxDraftMessageResponse400
    | PostInboxDraftMessageResponse401
    | PostInboxDraftMessageResponse500
    | TypesMessage
]:
    """Send draft

     Send an email draft. Handles secure send with attachments (creates client portal folders and
    invitations), validates recipient limits, manages private email settings, and processes the message
    after sending. Supports both Gmail and Outlook providers.

    Args:
        message (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[PostInboxDraftMessageResponse400 | PostInboxDraftMessageResponse401 | PostInboxDraftMessageResponse500 | TypesMessage]
    """

    kwargs = _get_kwargs(
        message=message,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    message: str,
    *,
    client: AuthenticatedClient,
) -> (
    PostInboxDraftMessageResponse400
    | PostInboxDraftMessageResponse401
    | PostInboxDraftMessageResponse500
    | TypesMessage
    | None
):
    """Send draft

     Send an email draft. Handles secure send with attachments (creates client portal folders and
    invitations), validates recipient limits, manages private email settings, and processes the message
    after sending. Supports both Gmail and Outlook providers.

    Args:
        message (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:
        PostInboxDraftMessageResponse400 | PostInboxDraftMessageResponse401 | PostInboxDraftMessageResponse500 | TypesMessage
    """

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