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.cws_error_dto import CwsErrorDTO
from ...models.get_items_status import GetItemsStatus
from ...models.item import Item
from ...types import UNSET, Response, Unset


def _get_kwargs(
    customer_ekp: str,
    *,
    status: GetItemsStatus | Unset = GetItemsStatus.NEW,
    page: int | Unset = 0,
    page_size: int | Unset = 10,
    authorization: str = "Bearer [place  OAuth access_token here, without brackets]",
) -> dict[str, Any]:
    headers: dict[str, Any] = {}
    headers["Authorization"] = authorization

    params: dict[str, Any] = {}

    json_status: str | Unset = UNSET
    if not isinstance(status, Unset):
        json_status = status.value

    params["status"] = json_status

    params["page"] = page

    params["pageSize"] = page_size

    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/customers/{customer_ekp}/items".format(
            customer_ekp=quote(str(customer_ekp), safe=""),
        ),
        "params": params,
    }

    _kwargs["headers"] = headers
    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> CwsErrorDTO | list[Item] | None:
    if response.status_code == 200:
        response_200 = []
        _response_200 = response.json()
        for componentsschemas_list_of_items_item_data in _response_200:
            componentsschemas_list_of_items_item = Item.from_dict(componentsschemas_list_of_items_item_data)

            response_200.append(componentsschemas_list_of_items_item)

        return response_200

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

        return response_403

    if response.status_code == 422:
        response_422 = CwsErrorDTO.from_dict(response.json())

        return response_422

    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[CwsErrorDTO | list[Item]]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    customer_ekp: str,
    *,
    client: AuthenticatedClient | Client,
    status: GetItemsStatus | Unset = GetItemsStatus.NEW,
    page: int | Unset = 0,
    page_size: int | Unset = 10,
    authorization: str = "Bearer [place  OAuth access_token here, without brackets]",
) -> Response[CwsErrorDTO | list[Item]]:
    """Get Available Items

     Single items are not attached to an order. Adding items to an order can be done later.

    Args:
        customer_ekp (str):
        status (GetItemsStatus | Unset):  Default: GetItemsStatus.NEW.
        page (int | Unset):  Default: 0.
        page_size (int | Unset):  Default: 10.
        authorization (str):  Default: 'Bearer [place  OAuth access_token here, without
            brackets]'.

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

    kwargs = _get_kwargs(
        customer_ekp=customer_ekp,
        status=status,
        page=page,
        page_size=page_size,
        authorization=authorization,
    )

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

    return _build_response(client=client, response=response)


def sync(
    customer_ekp: str,
    *,
    client: AuthenticatedClient | Client,
    status: GetItemsStatus | Unset = GetItemsStatus.NEW,
    page: int | Unset = 0,
    page_size: int | Unset = 10,
    authorization: str = "Bearer [place  OAuth access_token here, without brackets]",
) -> CwsErrorDTO | list[Item] | None:
    """Get Available Items

     Single items are not attached to an order. Adding items to an order can be done later.

    Args:
        customer_ekp (str):
        status (GetItemsStatus | Unset):  Default: GetItemsStatus.NEW.
        page (int | Unset):  Default: 0.
        page_size (int | Unset):  Default: 10.
        authorization (str):  Default: 'Bearer [place  OAuth access_token here, without
            brackets]'.

    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:
        CwsErrorDTO | list[Item]
    """

    return sync_detailed(
        customer_ekp=customer_ekp,
        client=client,
        status=status,
        page=page,
        page_size=page_size,
        authorization=authorization,
    ).parsed


async def asyncio_detailed(
    customer_ekp: str,
    *,
    client: AuthenticatedClient | Client,
    status: GetItemsStatus | Unset = GetItemsStatus.NEW,
    page: int | Unset = 0,
    page_size: int | Unset = 10,
    authorization: str = "Bearer [place  OAuth access_token here, without brackets]",
) -> Response[CwsErrorDTO | list[Item]]:
    """Get Available Items

     Single items are not attached to an order. Adding items to an order can be done later.

    Args:
        customer_ekp (str):
        status (GetItemsStatus | Unset):  Default: GetItemsStatus.NEW.
        page (int | Unset):  Default: 0.
        page_size (int | Unset):  Default: 10.
        authorization (str):  Default: 'Bearer [place  OAuth access_token here, without
            brackets]'.

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

    kwargs = _get_kwargs(
        customer_ekp=customer_ekp,
        status=status,
        page=page,
        page_size=page_size,
        authorization=authorization,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    customer_ekp: str,
    *,
    client: AuthenticatedClient | Client,
    status: GetItemsStatus | Unset = GetItemsStatus.NEW,
    page: int | Unset = 0,
    page_size: int | Unset = 10,
    authorization: str = "Bearer [place  OAuth access_token here, without brackets]",
) -> CwsErrorDTO | list[Item] | None:
    """Get Available Items

     Single items are not attached to an order. Adding items to an order can be done later.

    Args:
        customer_ekp (str):
        status (GetItemsStatus | Unset):  Default: GetItemsStatus.NEW.
        page (int | Unset):  Default: 0.
        page_size (int | Unset):  Default: 10.
        authorization (str):  Default: 'Bearer [place  OAuth access_token here, without
            brackets]'.

    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:
        CwsErrorDTO | list[Item]
    """

    return (
        await asyncio_detailed(
            customer_ekp=customer_ekp,
            client=client,
            status=status,
            page=page,
            page_size=page_size,
            authorization=authorization,
        )
    ).parsed
