# 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.break_type import BreakTypeParams
from ...types.break_type import BreakType
from ...types.create_break_type_response import CreateBreakTypeResponse
from ...types.delete_break_type_response import DeleteBreakTypeResponse
from ...types.get_break_type_response import GetBreakTypeResponse
from ...types.list_break_types_response import ListBreakTypesResponse
from ...types.update_break_type_response import UpdateBreakTypeResponse

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


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

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

        Parameters
        ----------
        location_id : typing.Optional[str]
            Filter the returned `BreakType` results to only those that are associated with the
            specified location.

        limit : typing.Optional[int]
            The maximum number of `BreakType` results to return per page. The number can range between 1
            and 200. The default is 200.

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

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

        Returns
        -------
        SyncPager[BreakType]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/labor/break-types",
            method="GET",
            params={
                "location_id": location_id,
                "limit": limit,
                "cursor": cursor,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _parsed_response = typing.cast(
                    ListBreakTypesResponse,
                    construct_type(
                        type_=ListBreakTypesResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                _items = _parsed_response.break_types
                _parsed_next = _parsed_response.cursor
                _has_next = _parsed_next is not None and _parsed_next != ""
                _get_next = lambda: self.list(
                    location_id=location_id,
                    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 create(
        self,
        *,
        break_type: BreakTypeParams,
        idempotency_key: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[CreateBreakTypeResponse]:
        """
        Creates a new `BreakType`.

        A `BreakType` is a template for creating `Break` objects.
        You must provide the following values in your request to this
        endpoint:

        - `location_id`
        - `break_name`
        - `expected_duration`
        - `is_paid`

        You can only have three `BreakType` instances per location. If you attempt to add a fourth
        `BreakType` for a location, an `INVALID_REQUEST_ERROR` "Exceeded limit of 3 breaks per location."
        is returned.

        Parameters
        ----------
        break_type : BreakTypeParams
            The `BreakType` to be created.

        idempotency_key : typing.Optional[str]
            A unique string value to ensure the idempotency of the operation.

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

        Returns
        -------
        HttpResponse[CreateBreakTypeResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/labor/break-types",
            method="POST",
            json={
                "idempotency_key": idempotency_key,
                "break_type": convert_and_respect_annotation_metadata(
                    object_=break_type, annotation=BreakTypeParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateBreakTypeResponse,
                    construct_type(
                        type_=CreateBreakTypeResponse,  # 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)

    def get(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[GetBreakTypeResponse]:
        """
        Returns a single `BreakType` specified by `id`.

        Parameters
        ----------
        id : str
            The UUID for the `BreakType` being retrieved.

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

        Returns
        -------
        HttpResponse[GetBreakTypeResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/labor/break-types/{jsonable_encoder(id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetBreakTypeResponse,
                    construct_type(
                        type_=GetBreakTypeResponse,  # 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)

    def update(
        self, id: str, *, break_type: BreakTypeParams, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[UpdateBreakTypeResponse]:
        """
        Updates an existing `BreakType`.

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

        break_type : BreakTypeParams
            The updated `BreakType`.

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

        Returns
        -------
        HttpResponse[UpdateBreakTypeResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/labor/break-types/{jsonable_encoder(id)}",
            method="PUT",
            json={
                "break_type": convert_and_respect_annotation_metadata(
                    object_=break_type, annotation=BreakTypeParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateBreakTypeResponse,
                    construct_type(
                        type_=UpdateBreakTypeResponse,  # 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)

    def delete(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[DeleteBreakTypeResponse]:
        """
        Deletes an existing `BreakType`.

        A `BreakType` can be deleted even if it is referenced from a `Shift`.

        Parameters
        ----------
        id : str
            The UUID for the `BreakType` being deleted.

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

        Returns
        -------
        HttpResponse[DeleteBreakTypeResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/labor/break-types/{jsonable_encoder(id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DeleteBreakTypeResponse,
                    construct_type(
                        type_=DeleteBreakTypeResponse,  # 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 AsyncRawBreakTypesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

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

        Parameters
        ----------
        location_id : typing.Optional[str]
            Filter the returned `BreakType` results to only those that are associated with the
            specified location.

        limit : typing.Optional[int]
            The maximum number of `BreakType` results to return per page. The number can range between 1
            and 200. The default is 200.

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

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

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

                async def _get_next():
                    return await self.list(
                        location_id=location_id,
                        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 create(
        self,
        *,
        break_type: BreakTypeParams,
        idempotency_key: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[CreateBreakTypeResponse]:
        """
        Creates a new `BreakType`.

        A `BreakType` is a template for creating `Break` objects.
        You must provide the following values in your request to this
        endpoint:

        - `location_id`
        - `break_name`
        - `expected_duration`
        - `is_paid`

        You can only have three `BreakType` instances per location. If you attempt to add a fourth
        `BreakType` for a location, an `INVALID_REQUEST_ERROR` "Exceeded limit of 3 breaks per location."
        is returned.

        Parameters
        ----------
        break_type : BreakTypeParams
            The `BreakType` to be created.

        idempotency_key : typing.Optional[str]
            A unique string value to ensure the idempotency of the operation.

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

        Returns
        -------
        AsyncHttpResponse[CreateBreakTypeResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/labor/break-types",
            method="POST",
            json={
                "idempotency_key": idempotency_key,
                "break_type": convert_and_respect_annotation_metadata(
                    object_=break_type, annotation=BreakTypeParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateBreakTypeResponse,
                    construct_type(
                        type_=CreateBreakTypeResponse,  # 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)

    async def get(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[GetBreakTypeResponse]:
        """
        Returns a single `BreakType` specified by `id`.

        Parameters
        ----------
        id : str
            The UUID for the `BreakType` being retrieved.

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

        Returns
        -------
        AsyncHttpResponse[GetBreakTypeResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/labor/break-types/{jsonable_encoder(id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetBreakTypeResponse,
                    construct_type(
                        type_=GetBreakTypeResponse,  # 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)

    async def update(
        self, id: str, *, break_type: BreakTypeParams, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[UpdateBreakTypeResponse]:
        """
        Updates an existing `BreakType`.

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

        break_type : BreakTypeParams
            The updated `BreakType`.

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

        Returns
        -------
        AsyncHttpResponse[UpdateBreakTypeResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/labor/break-types/{jsonable_encoder(id)}",
            method="PUT",
            json={
                "break_type": convert_and_respect_annotation_metadata(
                    object_=break_type, annotation=BreakTypeParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateBreakTypeResponse,
                    construct_type(
                        type_=UpdateBreakTypeResponse,  # 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)

    async def delete(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[DeleteBreakTypeResponse]:
        """
        Deletes an existing `BreakType`.

        A `BreakType` can be deleted even if it is referenced from a `Shift`.

        Parameters
        ----------
        id : str
            The UUID for the `BreakType` being deleted.

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

        Returns
        -------
        AsyncHttpResponse[DeleteBreakTypeResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/labor/break-types/{jsonable_encoder(id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DeleteBreakTypeResponse,
                    construct_type(
                        type_=DeleteBreakTypeResponse,  # 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)
