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

import typing
from ..core.client_wrapper import SyncClientWrapper
from ..core.request_options import RequestOptions
from ..core.http_response import HttpResponse
from ..types.list_jobs_response import ListJobsResponse
from ..core.unchecked_base_model import construct_type
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..requests.job import JobParams
from ..types.create_job_response import CreateJobResponse
from ..core.serialization import convert_and_respect_annotation_metadata
from ..types.retrieve_job_response import RetrieveJobResponse
from ..core.jsonable_encoder import jsonable_encoder
from ..types.update_job_response import UpdateJobResponse
from ..core.client_wrapper import AsyncClientWrapper
from ..core.http_response import AsyncHttpResponse

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


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

    def list_jobs(
        self, *, cursor: typing.Optional[str] = None, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[ListJobsResponse]:
        """
        Lists jobs in a seller account. Results are sorted by title in ascending order.

        Parameters
        ----------
        cursor : typing.Optional[str]
            The pagination cursor returned by the previous call to this endpoint. Provide this
            cursor to retrieve the next page of results for your original request. For more information,
            see [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination).

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

        Returns
        -------
        HttpResponse[ListJobsResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/team-members/jobs",
            method="GET",
            params={
                "cursor": cursor,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ListJobsResponse,
                    construct_type(
                        type_=ListJobsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def create_job(
        self, *, job: JobParams, idempotency_key: str, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[CreateJobResponse]:
        """
        Creates a job in a seller account. A job defines a title and tip eligibility. Note that
        compensation is defined in a [job assignment](entity:JobAssignment) in a team member's wage setting.

        Parameters
        ----------
        job : JobParams
            The job to create. The `title` field is required and `is_tip_eligible` defaults to true.

        idempotency_key : str
            A unique identifier for the `CreateJob` request. Keys can be any valid string,
            but must be unique for each request. For more information, see
            [Idempotency](https://developer.squareup.com/docs/build-basics/common-api-patterns/idempotency).

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

        Returns
        -------
        HttpResponse[CreateJobResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/team-members/jobs",
            method="POST",
            json={
                "job": convert_and_respect_annotation_metadata(object_=job, annotation=JobParams, direction="write"),
                "idempotency_key": idempotency_key,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateJobResponse,
                    construct_type(
                        type_=CreateJobResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def retrieve_job(
        self, job_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[RetrieveJobResponse]:
        """
        Retrieves a specified job.

        Parameters
        ----------
        job_id : str
            The ID of the job to retrieve.

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

        Returns
        -------
        HttpResponse[RetrieveJobResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/team-members/jobs/{jsonable_encoder(job_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RetrieveJobResponse,
                    construct_type(
                        type_=RetrieveJobResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def update_job(
        self, job_id: str, *, job: JobParams, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[UpdateJobResponse]:
        """
        Updates the title or tip eligibility of a job. Changes to the title propagate to all
        `JobAssignment`, `Shift`, and `TeamMemberWage` objects that reference the job ID. Changes to
        tip eligibility propagate to all `TeamMemberWage` objects that reference the job ID.

        Parameters
        ----------
        job_id : str
            The ID of the job to update.

        job : JobParams
            The job with the updated fields, either `title`, `is_tip_eligible`, or both. Only changed fields need
            to be included in the request. Optionally include `version` to enable optimistic concurrency control.

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

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


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

    async def list_jobs(
        self, *, cursor: typing.Optional[str] = None, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[ListJobsResponse]:
        """
        Lists jobs in a seller account. Results are sorted by title in ascending order.

        Parameters
        ----------
        cursor : typing.Optional[str]
            The pagination cursor returned by the previous call to this endpoint. Provide this
            cursor to retrieve the next page of results for your original request. For more information,
            see [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination).

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

        Returns
        -------
        AsyncHttpResponse[ListJobsResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/team-members/jobs",
            method="GET",
            params={
                "cursor": cursor,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ListJobsResponse,
                    construct_type(
                        type_=ListJobsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def create_job(
        self, *, job: JobParams, idempotency_key: str, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[CreateJobResponse]:
        """
        Creates a job in a seller account. A job defines a title and tip eligibility. Note that
        compensation is defined in a [job assignment](entity:JobAssignment) in a team member's wage setting.

        Parameters
        ----------
        job : JobParams
            The job to create. The `title` field is required and `is_tip_eligible` defaults to true.

        idempotency_key : str
            A unique identifier for the `CreateJob` request. Keys can be any valid string,
            but must be unique for each request. For more information, see
            [Idempotency](https://developer.squareup.com/docs/build-basics/common-api-patterns/idempotency).

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

        Returns
        -------
        AsyncHttpResponse[CreateJobResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/team-members/jobs",
            method="POST",
            json={
                "job": convert_and_respect_annotation_metadata(object_=job, annotation=JobParams, direction="write"),
                "idempotency_key": idempotency_key,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateJobResponse,
                    construct_type(
                        type_=CreateJobResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def retrieve_job(
        self, job_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[RetrieveJobResponse]:
        """
        Retrieves a specified job.

        Parameters
        ----------
        job_id : str
            The ID of the job to retrieve.

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

        Returns
        -------
        AsyncHttpResponse[RetrieveJobResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/team-members/jobs/{jsonable_encoder(job_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RetrieveJobResponse,
                    construct_type(
                        type_=RetrieveJobResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def update_job(
        self, job_id: str, *, job: JobParams, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[UpdateJobResponse]:
        """
        Updates the title or tip eligibility of a job. Changes to the title propagate to all
        `JobAssignment`, `Shift`, and `TeamMemberWage` objects that reference the job ID. Changes to
        tip eligibility propagate to all `TeamMemberWage` objects that reference the job ID.

        Parameters
        ----------
        job_id : str
            The ID of the job to update.

        job : JobParams
            The job with the updated fields, either `title`, `is_tip_eligible`, or both. Only changed fields need
            to be included in the request. Optionally include `version` to enable optimistic concurrency control.

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

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