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

import typing
import urllib.parse
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 ..commons.types.street_address_long_zip import StreetAddressLongZip
from .types.encounter_service_facility import EncounterServiceFacility
from .types.service_facility_id import ServiceFacilityId

try:
    import pydantic.v1 as pydantic  # type: ignore
except ImportError:
    import pydantic  # type: ignore

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


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

    def update(
        self,
        service_facility_id: ServiceFacilityId,
        *,
        organization_name: typing.Optional[str] = OMIT,
        npi: typing.Optional[str] = OMIT,
        address: typing.Optional[StreetAddressLongZip] = OMIT,
    ) -> EncounterServiceFacility:
        """
        Parameters:
            - service_facility_id: ServiceFacilityId.

            - organization_name: typing.Optional[str].

            - npi: typing.Optional[str]. An NPI specific to the service facility if applicable, i.e. if it has one and is not under the billing provider's NPI.
                                         Box 32 section (a) of the CMS-1500 claim form.

            - address: typing.Optional[StreetAddressLongZip]. zip_plus_four_code is required for service facility address. When the zip_plus_four_code is not available use "9998" as per CMS documentation.

        """
        _request: typing.Dict[str, typing.Any] = {}
        if organization_name is not OMIT:
            _request["organization_name"] = organization_name
        if npi is not OMIT:
            _request["npi"] = npi
        if address is not OMIT:
            _request["address"] = address
        _response = self._client_wrapper.httpx_client.request(
            "PATCH",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_base_url()}/", f"api/service_facility/v2/{service_facility_id}"
            ),
            json=jsonable_encoder(_request),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        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.parse_obj_as(EncounterServiceFacility, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)


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

    async def update(
        self,
        service_facility_id: ServiceFacilityId,
        *,
        organization_name: typing.Optional[str] = OMIT,
        npi: typing.Optional[str] = OMIT,
        address: typing.Optional[StreetAddressLongZip] = OMIT,
    ) -> EncounterServiceFacility:
        """
        Parameters:
            - service_facility_id: ServiceFacilityId.

            - organization_name: typing.Optional[str].

            - npi: typing.Optional[str]. An NPI specific to the service facility if applicable, i.e. if it has one and is not under the billing provider's NPI.
                                         Box 32 section (a) of the CMS-1500 claim form.

            - address: typing.Optional[StreetAddressLongZip]. zip_plus_four_code is required for service facility address. When the zip_plus_four_code is not available use "9998" as per CMS documentation.

        """
        _request: typing.Dict[str, typing.Any] = {}
        if organization_name is not OMIT:
            _request["organization_name"] = organization_name
        if npi is not OMIT:
            _request["npi"] = npi
        if address is not OMIT:
            _request["address"] = address
        _response = await self._client_wrapper.httpx_client.request(
            "PATCH",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_base_url()}/", f"api/service_facility/v2/{service_facility_id}"
            ),
            json=jsonable_encoder(_request),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        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.parse_obj_as(EncounterServiceFacility, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)
