# 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 .....core.remove_none_from_dict import remove_none_from_dict
from ....commons.errors.entity_not_found_error import EntityNotFoundError
from ....commons.errors.http_request_validation_error import HttpRequestValidationError
from ....commons.types.entity_not_found_error_message import EntityNotFoundErrorMessage
from ....commons.types.page_token import PageToken
from ....commons.types.request_validation_error import RequestValidationError
from .types.organization_service_facility import OrganizationServiceFacility
from .types.organization_service_facility_create import OrganizationServiceFacilityCreate
from .types.organization_service_facility_id import OrganizationServiceFacilityId
from .types.organization_service_facility_page import OrganizationServiceFacilityPage
from .types.organization_service_facility_update import OrganizationServiceFacilityUpdate

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 V2Client:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def get(self, organization_service_facility_id: OrganizationServiceFacilityId) -> OrganizationServiceFacility:
        """
        Parameters:
            - organization_service_facility_id: OrganizationServiceFacilityId.
        """
        _response = self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_base_url()}/",
                f"api/organization-service-facilities/v2/{organization_service_facility_id}",
            ),
            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(OrganizationServiceFacility, _response_json)  # type: ignore
        if "errorName" in _response_json:
            if _response_json["errorName"] == "EntityNotFoundError":
                raise EntityNotFoundError(
                    pydantic.parse_obj_as(EntityNotFoundErrorMessage, _response_json["content"])  # type: ignore
                )
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def get_multi(
        self,
        *,
        limit: typing.Optional[int] = None,
        name: typing.Optional[str] = None,
        page_token: typing.Optional[PageToken] = None,
    ) -> OrganizationServiceFacilityPage:
        """
        Parameters:
            - limit: typing.Optional[int]. Limit the number of results returned. Defaults to 100.

            - name: typing.Optional[str]. Filter to a name or a part of a name.

            - page_token: typing.Optional[PageToken]. The page token to continue paging through a previous request.
        ---
        from candid.client import CandidApi

        client = CandidApi(
            token="YOUR_TOKEN",
        )
        client.organization_service_facilities.v_2.get_multi(
            limit=100,
            name="Test Service Facility",
            page_token="eyJ0b2tlbiI6IjEiLCJwYWdlX3Rva2VuIjoiMiJ9",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "api/organization-service-facilities/v2"),
            params=remove_none_from_dict({"limit": limit, "name": name, "page_token": page_token}),
            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(OrganizationServiceFacilityPage, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def create(self, *, request: OrganizationServiceFacilityCreate) -> OrganizationServiceFacility:
        """
        Parameters:
            - request: OrganizationServiceFacilityCreate.
        ---
        from candid import State, StreetAddressLongZip
        from candid.client import CandidApi
        from candid.resources.organization_service_facilities.v_2 import (
            OrganizationServiceFacilityCreate,
            ServiceFacilityMode,
            ServiceFacilityOperationalStatus,
            ServiceFacilityPhysicalType,
            ServiceFacilityStatus,
            ServiceFacilityType,
        )

        client = CandidApi(
            token="YOUR_TOKEN",
        )
        client.organization_service_facilities.v_2.create(
            request=OrganizationServiceFacilityCreate(
                name="Test Service Facility",
                aliases=["Test Service Facility Alias"],
                description="Test Service Facility Description",
                status=ServiceFacilityStatus.ACTIVE,
                operational_status=ServiceFacilityOperationalStatus.C,
                mode=ServiceFacilityMode.INSTANCE,
                type=ServiceFacilityType.DX,
                physical_type=ServiceFacilityPhysicalType.SI,
                telecoms=["555-555-5555"],
                address=StreetAddressLongZip(
                    address_1="123 Main St",
                    address_2="Apt 1",
                    city="New York",
                    state=State.NY,
                    zip_code="10001",
                    zip_plus_four_code="1234",
                ),
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "POST",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "api/organization-service-facilities/v2"),
            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(OrganizationServiceFacility, _response_json)  # type: ignore
        if "errorName" in _response_json:
            if _response_json["errorName"] == "HttpRequestValidationError":
                raise HttpRequestValidationError(
                    pydantic.parse_obj_as(RequestValidationError, _response_json["content"])  # type: ignore
                )
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def update(
        self,
        organization_service_facility_id: OrganizationServiceFacilityId,
        *,
        request: OrganizationServiceFacilityUpdate,
    ) -> OrganizationServiceFacility:
        """
        Parameters:
            - organization_service_facility_id: OrganizationServiceFacilityId.

            - request: OrganizationServiceFacilityUpdate.
        ---
        import uuid

        from candid import State, StreetAddressLongZip
        from candid.client import CandidApi
        from candid.resources.organization_service_facilities.v_2 import (
            OrganizationServiceFacilityUpdate,
            ServiceFacilityMode,
            ServiceFacilityOperationalStatus,
            ServiceFacilityPhysicalType,
            ServiceFacilityStatus,
            ServiceFacilityType,
        )

        client = CandidApi(
            token="YOUR_TOKEN",
        )
        client.organization_service_facilities.v_2.update(
            organization_service_facility_id=uuid.UUID(
                "30f55ee6-8c0e-43fc-a7fc-dac00d5bf569",
            ),
            request=OrganizationServiceFacilityUpdate(
                name="Test Service Facility",
                aliases=["Test Service Facility Alias"],
                description="Test Service Facility Description",
                status=ServiceFacilityStatus.ACTIVE,
                operational_status=ServiceFacilityOperationalStatus.C,
                mode=ServiceFacilityMode.INSTANCE,
                type=ServiceFacilityType.DX,
                physical_type=ServiceFacilityPhysicalType.SI,
                telecoms=["555-555-5555"],
                address=StreetAddressLongZip(
                    address_1="123 Main St",
                    address_2="Apt 1",
                    city="New York",
                    state=State.NY,
                    zip_code="10001",
                    zip_plus_four_code="1234",
                ),
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "PATCH",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_base_url()}/",
                f"api/organization-service-facilities/v2/{organization_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(OrganizationServiceFacility, _response_json)  # type: ignore
        if "errorName" in _response_json:
            if _response_json["errorName"] == "HttpRequestValidationError":
                raise HttpRequestValidationError(
                    pydantic.parse_obj_as(RequestValidationError, _response_json["content"])  # type: ignore
                )
            if _response_json["errorName"] == "EntityNotFoundError":
                raise EntityNotFoundError(
                    pydantic.parse_obj_as(EntityNotFoundErrorMessage, _response_json["content"])  # type: ignore
                )
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def delete(self, organization_service_facility_id: OrganizationServiceFacilityId) -> None:
        """
        Parameters:
            - organization_service_facility_id: OrganizationServiceFacilityId.
        ---
        import uuid

        from candid.client import CandidApi

        client = CandidApi(
            token="YOUR_TOKEN",
        )
        client.organization_service_facilities.v_2.delete(
            organization_service_facility_id=uuid.UUID(
                "30f55ee6-8c0e-43fc-a7fc-dac00d5bf569",
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "DELETE",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_base_url()}/",
                f"api/organization-service-facilities/v2/{organization_service_facility_id}",
            ),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        if "errorName" in _response_json:
            if _response_json["errorName"] == "EntityNotFoundError":
                raise EntityNotFoundError(
                    pydantic.parse_obj_as(EntityNotFoundErrorMessage, _response_json["content"])  # type: ignore
                )
        raise ApiError(status_code=_response.status_code, body=_response_json)


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

    async def get(self, organization_service_facility_id: OrganizationServiceFacilityId) -> OrganizationServiceFacility:
        """
        Parameters:
            - organization_service_facility_id: OrganizationServiceFacilityId.
        """
        _response = await self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_base_url()}/",
                f"api/organization-service-facilities/v2/{organization_service_facility_id}",
            ),
            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(OrganizationServiceFacility, _response_json)  # type: ignore
        if "errorName" in _response_json:
            if _response_json["errorName"] == "EntityNotFoundError":
                raise EntityNotFoundError(
                    pydantic.parse_obj_as(EntityNotFoundErrorMessage, _response_json["content"])  # type: ignore
                )
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def get_multi(
        self,
        *,
        limit: typing.Optional[int] = None,
        name: typing.Optional[str] = None,
        page_token: typing.Optional[PageToken] = None,
    ) -> OrganizationServiceFacilityPage:
        """
        Parameters:
            - limit: typing.Optional[int]. Limit the number of results returned. Defaults to 100.

            - name: typing.Optional[str]. Filter to a name or a part of a name.

            - page_token: typing.Optional[PageToken]. The page token to continue paging through a previous request.
        ---
        from candid.client import AsyncCandidApi

        client = AsyncCandidApi(
            token="YOUR_TOKEN",
        )
        await client.organization_service_facilities.v_2.get_multi(
            limit=100,
            name="Test Service Facility",
            page_token="eyJ0b2tlbiI6IjEiLCJwYWdlX3Rva2VuIjoiMiJ9",
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "api/organization-service-facilities/v2"),
            params=remove_none_from_dict({"limit": limit, "name": name, "page_token": page_token}),
            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(OrganizationServiceFacilityPage, _response_json)  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def create(self, *, request: OrganizationServiceFacilityCreate) -> OrganizationServiceFacility:
        """
        Parameters:
            - request: OrganizationServiceFacilityCreate.
        ---
        from candid import State, StreetAddressLongZip
        from candid.client import AsyncCandidApi
        from candid.resources.organization_service_facilities.v_2 import (
            OrganizationServiceFacilityCreate,
            ServiceFacilityMode,
            ServiceFacilityOperationalStatus,
            ServiceFacilityPhysicalType,
            ServiceFacilityStatus,
            ServiceFacilityType,
        )

        client = AsyncCandidApi(
            token="YOUR_TOKEN",
        )
        await client.organization_service_facilities.v_2.create(
            request=OrganizationServiceFacilityCreate(
                name="Test Service Facility",
                aliases=["Test Service Facility Alias"],
                description="Test Service Facility Description",
                status=ServiceFacilityStatus.ACTIVE,
                operational_status=ServiceFacilityOperationalStatus.C,
                mode=ServiceFacilityMode.INSTANCE,
                type=ServiceFacilityType.DX,
                physical_type=ServiceFacilityPhysicalType.SI,
                telecoms=["555-555-5555"],
                address=StreetAddressLongZip(
                    address_1="123 Main St",
                    address_2="Apt 1",
                    city="New York",
                    state=State.NY,
                    zip_code="10001",
                    zip_plus_four_code="1234",
                ),
            ),
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "POST",
            urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "api/organization-service-facilities/v2"),
            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(OrganizationServiceFacility, _response_json)  # type: ignore
        if "errorName" in _response_json:
            if _response_json["errorName"] == "HttpRequestValidationError":
                raise HttpRequestValidationError(
                    pydantic.parse_obj_as(RequestValidationError, _response_json["content"])  # type: ignore
                )
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def update(
        self,
        organization_service_facility_id: OrganizationServiceFacilityId,
        *,
        request: OrganizationServiceFacilityUpdate,
    ) -> OrganizationServiceFacility:
        """
        Parameters:
            - organization_service_facility_id: OrganizationServiceFacilityId.

            - request: OrganizationServiceFacilityUpdate.
        ---
        import uuid

        from candid import State, StreetAddressLongZip
        from candid.client import AsyncCandidApi
        from candid.resources.organization_service_facilities.v_2 import (
            OrganizationServiceFacilityUpdate,
            ServiceFacilityMode,
            ServiceFacilityOperationalStatus,
            ServiceFacilityPhysicalType,
            ServiceFacilityStatus,
            ServiceFacilityType,
        )

        client = AsyncCandidApi(
            token="YOUR_TOKEN",
        )
        await client.organization_service_facilities.v_2.update(
            organization_service_facility_id=uuid.UUID(
                "30f55ee6-8c0e-43fc-a7fc-dac00d5bf569",
            ),
            request=OrganizationServiceFacilityUpdate(
                name="Test Service Facility",
                aliases=["Test Service Facility Alias"],
                description="Test Service Facility Description",
                status=ServiceFacilityStatus.ACTIVE,
                operational_status=ServiceFacilityOperationalStatus.C,
                mode=ServiceFacilityMode.INSTANCE,
                type=ServiceFacilityType.DX,
                physical_type=ServiceFacilityPhysicalType.SI,
                telecoms=["555-555-5555"],
                address=StreetAddressLongZip(
                    address_1="123 Main St",
                    address_2="Apt 1",
                    city="New York",
                    state=State.NY,
                    zip_code="10001",
                    zip_plus_four_code="1234",
                ),
            ),
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "PATCH",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_base_url()}/",
                f"api/organization-service-facilities/v2/{organization_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(OrganizationServiceFacility, _response_json)  # type: ignore
        if "errorName" in _response_json:
            if _response_json["errorName"] == "HttpRequestValidationError":
                raise HttpRequestValidationError(
                    pydantic.parse_obj_as(RequestValidationError, _response_json["content"])  # type: ignore
                )
            if _response_json["errorName"] == "EntityNotFoundError":
                raise EntityNotFoundError(
                    pydantic.parse_obj_as(EntityNotFoundErrorMessage, _response_json["content"])  # type: ignore
                )
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def delete(self, organization_service_facility_id: OrganizationServiceFacilityId) -> None:
        """
        Parameters:
            - organization_service_facility_id: OrganizationServiceFacilityId.
        ---
        import uuid

        from candid.client import AsyncCandidApi

        client = AsyncCandidApi(
            token="YOUR_TOKEN",
        )
        await client.organization_service_facilities.v_2.delete(
            organization_service_facility_id=uuid.UUID(
                "30f55ee6-8c0e-43fc-a7fc-dac00d5bf569",
            ),
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "DELETE",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_base_url()}/",
                f"api/organization-service-facilities/v2/{organization_service_facility_id}",
            ),
            headers=self._client_wrapper.get_headers(),
            timeout=60,
        )
        if 200 <= _response.status_code < 300:
            return
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        if "errorName" in _response_json:
            if _response_json["errorName"] == "EntityNotFoundError":
                raise EntityNotFoundError(
                    pydantic.parse_obj_as(EntityNotFoundErrorMessage, _response_json["content"])  # type: ignore
                )
        raise ApiError(status_code=_response.status_code, body=_response_json)
