# This file was auto-generated by Fern from our API Definition.

import typing
from json.decoder import JSONDecodeError

from ...core.api_error import ApiError
from ...core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ...core.http_response import AsyncHttpResponse, HttpResponse
from ...core.jsonable_encoder import jsonable_encoder
from ...core.pagination import AsyncPager, BaseHttpResponse, SyncPager
from ...core.request_options import RequestOptions
from ...core.serialization import convert_and_respect_annotation_metadata
from ...core.unchecked_base_model import construct_type
from ...requests.workweek_config import WorkweekConfigParams
from ...types.list_workweek_configs_response import ListWorkweekConfigsResponse
from ...types.update_workweek_config_response import UpdateWorkweekConfigResponse
from ...types.workweek_config import WorkweekConfig

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class RawWorkweekConfigsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def list(
        self,
        *,
        limit: typing.Optional[int] = None,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SyncPager[WorkweekConfig]:
        """
        Returns a list of `WorkweekConfig` instances for a business.

        Parameters
        ----------
        limit : typing.Optional[int]
            The maximum number of `WorkweekConfigs` results to return per page.

        cursor : typing.Optional[str]
            A pointer to the next page of `WorkweekConfig` results to fetch.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        SyncPager[WorkweekConfig]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/labor/workweek-configs",
            method="GET",
            params={
                "limit": limit,
                "cursor": cursor,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _parsed_response = typing.cast(
                    ListWorkweekConfigsResponse,
                    construct_type(
                        type_=ListWorkweekConfigsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                _items = _parsed_response.workweek_configs
                _parsed_next = _parsed_response.cursor
                _has_next = _parsed_next is not None and _parsed_next != ""
                _get_next = lambda: self.list(
                    limit=limit,
                    cursor=_parsed_next,
                    request_options=request_options,
                )
                return SyncPager(
                    has_next=_has_next, items=_items, get_next=_get_next, response=BaseHttpResponse(response=_response)
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def get(
        self, id: str, *, workweek_config: WorkweekConfigParams, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[UpdateWorkweekConfigResponse]:
        """
        Updates a `WorkweekConfig`.

        Parameters
        ----------
        id : str
            The UUID for the `WorkweekConfig` object being updated.

        workweek_config : WorkweekConfigParams
            The updated `WorkweekConfig` object.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[UpdateWorkweekConfigResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/labor/workweek-configs/{jsonable_encoder(id)}",
            method="PUT",
            json={
                "workweek_config": convert_and_respect_annotation_metadata(
                    object_=workweek_config, annotation=WorkweekConfigParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateWorkweekConfigResponse,
                    construct_type(
                        type_=UpdateWorkweekConfigResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


class AsyncRawWorkweekConfigsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list(
        self,
        *,
        limit: typing.Optional[int] = None,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncPager[WorkweekConfig]:
        """
        Returns a list of `WorkweekConfig` instances for a business.

        Parameters
        ----------
        limit : typing.Optional[int]
            The maximum number of `WorkweekConfigs` results to return per page.

        cursor : typing.Optional[str]
            A pointer to the next page of `WorkweekConfig` results to fetch.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncPager[WorkweekConfig]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/labor/workweek-configs",
            method="GET",
            params={
                "limit": limit,
                "cursor": cursor,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _parsed_response = typing.cast(
                    ListWorkweekConfigsResponse,
                    construct_type(
                        type_=ListWorkweekConfigsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                _items = _parsed_response.workweek_configs
                _parsed_next = _parsed_response.cursor
                _has_next = _parsed_next is not None and _parsed_next != ""

                async def _get_next():
                    return await self.list(
                        limit=limit,
                        cursor=_parsed_next,
                        request_options=request_options,
                    )

                return AsyncPager(
                    has_next=_has_next, items=_items, get_next=_get_next, response=BaseHttpResponse(response=_response)
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def get(
        self, id: str, *, workweek_config: WorkweekConfigParams, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[UpdateWorkweekConfigResponse]:
        """
        Updates a `WorkweekConfig`.

        Parameters
        ----------
        id : str
            The UUID for the `WorkweekConfig` object being updated.

        workweek_config : WorkweekConfigParams
            The updated `WorkweekConfig` object.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[UpdateWorkweekConfigResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/labor/workweek-configs/{jsonable_encoder(id)}",
            method="PUT",
            json={
                "workweek_config": convert_and_respect_annotation_metadata(
                    object_=workweek_config, annotation=WorkweekConfigParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateWorkweekConfigResponse,
                    construct_type(
                        type_=UpdateWorkweekConfigResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
