from http import HTTPStatus
from typing import Any

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.get_cp_payroll_list_response_500 import GetCpPayrollListResponse500
from ...models.pkg_routes_cp_payroll_connections_response import PkgRoutesCpPayrollConnectionsResponse
from ...types import UNSET, Response, Unset


def _get_kwargs(
    *,
    include_pending: str | Unset = UNSET,
    include_reports: str | Unset = UNSET,
    limit: int | Unset = UNSET,
    skip: int | Unset = UNSET,
) -> dict[str, Any]:

    params: dict[str, Any] = {}

    params["include_pending"] = include_pending

    params["include_reports"] = include_reports

    params["limit"] = limit

    params["skip"] = skip

    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": "/cp/payroll/_list",
        "params": params,
    }

    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> GetCpPayrollListResponse500 | PkgRoutesCpPayrollConnectionsResponse | None:
    if response.status_code == 200:
        response_200 = PkgRoutesCpPayrollConnectionsResponse.from_dict(response.json())

        return response_200

    if response.status_code == 500:
        response_500 = GetCpPayrollListResponse500.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[GetCpPayrollListResponse500 | PkgRoutesCpPayrollConnectionsResponse]:
    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,
    include_pending: str | Unset = UNSET,
    include_reports: str | Unset = UNSET,
    limit: int | Unset = UNSET,
    skip: int | Unset = UNSET,
) -> Response[GetCpPayrollListResponse500 | PkgRoutesCpPayrollConnectionsResponse]:
    """List payroll connections (client portal)

     List all payroll connections for the authenticated client with provider information. Supports
    filtering by status and provider type.

    Args:
        include_pending (str | Unset):
        include_reports (str | Unset):
        limit (int | Unset):
        skip (int | 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[GetCpPayrollListResponse500 | PkgRoutesCpPayrollConnectionsResponse]
    """

    kwargs = _get_kwargs(
        include_pending=include_pending,
        include_reports=include_reports,
        limit=limit,
        skip=skip,
    )

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

    return _build_response(client=client, response=response)


def sync(
    *,
    client: AuthenticatedClient,
    include_pending: str | Unset = UNSET,
    include_reports: str | Unset = UNSET,
    limit: int | Unset = UNSET,
    skip: int | Unset = UNSET,
) -> GetCpPayrollListResponse500 | PkgRoutesCpPayrollConnectionsResponse | None:
    """List payroll connections (client portal)

     List all payroll connections for the authenticated client with provider information. Supports
    filtering by status and provider type.

    Args:
        include_pending (str | Unset):
        include_reports (str | Unset):
        limit (int | Unset):
        skip (int | 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:
        GetCpPayrollListResponse500 | PkgRoutesCpPayrollConnectionsResponse
    """

    return sync_detailed(
        client=client,
        include_pending=include_pending,
        include_reports=include_reports,
        limit=limit,
        skip=skip,
    ).parsed


async def asyncio_detailed(
    *,
    client: AuthenticatedClient,
    include_pending: str | Unset = UNSET,
    include_reports: str | Unset = UNSET,
    limit: int | Unset = UNSET,
    skip: int | Unset = UNSET,
) -> Response[GetCpPayrollListResponse500 | PkgRoutesCpPayrollConnectionsResponse]:
    """List payroll connections (client portal)

     List all payroll connections for the authenticated client with provider information. Supports
    filtering by status and provider type.

    Args:
        include_pending (str | Unset):
        include_reports (str | Unset):
        limit (int | Unset):
        skip (int | 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[GetCpPayrollListResponse500 | PkgRoutesCpPayrollConnectionsResponse]
    """

    kwargs = _get_kwargs(
        include_pending=include_pending,
        include_reports=include_reports,
        limit=limit,
        skip=skip,
    )

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

    return _build_response(client=client, response=response)


async def asyncio(
    *,
    client: AuthenticatedClient,
    include_pending: str | Unset = UNSET,
    include_reports: str | Unset = UNSET,
    limit: int | Unset = UNSET,
    skip: int | Unset = UNSET,
) -> GetCpPayrollListResponse500 | PkgRoutesCpPayrollConnectionsResponse | None:
    """List payroll connections (client portal)

     List all payroll connections for the authenticated client with provider information. Supports
    filtering by status and provider type.

    Args:
        include_pending (str | Unset):
        include_reports (str | Unset):
        limit (int | Unset):
        skip (int | 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:
        GetCpPayrollListResponse500 | PkgRoutesCpPayrollConnectionsResponse
    """

    return (
        await asyncio_detailed(
            client=client,
            include_pending=include_pending,
            include_reports=include_reports,
            limit=limit,
            skip=skip,
        )
    ).parsed
