from http import HTTPStatus
from io import BytesIO
from typing import Any
from urllib.parse import quote

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.get_inbox_thread_thread_attachment_attachment_response_400 import (
    GetInboxThreadThreadAttachmentAttachmentResponse400,
)
from ...models.get_inbox_thread_thread_attachment_attachment_response_401 import (
    GetInboxThreadThreadAttachmentAttachmentResponse401,
)
from ...models.get_inbox_thread_thread_attachment_attachment_response_404 import (
    GetInboxThreadThreadAttachmentAttachmentResponse404,
)
from ...models.get_inbox_thread_thread_attachment_attachment_response_500 import (
    GetInboxThreadThreadAttachmentAttachmentResponse500,
)
from ...types import File, Response


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

    _kwargs: dict[str, Any] = {
        "method": "get",
        "url": "/inbox/thread/{thread}/attachment/{attachment}".format(
            thread=quote(str(thread), safe=""),
            attachment=quote(str(attachment), safe=""),
        ),
    }

    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> (
    File
    | GetInboxThreadThreadAttachmentAttachmentResponse400
    | GetInboxThreadThreadAttachmentAttachmentResponse401
    | GetInboxThreadThreadAttachmentAttachmentResponse404
    | GetInboxThreadThreadAttachmentAttachmentResponse500
    | None
):
    if response.status_code == 200:
        response_200 = File(payload=BytesIO(response.content))

        return response_200

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

        return response_400

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

        return response_401

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

        return response_404

    if response.status_code == 500:
        response_500 = GetInboxThreadThreadAttachmentAttachmentResponse500.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[
    File
    | GetInboxThreadThreadAttachmentAttachmentResponse400
    | GetInboxThreadThreadAttachmentAttachmentResponse401
    | GetInboxThreadThreadAttachmentAttachmentResponse404
    | GetInboxThreadThreadAttachmentAttachmentResponse500
]:
    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,
) -> Response[
    File
    | GetInboxThreadThreadAttachmentAttachmentResponse400
    | GetInboxThreadThreadAttachmentAttachmentResponse401
    | GetInboxThreadThreadAttachmentAttachmentResponse404
    | GetInboxThreadThreadAttachmentAttachmentResponse500
]:
    """Download email attachment

     Download an email attachment as binary data. Supports both GCP-stored attachments (by file ID) and
    email provider attachments (by source ID). The attachment ID can be either a MongoDB ObjectID (24
    chars) for GCP-stored files or an email provider attachment ID.

    Args:
        thread (str):
        attachment (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[File | GetInboxThreadThreadAttachmentAttachmentResponse400 | GetInboxThreadThreadAttachmentAttachmentResponse401 | GetInboxThreadThreadAttachmentAttachmentResponse404 | GetInboxThreadThreadAttachmentAttachmentResponse500]
    """

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

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

    return _build_response(client=client, response=response)


def sync(
    thread: str,
    attachment: str,
    *,
    client: AuthenticatedClient,
) -> (
    File
    | GetInboxThreadThreadAttachmentAttachmentResponse400
    | GetInboxThreadThreadAttachmentAttachmentResponse401
    | GetInboxThreadThreadAttachmentAttachmentResponse404
    | GetInboxThreadThreadAttachmentAttachmentResponse500
    | None
):
    """Download email attachment

     Download an email attachment as binary data. Supports both GCP-stored attachments (by file ID) and
    email provider attachments (by source ID). The attachment ID can be either a MongoDB ObjectID (24
    chars) for GCP-stored files or an email provider attachment ID.

    Args:
        thread (str):
        attachment (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:
        File | GetInboxThreadThreadAttachmentAttachmentResponse400 | GetInboxThreadThreadAttachmentAttachmentResponse401 | GetInboxThreadThreadAttachmentAttachmentResponse404 | GetInboxThreadThreadAttachmentAttachmentResponse500
    """

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


async def asyncio_detailed(
    thread: str,
    attachment: str,
    *,
    client: AuthenticatedClient,
) -> Response[
    File
    | GetInboxThreadThreadAttachmentAttachmentResponse400
    | GetInboxThreadThreadAttachmentAttachmentResponse401
    | GetInboxThreadThreadAttachmentAttachmentResponse404
    | GetInboxThreadThreadAttachmentAttachmentResponse500
]:
    """Download email attachment

     Download an email attachment as binary data. Supports both GCP-stored attachments (by file ID) and
    email provider attachments (by source ID). The attachment ID can be either a MongoDB ObjectID (24
    chars) for GCP-stored files or an email provider attachment ID.

    Args:
        thread (str):
        attachment (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[File | GetInboxThreadThreadAttachmentAttachmentResponse400 | GetInboxThreadThreadAttachmentAttachmentResponse401 | GetInboxThreadThreadAttachmentAttachmentResponse404 | GetInboxThreadThreadAttachmentAttachmentResponse500]
    """

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

    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,
) -> (
    File
    | GetInboxThreadThreadAttachmentAttachmentResponse400
    | GetInboxThreadThreadAttachmentAttachmentResponse401
    | GetInboxThreadThreadAttachmentAttachmentResponse404
    | GetInboxThreadThreadAttachmentAttachmentResponse500
    | None
):
    """Download email attachment

     Download an email attachment as binary data. Supports both GCP-stored attachments (by file ID) and
    email provider attachments (by source ID). The attachment ID can be either a MongoDB ObjectID (24
    chars) for GCP-stored files or an email provider attachment ID.

    Args:
        thread (str):
        attachment (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:
        File | GetInboxThreadThreadAttachmentAttachmentResponse400 | GetInboxThreadThreadAttachmentAttachmentResponse401 | GetInboxThreadThreadAttachmentAttachmentResponse404 | GetInboxThreadThreadAttachmentAttachmentResponse500
    """

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