# 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.jsonable_encoder import jsonable_encoder
from .....core.pydantic_utilities import pydantic_v1
from .....core.request_options import RequestOptions
from ....commons.types.claim_id import ClaimId
from ....commons.types.page_token import PageToken
from ....commons.types.patient_external_id import PatientExternalId
from ....commons.types.provider_id import ProviderId
from ....commons.types.service_line_id import ServiceLineId
from ....commons.types.sort_direction import SortDirection
from ....financials.types.account_type import AccountType
from ....payers.resources.v_3.types.payer_uuid import PayerUuid
from .types.create_write_offs_response import CreateWriteOffsResponse
from .types.write_off import WriteOff
from .types.write_off_create import WriteOffCreate
from .types.write_off_id import WriteOffId
from .types.write_off_sort_field import WriteOffSortField
from .types.write_offs_page import WriteOffsPage

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


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

    def get_multi(
        self,
        *,
        limit: typing.Optional[int] = None,
        patient_external_id: typing.Optional[PatientExternalId] = None,
        payer_uuid: typing.Optional[PayerUuid] = None,
        service_line_id: typing.Optional[ServiceLineId] = None,
        claim_id: typing.Optional[ClaimId] = None,
        billing_provider_id: typing.Optional[ProviderId] = None,
        sort: typing.Optional[WriteOffSortField] = None,
        sort_direction: typing.Optional[SortDirection] = None,
        page_token: typing.Optional[PageToken] = None,
        account_types: typing.Optional[typing.Union[AccountType, typing.Sequence[AccountType]]] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WriteOffsPage:
        """
        Returns all write-offs satisfying the search criteria.

        Parameters
        ----------
        limit : typing.Optional[int]
            Defaults to 100. The value must be greater than 0 and less than 1000.

        patient_external_id : typing.Optional[PatientExternalId]

        payer_uuid : typing.Optional[PayerUuid]

        service_line_id : typing.Optional[ServiceLineId]

        claim_id : typing.Optional[ClaimId]

        billing_provider_id : typing.Optional[ProviderId]

        sort : typing.Optional[WriteOffSortField]
            Defaults to write_off_timestamp

        sort_direction : typing.Optional[SortDirection]
            Sort direction. Defaults to descending order if not provided.

        page_token : typing.Optional[PageToken]

        account_types : typing.Optional[typing.Union[AccountType, typing.Sequence[AccountType]]]
            Filters the returned values to include only the provided account types.

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

        Returns
        -------
        WriteOffsPage

        Examples
        --------
        import uuid

        from candid import AccountType, SortDirection
        from candid.client import CandidApiClient
        from candid.resources.write_offs.v_1 import WriteOffSortField

        client = CandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        client.write_offs.v_1.get_multi(
            limit=1,
            patient_external_id="string",
            payer_uuid=uuid.UUID(
                "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
            ),
            service_line_id=uuid.UUID(
                "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
            ),
            claim_id=uuid.UUID(
                "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
            ),
            billing_provider_id=uuid.UUID(
                "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
            ),
            sort=WriteOffSortField.AMOUNT_CENTS,
            sort_direction=SortDirection.ASC,
            page_token="eyJ0b2tlbiI6IjEiLCJwYWdlX3Rva2VuIjoiMiJ9",
            account_types=AccountType.PATIENT,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/write-offs/v1",
            base_url=self._client_wrapper.get_environment().candid_api,
            method="GET",
            params={
                "limit": limit,
                "patient_external_id": patient_external_id,
                "payer_uuid": jsonable_encoder(payer_uuid),
                "service_line_id": jsonable_encoder(service_line_id),
                "claim_id": jsonable_encoder(claim_id),
                "billing_provider_id": jsonable_encoder(billing_provider_id),
                "sort": sort,
                "sort_direction": sort_direction,
                "page_token": page_token,
                "account_types": account_types,
            },
            request_options=request_options,
        )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(WriteOffsPage, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def get(self, write_off_id: WriteOffId, *, request_options: typing.Optional[RequestOptions] = None) -> WriteOff:
        """
        Retrieves a previously created write off by its `write_off_id`.

        Parameters
        ----------
        write_off_id : WriteOffId

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

        Returns
        -------
        WriteOff

        Examples
        --------
        import uuid

        from candid.client import CandidApiClient

        client = CandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        client.write_offs.v_1.get(
            write_off_id=uuid.UUID(
                "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/write-offs/v1/{jsonable_encoder(write_off_id)}",
            base_url=self._client_wrapper.get_environment().candid_api,
            method="GET",
            request_options=request_options,
        )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(WriteOff, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def create(
        self, *, write_offs: typing.Sequence[WriteOffCreate], request_options: typing.Optional[RequestOptions] = None
    ) -> CreateWriteOffsResponse:
        """
        Creates one or many write-offs applied toward a specific service line,
        claim, or billing provider.

        Parameters
        ----------
        write_offs : typing.Sequence[WriteOffCreate]

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

        Returns
        -------
        CreateWriteOffsResponse

        Examples
        --------
        import datetime
        import uuid

        from candid.client import CandidApiClient
        from candid.resources.write_offs.v_1 import (
            PatientWriteOffReason,
            WriteOffCreate_Patient,
        )

        client = CandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        client.write_offs.v_1.create(
            write_offs=[
                WriteOffCreate_Patient(
                    write_off_timestamp=datetime.datetime.fromisoformat(
                        "2024-01-15 09:30:00+00:00",
                    ),
                    write_off_note="string",
                    write_off_reason=PatientWriteOffReason.SMALL_BALANCE,
                    service_line_id=uuid.UUID(
                        "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
                    ),
                    amount_cents=1,
                )
            ],
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/write-offs/v1",
            base_url=self._client_wrapper.get_environment().candid_api,
            method="POST",
            json={"write_offs": write_offs},
            request_options=request_options,
            omit=OMIT,
        )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(CreateWriteOffsResponse, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def revert(self, write_off_id: WriteOffId, *, request_options: typing.Optional[RequestOptions] = None) -> WriteOff:
        """
        Reverts a write off given a `write_off_id`.

        Parameters
        ----------
        write_off_id : WriteOffId

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

        Returns
        -------
        WriteOff

        Examples
        --------
        import uuid

        from candid.client import CandidApiClient

        client = CandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        client.write_offs.v_1.revert(
            write_off_id=uuid.UUID(
                "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/write-offs/v1/{jsonable_encoder(write_off_id)}/revert",
            base_url=self._client_wrapper.get_environment().candid_api,
            method="POST",
            request_options=request_options,
        )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(WriteOff, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)


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

    async def get_multi(
        self,
        *,
        limit: typing.Optional[int] = None,
        patient_external_id: typing.Optional[PatientExternalId] = None,
        payer_uuid: typing.Optional[PayerUuid] = None,
        service_line_id: typing.Optional[ServiceLineId] = None,
        claim_id: typing.Optional[ClaimId] = None,
        billing_provider_id: typing.Optional[ProviderId] = None,
        sort: typing.Optional[WriteOffSortField] = None,
        sort_direction: typing.Optional[SortDirection] = None,
        page_token: typing.Optional[PageToken] = None,
        account_types: typing.Optional[typing.Union[AccountType, typing.Sequence[AccountType]]] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WriteOffsPage:
        """
        Returns all write-offs satisfying the search criteria.

        Parameters
        ----------
        limit : typing.Optional[int]
            Defaults to 100. The value must be greater than 0 and less than 1000.

        patient_external_id : typing.Optional[PatientExternalId]

        payer_uuid : typing.Optional[PayerUuid]

        service_line_id : typing.Optional[ServiceLineId]

        claim_id : typing.Optional[ClaimId]

        billing_provider_id : typing.Optional[ProviderId]

        sort : typing.Optional[WriteOffSortField]
            Defaults to write_off_timestamp

        sort_direction : typing.Optional[SortDirection]
            Sort direction. Defaults to descending order if not provided.

        page_token : typing.Optional[PageToken]

        account_types : typing.Optional[typing.Union[AccountType, typing.Sequence[AccountType]]]
            Filters the returned values to include only the provided account types.

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

        Returns
        -------
        WriteOffsPage

        Examples
        --------
        import asyncio
        import uuid

        from candid import AccountType, SortDirection
        from candid.client import AsyncCandidApiClient
        from candid.resources.write_offs.v_1 import WriteOffSortField

        client = AsyncCandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )


        async def main() -> None:
            await client.write_offs.v_1.get_multi(
                limit=1,
                patient_external_id="string",
                payer_uuid=uuid.UUID(
                    "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
                ),
                service_line_id=uuid.UUID(
                    "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
                ),
                claim_id=uuid.UUID(
                    "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
                ),
                billing_provider_id=uuid.UUID(
                    "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
                ),
                sort=WriteOffSortField.AMOUNT_CENTS,
                sort_direction=SortDirection.ASC,
                page_token="eyJ0b2tlbiI6IjEiLCJwYWdlX3Rva2VuIjoiMiJ9",
                account_types=AccountType.PATIENT,
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/write-offs/v1",
            base_url=self._client_wrapper.get_environment().candid_api,
            method="GET",
            params={
                "limit": limit,
                "patient_external_id": patient_external_id,
                "payer_uuid": jsonable_encoder(payer_uuid),
                "service_line_id": jsonable_encoder(service_line_id),
                "claim_id": jsonable_encoder(claim_id),
                "billing_provider_id": jsonable_encoder(billing_provider_id),
                "sort": sort,
                "sort_direction": sort_direction,
                "page_token": page_token,
                "account_types": account_types,
            },
            request_options=request_options,
        )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(WriteOffsPage, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def get(
        self, write_off_id: WriteOffId, *, request_options: typing.Optional[RequestOptions] = None
    ) -> WriteOff:
        """
        Retrieves a previously created write off by its `write_off_id`.

        Parameters
        ----------
        write_off_id : WriteOffId

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

        Returns
        -------
        WriteOff

        Examples
        --------
        import asyncio
        import uuid

        from candid.client import AsyncCandidApiClient

        client = AsyncCandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )


        async def main() -> None:
            await client.write_offs.v_1.get(
                write_off_id=uuid.UUID(
                    "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
                ),
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/write-offs/v1/{jsonable_encoder(write_off_id)}",
            base_url=self._client_wrapper.get_environment().candid_api,
            method="GET",
            request_options=request_options,
        )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(WriteOff, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def create(
        self, *, write_offs: typing.Sequence[WriteOffCreate], request_options: typing.Optional[RequestOptions] = None
    ) -> CreateWriteOffsResponse:
        """
        Creates one or many write-offs applied toward a specific service line,
        claim, or billing provider.

        Parameters
        ----------
        write_offs : typing.Sequence[WriteOffCreate]

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

        Returns
        -------
        CreateWriteOffsResponse

        Examples
        --------
        import asyncio
        import datetime
        import uuid

        from candid.client import AsyncCandidApiClient
        from candid.resources.write_offs.v_1 import (
            PatientWriteOffReason,
            WriteOffCreate_Patient,
        )

        client = AsyncCandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )


        async def main() -> None:
            await client.write_offs.v_1.create(
                write_offs=[
                    WriteOffCreate_Patient(
                        write_off_timestamp=datetime.datetime.fromisoformat(
                            "2024-01-15 09:30:00+00:00",
                        ),
                        write_off_note="string",
                        write_off_reason=PatientWriteOffReason.SMALL_BALANCE,
                        service_line_id=uuid.UUID(
                            "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
                        ),
                        amount_cents=1,
                    )
                ],
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/write-offs/v1",
            base_url=self._client_wrapper.get_environment().candid_api,
            method="POST",
            json={"write_offs": write_offs},
            request_options=request_options,
            omit=OMIT,
        )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(CreateWriteOffsResponse, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def revert(
        self, write_off_id: WriteOffId, *, request_options: typing.Optional[RequestOptions] = None
    ) -> WriteOff:
        """
        Reverts a write off given a `write_off_id`.

        Parameters
        ----------
        write_off_id : WriteOffId

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

        Returns
        -------
        WriteOff

        Examples
        --------
        import asyncio
        import uuid

        from candid.client import AsyncCandidApiClient

        client = AsyncCandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )


        async def main() -> None:
            await client.write_offs.v_1.revert(
                write_off_id=uuid.UUID(
                    "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
                ),
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/write-offs/v1/{jsonable_encoder(write_off_id)}/revert",
            base_url=self._client_wrapper.get_environment().candid_api,
            method="POST",
            request_options=request_options,
        )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(WriteOff, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)
