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.get_inbox_thread_thread_response_400 import GetInboxThreadThreadResponse400
from ...models.get_inbox_thread_thread_response_401 import GetInboxThreadThreadResponse401
from ...models.get_inbox_thread_thread_response_403 import GetInboxThreadThreadResponse403
from ...models.get_inbox_thread_thread_response_500 import GetInboxThreadThreadResponse500
from ...models.pkg_routes_inbox_thread_get_thread_response import PkgRoutesInboxThreadGetThreadResponse
from ...types import UNSET, Response, Unset


def _get_kwargs(
    thread: str,
    *,
    attachments: str | Unset = UNSET,
) -> dict[str, Any]:

    params: dict[str, Any] = {}

    params["attachments"] = attachments

    params = {k: v for k, v in params.items() if v is not UNSET and v is not None}

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

    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> (
    GetInboxThreadThreadResponse400
    | GetInboxThreadThreadResponse401
    | GetInboxThreadThreadResponse403
    | GetInboxThreadThreadResponse500
    | PkgRoutesInboxThreadGetThreadResponse
    | None
):
    if response.status_code == 200:
        response_200 = PkgRoutesInboxThreadGetThreadResponse.from_dict(response.json())

        return response_200

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

        return response_400

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

        return response_401

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

        return response_403

    if response.status_code == 500:
        response_500 = GetInboxThreadThreadResponse500.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[
    GetInboxThreadThreadResponse400
    | GetInboxThreadThreadResponse401
    | GetInboxThreadThreadResponse403
    | GetInboxThreadThreadResponse500
    | PkgRoutesInboxThreadGetThreadResponse
]:
    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,
    *,
    client: AuthenticatedClient,
    attachments: str | Unset = UNSET,
) -> Response[
    GetInboxThreadThreadResponse400
    | GetInboxThreadThreadResponse401
    | GetInboxThreadThreadResponse403
    | GetInboxThreadThreadResponse500
    | PkgRoutesInboxThreadGetThreadResponse
]:
    """Get email thread

     Retrieve all messages in an email thread with associated clients and tasks. Optionally presign
    attachment URLs for embedded attachments. Access is restricted based on message ownership and client
    associations.

    Args:
        thread (str):
        attachments (str | Unset):

    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[GetInboxThreadThreadResponse400 | GetInboxThreadThreadResponse401 | GetInboxThreadThreadResponse403 | GetInboxThreadThreadResponse500 | PkgRoutesInboxThreadGetThreadResponse]
    """

    kwargs = _get_kwargs(
        thread=thread,
        attachments=attachments,
    )

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

    return _build_response(client=client, response=response)


def sync(
    thread: str,
    *,
    client: AuthenticatedClient,
    attachments: str | Unset = UNSET,
) -> (
    GetInboxThreadThreadResponse400
    | GetInboxThreadThreadResponse401
    | GetInboxThreadThreadResponse403
    | GetInboxThreadThreadResponse500
    | PkgRoutesInboxThreadGetThreadResponse
    | None
):
    """Get email thread

     Retrieve all messages in an email thread with associated clients and tasks. Optionally presign
    attachment URLs for embedded attachments. Access is restricted based on message ownership and client
    associations.

    Args:
        thread (str):
        attachments (str | Unset):

    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:
        GetInboxThreadThreadResponse400 | GetInboxThreadThreadResponse401 | GetInboxThreadThreadResponse403 | GetInboxThreadThreadResponse500 | PkgRoutesInboxThreadGetThreadResponse
    """

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


async def asyncio_detailed(
    thread: str,
    *,
    client: AuthenticatedClient,
    attachments: str | Unset = UNSET,
) -> Response[
    GetInboxThreadThreadResponse400
    | GetInboxThreadThreadResponse401
    | GetInboxThreadThreadResponse403
    | GetInboxThreadThreadResponse500
    | PkgRoutesInboxThreadGetThreadResponse
]:
    """Get email thread

     Retrieve all messages in an email thread with associated clients and tasks. Optionally presign
    attachment URLs for embedded attachments. Access is restricted based on message ownership and client
    associations.

    Args:
        thread (str):
        attachments (str | Unset):

    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[GetInboxThreadThreadResponse400 | GetInboxThreadThreadResponse401 | GetInboxThreadThreadResponse403 | GetInboxThreadThreadResponse500 | PkgRoutesInboxThreadGetThreadResponse]
    """

    kwargs = _get_kwargs(
        thread=thread,
        attachments=attachments,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    thread: str,
    *,
    client: AuthenticatedClient,
    attachments: str | Unset = UNSET,
) -> (
    GetInboxThreadThreadResponse400
    | GetInboxThreadThreadResponse401
    | GetInboxThreadThreadResponse403
    | GetInboxThreadThreadResponse500
    | PkgRoutesInboxThreadGetThreadResponse
    | None
):
    """Get email thread

     Retrieve all messages in an email thread with associated clients and tasks. Optionally presign
    attachment URLs for embedded attachments. Access is restricted based on message ownership and client
    associations.

    Args:
        thread (str):
        attachments (str | Unset):

    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:
        GetInboxThreadThreadResponse400 | GetInboxThreadThreadResponse401 | GetInboxThreadThreadResponse403 | GetInboxThreadThreadResponse500 | PkgRoutesInboxThreadGetThreadResponse
    """

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