# 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.errors.entity_not_found_error import EntityNotFoundError
from ....commons.types.entity_not_found_error_message import EntityNotFoundErrorMessage
from ....commons.types.invoice_id import InvoiceId
from .types.create_import_invoice_request import CreateImportInvoiceRequest
from .types.import_invoice import ImportInvoice
from .types.import_invoice_update_request import ImportInvoiceUpdateRequest

# 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 import_invoice(
        self, *, request: CreateImportInvoiceRequest, request_options: typing.Optional[RequestOptions] = None
    ) -> ImportInvoice:
        """
        Import an existing invoice from a third party service to reflect state in Candid.

        Parameters
        ----------
        request : CreateImportInvoiceRequest

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

        Returns
        -------
        ImportInvoice

        Examples
        --------
        import datetime

        from candid.client import CandidApiClient
        from candid.resources.import_invoice.v_1 import CreateImportInvoiceRequest
        from candid.resources.invoices.v_2 import InvoiceItemCreate, InvoiceStatus

        client = CandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        client.import_invoice.v_1.import_invoice(
            request=CreateImportInvoiceRequest(
                patient_external_id="string",
                external_customer_identifier="string",
                note="string",
                due_date=datetime.date.fromisoformat(
                    "2023-01-15",
                ),
                items=[InvoiceItemCreate()],
                status=InvoiceStatus.DRAFT,
                external_identifier="string",
                customer_invoice_url="string",
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/import-invoice/v1", 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(ImportInvoice, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def get(self, invoice_id: InvoiceId, *, request_options: typing.Optional[RequestOptions] = None) -> ImportInvoice:
        """
        Retrieve and view an import invoice

        Parameters
        ----------
        invoice_id : InvoiceId
            InvoiceId to be returned

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

        Returns
        -------
        ImportInvoice

        Examples
        --------
        import uuid

        from candid.client import CandidApiClient

        client = CandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        client.import_invoice.v_1.get(
            invoice_id=uuid.UUID(
                "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/import-invoice/v1/{jsonable_encoder(invoice_id)}", 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(ImportInvoice, _response_json)  # type: ignore
        if "errorName" in _response_json:
            if _response_json["errorName"] == "EntityNotFoundError":
                raise EntityNotFoundError(
                    pydantic_v1.parse_obj_as(EntityNotFoundErrorMessage, _response_json["content"])  # type: ignore
                )
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def update(
        self,
        invoice_id: InvoiceId,
        *,
        request: ImportInvoiceUpdateRequest,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ImportInvoice:
        """
        Update the information on the imported invoice

        Parameters
        ----------
        invoice_id : InvoiceId

        request : ImportInvoiceUpdateRequest

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

        Returns
        -------
        ImportInvoice

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

        from candid.client import CandidApiClient
        from candid.resources.import_invoice.v_1 import (
            ImportInvoiceUpdateRequest,
            InvoiceItemInfoUpdate,
            InvoiceItemUpdateType,
        )
        from candid.resources.invoices.v_2 import InvoiceItemCreate, InvoiceStatus

        client = CandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        client.import_invoice.v_1.update(
            invoice_id=uuid.UUID(
                "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
            ),
            request=ImportInvoiceUpdateRequest(
                customer_invoice_url="string",
                status=InvoiceStatus.DRAFT,
                note="string",
                due_date=datetime.date.fromisoformat(
                    "2023-01-15",
                ),
                items=InvoiceItemInfoUpdate(
                    update_type=InvoiceItemUpdateType.APPEND,
                    items=[InvoiceItemCreate()],
                ),
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/import-invoice/v1/{jsonable_encoder(invoice_id)}",
            method="PATCH",
            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(ImportInvoice, _response_json)  # type: ignore
        if "errorName" in _response_json:
            if _response_json["errorName"] == "EntityNotFoundError":
                raise EntityNotFoundError(
                    pydantic_v1.parse_obj_as(EntityNotFoundErrorMessage, _response_json["content"])  # 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 import_invoice(
        self, *, request: CreateImportInvoiceRequest, request_options: typing.Optional[RequestOptions] = None
    ) -> ImportInvoice:
        """
        Import an existing invoice from a third party service to reflect state in Candid.

        Parameters
        ----------
        request : CreateImportInvoiceRequest

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

        Returns
        -------
        ImportInvoice

        Examples
        --------
        import datetime

        from candid.client import AsyncCandidApiClient
        from candid.resources.import_invoice.v_1 import CreateImportInvoiceRequest
        from candid.resources.invoices.v_2 import InvoiceItemCreate, InvoiceStatus

        client = AsyncCandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        await client.import_invoice.v_1.import_invoice(
            request=CreateImportInvoiceRequest(
                patient_external_id="string",
                external_customer_identifier="string",
                note="string",
                due_date=datetime.date.fromisoformat(
                    "2023-01-15",
                ),
                items=[InvoiceItemCreate()],
                status=InvoiceStatus.DRAFT,
                external_identifier="string",
                customer_invoice_url="string",
            ),
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/import-invoice/v1", 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(ImportInvoice, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def get(
        self, invoice_id: InvoiceId, *, request_options: typing.Optional[RequestOptions] = None
    ) -> ImportInvoice:
        """
        Retrieve and view an import invoice

        Parameters
        ----------
        invoice_id : InvoiceId
            InvoiceId to be returned

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

        Returns
        -------
        ImportInvoice

        Examples
        --------
        import uuid

        from candid.client import AsyncCandidApiClient

        client = AsyncCandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        await client.import_invoice.v_1.get(
            invoice_id=uuid.UUID(
                "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
            ),
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/import-invoice/v1/{jsonable_encoder(invoice_id)}", 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(ImportInvoice, _response_json)  # type: ignore
        if "errorName" in _response_json:
            if _response_json["errorName"] == "EntityNotFoundError":
                raise EntityNotFoundError(
                    pydantic_v1.parse_obj_as(EntityNotFoundErrorMessage, _response_json["content"])  # type: ignore
                )
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def update(
        self,
        invoice_id: InvoiceId,
        *,
        request: ImportInvoiceUpdateRequest,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ImportInvoice:
        """
        Update the information on the imported invoice

        Parameters
        ----------
        invoice_id : InvoiceId

        request : ImportInvoiceUpdateRequest

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

        Returns
        -------
        ImportInvoice

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

        from candid.client import AsyncCandidApiClient
        from candid.resources.import_invoice.v_1 import (
            ImportInvoiceUpdateRequest,
            InvoiceItemInfoUpdate,
            InvoiceItemUpdateType,
        )
        from candid.resources.invoices.v_2 import InvoiceItemCreate, InvoiceStatus

        client = AsyncCandidApiClient(
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        await client.import_invoice.v_1.update(
            invoice_id=uuid.UUID(
                "d5e9c84f-c2b2-4bf4-b4b0-7ffd7a9ffc32",
            ),
            request=ImportInvoiceUpdateRequest(
                customer_invoice_url="string",
                status=InvoiceStatus.DRAFT,
                note="string",
                due_date=datetime.date.fromisoformat(
                    "2023-01-15",
                ),
                items=InvoiceItemInfoUpdate(
                    update_type=InvoiceItemUpdateType.APPEND,
                    items=[InvoiceItemCreate()],
                ),
            ),
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/import-invoice/v1/{jsonable_encoder(invoice_id)}",
            method="PATCH",
            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(ImportInvoice, _response_json)  # type: ignore
        if "errorName" in _response_json:
            if _response_json["errorName"] == "EntityNotFoundError":
                raise EntityNotFoundError(
                    pydantic_v1.parse_obj_as(EntityNotFoundErrorMessage, _response_json["content"])  # type: ignore
                )
        raise ApiError(status_code=_response.status_code, body=_response_json)
