from http import HTTPStatus
from typing import Any, cast

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.cws_error_dto import CwsErrorDTO
from ...models.get_bag_tag_label_accept import GetBagTagLabelAccept
from ...types import UNSET, Response


def _get_kwargs(
    *,
    bag_ids: list[str],
    authorization: str = "Bearer [place  OAuth access_token here, without brackets]",
    accept: GetBagTagLabelAccept,
) -> dict[str, Any]:
    headers: dict[str, Any] = {}
    headers["Authorization"] = authorization

    headers["accept"] = str(accept)

    params: dict[str, Any] = {}

    json_bag_ids = bag_ids

    params["bagIds"] = json_bag_ids

    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": "/dpi/shipping/v1/bulk/label",
        "params": params,
    }

    _kwargs["headers"] = headers
    return _kwargs


def _parse_response(*, client: AuthenticatedClient | Client, response: httpx.Response) -> Any | CwsErrorDTO | None:
    if response.status_code == 200:
        response_200 = cast(Any, None)
        return response_200

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

        return response_403

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

        return response_404

    if response.status_code == 500:
        response_500 = CwsErrorDTO.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[Any | CwsErrorDTO]:
    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,
    bag_ids: list[str],
    authorization: str = "Bearer [place  OAuth access_token here, without brackets]",
    accept: GetBagTagLabelAccept,
) -> Response[Any | CwsErrorDTO]:
    """Get Bag Tag Label

     For a given bag get the label of the bag tag, sentt as a bytestream response.

    Args:
        bag_ids (list[str]):
        authorization (str):  Default: 'Bearer [place  OAuth access_token here, without
            brackets]'.
        accept (GetBagTagLabelAccept):

    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[Any | CwsErrorDTO]
    """

    kwargs = _get_kwargs(
        bag_ids=bag_ids,
        authorization=authorization,
        accept=accept,
    )

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

    return _build_response(client=client, response=response)


def sync(
    *,
    client: AuthenticatedClient | Client,
    bag_ids: list[str],
    authorization: str = "Bearer [place  OAuth access_token here, without brackets]",
    accept: GetBagTagLabelAccept,
) -> Any | CwsErrorDTO | None:
    """Get Bag Tag Label

     For a given bag get the label of the bag tag, sentt as a bytestream response.

    Args:
        bag_ids (list[str]):
        authorization (str):  Default: 'Bearer [place  OAuth access_token here, without
            brackets]'.
        accept (GetBagTagLabelAccept):

    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:
        Any | CwsErrorDTO
    """

    return sync_detailed(
        client=client,
        bag_ids=bag_ids,
        authorization=authorization,
        accept=accept,
    ).parsed


async def asyncio_detailed(
    *,
    client: AuthenticatedClient | Client,
    bag_ids: list[str],
    authorization: str = "Bearer [place  OAuth access_token here, without brackets]",
    accept: GetBagTagLabelAccept,
) -> Response[Any | CwsErrorDTO]:
    """Get Bag Tag Label

     For a given bag get the label of the bag tag, sentt as a bytestream response.

    Args:
        bag_ids (list[str]):
        authorization (str):  Default: 'Bearer [place  OAuth access_token here, without
            brackets]'.
        accept (GetBagTagLabelAccept):

    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[Any | CwsErrorDTO]
    """

    kwargs = _get_kwargs(
        bag_ids=bag_ids,
        authorization=authorization,
        accept=accept,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    *,
    client: AuthenticatedClient | Client,
    bag_ids: list[str],
    authorization: str = "Bearer [place  OAuth access_token here, without brackets]",
    accept: GetBagTagLabelAccept,
) -> Any | CwsErrorDTO | None:
    """Get Bag Tag Label

     For a given bag get the label of the bag tag, sentt as a bytestream response.

    Args:
        bag_ids (list[str]):
        authorization (str):  Default: 'Bearer [place  OAuth access_token here, without
            brackets]'.
        accept (GetBagTagLabelAccept):

    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:
        Any | CwsErrorDTO
    """

    return (
        await asyncio_detailed(
            client=client,
            bag_ids=bag_ids,
            authorization=authorization,
            accept=accept,
        )
    ).parsed
