# 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 ....era.errors.era_not_fully_processed_error import EraNotFullyProcessedError
from ....era.types.era_not_fully_processed_error_message import EraNotFullyProcessedErrorMessage
from .types.insurance_adjudication import InsuranceAdjudication
from .types.insurance_adjudication_create import InsuranceAdjudicationCreate
from .types.insurance_adjudication_id import InsuranceAdjudicationId

# 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(
        self,
        insurance_adjudication_id: InsuranceAdjudicationId,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> InsuranceAdjudication:
        """
        Retrieves a previously created insurance adjudication by its `insurance_adjudication_id`.

        Parameters
        ----------
        insurance_adjudication_id : InsuranceAdjudicationId

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

        Returns
        -------
        InsuranceAdjudication

        Examples
        --------
        import uuid

        from candid.client import CandidApiClient

        client = CandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        client.insurance_adjudications.v_1.get(
            insurance_adjudication_id=uuid.UUID(
                "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/insurance-adjudications/v1/{jsonable_encoder(insurance_adjudication_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(InsuranceAdjudication, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def create(
        self, *, request: InsuranceAdjudicationCreate, request_options: typing.Optional[RequestOptions] = None
    ) -> InsuranceAdjudication:
        """
        Creates a new insurance adjudication record and returns the newly created InsuranceAdjudication object.

        Parameters
        ----------
        request : InsuranceAdjudicationCreate

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

        Returns
        -------
        InsuranceAdjudication

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

        from candid.client import CandidApiClient
        from candid.resources.insurance_adjudications.v_1 import (
            ClaimAdjudicationCreate,
            InsuranceAdjudicationCreate,
        )
        from candid.resources.payers.v_3 import PayerIdentifier_PayerInfo
        from candid.resources.remits.v_1 import Payee

        client = CandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        client.insurance_adjudications.v_1.create(
            request=InsuranceAdjudicationCreate(
                payer_identifier=PayerIdentifier_PayerInfo(),
                payee=Payee(),
                post_date=datetime.date.fromisoformat(
                    "2023-01-15",
                ),
                check_number="string",
                check_date=datetime.date.fromisoformat(
                    "2023-01-15",
                ),
                note="string",
                claims={
                    uuid.UUID(
                        "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
                    ): [ClaimAdjudicationCreate()]
                },
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/insurance-adjudications/v1",
            base_url=self._client_wrapper.get_environment().candid_api,
            method="POST",
            json=request,
            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(InsuranceAdjudication, _response_json)  # type: ignore
        if "errorName" in _response_json:
            if _response_json["errorName"] == "EraNotFullyProcessedError":
                raise EraNotFullyProcessedError(
                    pydantic_v1.parse_obj_as(EraNotFullyProcessedErrorMessage, _response_json["content"])  # type: ignore
                )
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def delete(
        self,
        insurance_adjudication_id: InsuranceAdjudicationId,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Deletes the insurance adjudication record matching the provided insurance_adjudication_id.

        Parameters
        ----------
        insurance_adjudication_id : InsuranceAdjudicationId

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

        Returns
        -------
        None

        Examples
        --------
        import uuid

        from candid.client import CandidApiClient

        client = CandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        client.insurance_adjudications.v_1.delete(
            insurance_adjudication_id=uuid.UUID(
                "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/insurance-adjudications/v1/{jsonable_encoder(insurance_adjudication_id)}",
            base_url=self._client_wrapper.get_environment().candid_api,
            method="DELETE",
            request_options=request_options,
        )
        if 200 <= _response.status_code < 300:
            return
        try:
            _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 AsyncV1Client:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def get(
        self,
        insurance_adjudication_id: InsuranceAdjudicationId,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> InsuranceAdjudication:
        """
        Retrieves a previously created insurance adjudication by its `insurance_adjudication_id`.

        Parameters
        ----------
        insurance_adjudication_id : InsuranceAdjudicationId

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

        Returns
        -------
        InsuranceAdjudication

        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.insurance_adjudications.v_1.get(
                insurance_adjudication_id=uuid.UUID(
                    "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
                ),
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/insurance-adjudications/v1/{jsonable_encoder(insurance_adjudication_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(InsuranceAdjudication, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def create(
        self, *, request: InsuranceAdjudicationCreate, request_options: typing.Optional[RequestOptions] = None
    ) -> InsuranceAdjudication:
        """
        Creates a new insurance adjudication record and returns the newly created InsuranceAdjudication object.

        Parameters
        ----------
        request : InsuranceAdjudicationCreate

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

        Returns
        -------
        InsuranceAdjudication

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

        from candid.client import AsyncCandidApiClient
        from candid.resources.insurance_adjudications.v_1 import (
            ClaimAdjudicationCreate,
            InsuranceAdjudicationCreate,
        )
        from candid.resources.payers.v_3 import PayerIdentifier_PayerInfo
        from candid.resources.remits.v_1 import Payee

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


        async def main() -> None:
            await client.insurance_adjudications.v_1.create(
                request=InsuranceAdjudicationCreate(
                    payer_identifier=PayerIdentifier_PayerInfo(),
                    payee=Payee(),
                    post_date=datetime.date.fromisoformat(
                        "2023-01-15",
                    ),
                    check_number="string",
                    check_date=datetime.date.fromisoformat(
                        "2023-01-15",
                    ),
                    note="string",
                    claims={
                        uuid.UUID(
                            "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
                        ): [ClaimAdjudicationCreate()]
                    },
                ),
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/insurance-adjudications/v1",
            base_url=self._client_wrapper.get_environment().candid_api,
            method="POST",
            json=request,
            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(InsuranceAdjudication, _response_json)  # type: ignore
        if "errorName" in _response_json:
            if _response_json["errorName"] == "EraNotFullyProcessedError":
                raise EraNotFullyProcessedError(
                    pydantic_v1.parse_obj_as(EraNotFullyProcessedErrorMessage, _response_json["content"])  # type: ignore
                )
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def delete(
        self,
        insurance_adjudication_id: InsuranceAdjudicationId,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Deletes the insurance adjudication record matching the provided insurance_adjudication_id.

        Parameters
        ----------
        insurance_adjudication_id : InsuranceAdjudicationId

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

        Returns
        -------
        None

        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.insurance_adjudications.v_1.delete(
                insurance_adjudication_id=uuid.UUID(
                    "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
                ),
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/insurance-adjudications/v1/{jsonable_encoder(insurance_adjudication_id)}",
            base_url=self._client_wrapper.get_environment().candid_api,
            method="DELETE",
            request_options=request_options,
        )
        if 200 <= _response.status_code < 300:
            return
        try:
            _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)
