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.pkg_routes_inbox_thread_save_attachment_request import PkgRoutesInboxThreadSaveAttachmentRequest
from ...models.pkg_routes_inbox_thread_save_attachment_response import PkgRoutesInboxThreadSaveAttachmentResponse
from ...models.post_inbox_thread_thread_attachment_attachment_save_response_400 import (
    PostInboxThreadThreadAttachmentAttachmentSaveResponse400,
)
from ...models.post_inbox_thread_thread_attachment_attachment_save_response_401 import (
    PostInboxThreadThreadAttachmentAttachmentSaveResponse401,
)
from ...models.post_inbox_thread_thread_attachment_attachment_save_response_403 import (
    PostInboxThreadThreadAttachmentAttachmentSaveResponse403,
)
from ...models.post_inbox_thread_thread_attachment_attachment_save_response_404 import (
    PostInboxThreadThreadAttachmentAttachmentSaveResponse404,
)
from ...models.post_inbox_thread_thread_attachment_attachment_save_response_500 import (
    PostInboxThreadThreadAttachmentAttachmentSaveResponse500,
)
from ...types import Response


def _get_kwargs(
    thread: str,
    attachment: str,
    *,
    body: PkgRoutesInboxThreadSaveAttachmentRequest,
) -> dict[str, Any]:
    headers: dict[str, Any] = {}

    _kwargs: dict[str, Any] = {
        "method": "post",
        "url": "/inbox/thread/{thread}/attachment/{attachment}/_save".format(
            thread=quote(str(thread), safe=""),
            attachment=quote(str(attachment), 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
) -> (
    PkgRoutesInboxThreadSaveAttachmentResponse
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse400
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse401
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse403
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse404
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse500
    | None
):
    if response.status_code == 200:
        response_200 = PkgRoutesInboxThreadSaveAttachmentResponse.from_dict(response.json())

        return response_200

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

        return response_400

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

        return response_401

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

        return response_403

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

        return response_404

    if response.status_code == 500:
        response_500 = PostInboxThreadThreadAttachmentAttachmentSaveResponse500.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[
    PkgRoutesInboxThreadSaveAttachmentResponse
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse400
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse401
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse403
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse404
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse500
]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    thread: str,
    attachment: str,
    *,
    client: AuthenticatedClient,
    body: PkgRoutesInboxThreadSaveAttachmentRequest,
) -> Response[
    PkgRoutesInboxThreadSaveAttachmentResponse
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse400
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse401
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse403
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse404
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse500
]:
    """Save email attachment

     Download an email attachment and save it as a file in the specified folder. Supports both GCP-stored
    attachments (by file ID) and email provider attachments (by source ID). The parent folder must exist
    and the user must have write access.

    Args:
        thread (str):
        attachment (str):
        body (PkgRoutesInboxThreadSaveAttachmentRequest):

    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[PkgRoutesInboxThreadSaveAttachmentResponse | PostInboxThreadThreadAttachmentAttachmentSaveResponse400 | PostInboxThreadThreadAttachmentAttachmentSaveResponse401 | PostInboxThreadThreadAttachmentAttachmentSaveResponse403 | PostInboxThreadThreadAttachmentAttachmentSaveResponse404 | PostInboxThreadThreadAttachmentAttachmentSaveResponse500]
    """

    kwargs = _get_kwargs(
        thread=thread,
        attachment=attachment,
        body=body,
    )

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

    return _build_response(client=client, response=response)


def sync(
    thread: str,
    attachment: str,
    *,
    client: AuthenticatedClient,
    body: PkgRoutesInboxThreadSaveAttachmentRequest,
) -> (
    PkgRoutesInboxThreadSaveAttachmentResponse
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse400
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse401
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse403
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse404
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse500
    | None
):
    """Save email attachment

     Download an email attachment and save it as a file in the specified folder. Supports both GCP-stored
    attachments (by file ID) and email provider attachments (by source ID). The parent folder must exist
    and the user must have write access.

    Args:
        thread (str):
        attachment (str):
        body (PkgRoutesInboxThreadSaveAttachmentRequest):

    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:
        PkgRoutesInboxThreadSaveAttachmentResponse | PostInboxThreadThreadAttachmentAttachmentSaveResponse400 | PostInboxThreadThreadAttachmentAttachmentSaveResponse401 | PostInboxThreadThreadAttachmentAttachmentSaveResponse403 | PostInboxThreadThreadAttachmentAttachmentSaveResponse404 | PostInboxThreadThreadAttachmentAttachmentSaveResponse500
    """

    return sync_detailed(
        thread=thread,
        attachment=attachment,
        client=client,
        body=body,
    ).parsed


async def asyncio_detailed(
    thread: str,
    attachment: str,
    *,
    client: AuthenticatedClient,
    body: PkgRoutesInboxThreadSaveAttachmentRequest,
) -> Response[
    PkgRoutesInboxThreadSaveAttachmentResponse
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse400
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse401
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse403
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse404
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse500
]:
    """Save email attachment

     Download an email attachment and save it as a file in the specified folder. Supports both GCP-stored
    attachments (by file ID) and email provider attachments (by source ID). The parent folder must exist
    and the user must have write access.

    Args:
        thread (str):
        attachment (str):
        body (PkgRoutesInboxThreadSaveAttachmentRequest):

    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[PkgRoutesInboxThreadSaveAttachmentResponse | PostInboxThreadThreadAttachmentAttachmentSaveResponse400 | PostInboxThreadThreadAttachmentAttachmentSaveResponse401 | PostInboxThreadThreadAttachmentAttachmentSaveResponse403 | PostInboxThreadThreadAttachmentAttachmentSaveResponse404 | PostInboxThreadThreadAttachmentAttachmentSaveResponse500]
    """

    kwargs = _get_kwargs(
        thread=thread,
        attachment=attachment,
        body=body,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    thread: str,
    attachment: str,
    *,
    client: AuthenticatedClient,
    body: PkgRoutesInboxThreadSaveAttachmentRequest,
) -> (
    PkgRoutesInboxThreadSaveAttachmentResponse
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse400
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse401
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse403
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse404
    | PostInboxThreadThreadAttachmentAttachmentSaveResponse500
    | None
):
    """Save email attachment

     Download an email attachment and save it as a file in the specified folder. Supports both GCP-stored
    attachments (by file ID) and email provider attachments (by source ID). The parent folder must exist
    and the user must have write access.

    Args:
        thread (str):
        attachment (str):
        body (PkgRoutesInboxThreadSaveAttachmentRequest):

    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:
        PkgRoutesInboxThreadSaveAttachmentResponse | PostInboxThreadThreadAttachmentAttachmentSaveResponse400 | PostInboxThreadThreadAttachmentAttachmentSaveResponse401 | PostInboxThreadThreadAttachmentAttachmentSaveResponse403 | PostInboxThreadThreadAttachmentAttachmentSaveResponse404 | PostInboxThreadThreadAttachmentAttachmentSaveResponse500
    """

    return (
        await asyncio_detailed(
            thread=thread,
            attachment=attachment,
            client=client,
            body=body,
        )
    ).parsed
