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

import typing
from ..core.client_wrapper import SyncClientWrapper
from ..types.patient_details import PatientDetails
from ..types.patient_address_compatible import PatientAddressCompatible
from ..types.physician_create_request_base import PhysicianCreateRequestBase
from ..types.health_insurance_create_request import HealthInsuranceCreateRequest
from ..types.consent import Consent
from ..core.request_options import RequestOptions
from ..types.post_order_response import PostOrderResponse
from ..core.pydantic_utilities import parse_obj_as
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.http_validation_error import HttpValidationError
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..types.shipping_address import ShippingAddress
from ..core.client_wrapper import AsyncClientWrapper

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


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

    def register(
        self,
        *,
        user_id: str,
        sample_id: str,
        patient_details: PatientDetails,
        patient_address: PatientAddressCompatible,
        physician: typing.Optional[PhysicianCreateRequestBase] = OMIT,
        health_insurance: typing.Optional[HealthInsuranceCreateRequest] = OMIT,
        consents: typing.Optional[typing.Sequence[Consent]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PostOrderResponse:
        """
        Parameters
        ----------
        user_id : str

        sample_id : str

        patient_details : PatientDetails

        patient_address : PatientAddressCompatible

        physician : typing.Optional[PhysicianCreateRequestBase]

        health_insurance : typing.Optional[HealthInsuranceCreateRequest]

        consents : typing.Optional[typing.Sequence[Consent]]

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

        Returns
        -------
        PostOrderResponse
            Successful Response

        Examples
        --------
        import datetime

        from vital import Gender, PatientAddressCompatible, PatientDetails, Vital

        client = Vital(
            api_key="YOUR_API_KEY",
        )
        client.testkit.register(
            user_id="user_id",
            sample_id="sample_id",
            patient_details=PatientDetails(
                first_name="first_name",
                last_name="last_name",
                dob=datetime.datetime.fromisoformat(
                    "2024-01-15 09:30:00+00:00",
                ),
                gender=Gender.FEMALE,
                phone_number="phone_number",
                email="email",
            ),
            patient_address=PatientAddressCompatible(
                first_line="first_line",
                city="city",
                state="state",
                zip="zip",
                country="country",
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "v3/order/testkit/register",
            method="POST",
            json={
                "user_id": user_id,
                "sample_id": sample_id,
                "patient_details": patient_details,
                "patient_address": patient_address,
                "physician": physician,
                "health_insurance": health_insurance,
                "consents": consents,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    PostOrderResponse,
                    parse_obj_as(
                        type_=PostOrderResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _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)

    def create_order(
        self,
        *,
        user_id: str,
        lab_test_id: str,
        shipping_details: ShippingAddress,
        passthrough: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PostOrderResponse:
        """
        Creates an order for an unregistered testkit

        Parameters
        ----------
        user_id : str

        lab_test_id : str

        shipping_details : ShippingAddress

        passthrough : typing.Optional[str]

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

        Returns
        -------
        PostOrderResponse
            Successful Response

        Examples
        --------
        from vital import ShippingAddress, Vital

        client = Vital(
            api_key="YOUR_API_KEY",
        )
        client.testkit.create_order(
            user_id="user_id",
            lab_test_id="lab_test_id",
            shipping_details=ShippingAddress(
                receiver_name="receiver_name",
                first_line="first_line",
                city="city",
                state="state",
                zip="zip",
                country="country",
                phone_number="phone_number",
            ),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "v3/order/testkit",
            method="POST",
            json={
                "user_id": user_id,
                "lab_test_id": lab_test_id,
                "shipping_details": shipping_details,
                "passthrough": passthrough,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    PostOrderResponse,
                    parse_obj_as(
                        type_=PostOrderResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _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 AsyncTestkitClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def register(
        self,
        *,
        user_id: str,
        sample_id: str,
        patient_details: PatientDetails,
        patient_address: PatientAddressCompatible,
        physician: typing.Optional[PhysicianCreateRequestBase] = OMIT,
        health_insurance: typing.Optional[HealthInsuranceCreateRequest] = OMIT,
        consents: typing.Optional[typing.Sequence[Consent]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PostOrderResponse:
        """
        Parameters
        ----------
        user_id : str

        sample_id : str

        patient_details : PatientDetails

        patient_address : PatientAddressCompatible

        physician : typing.Optional[PhysicianCreateRequestBase]

        health_insurance : typing.Optional[HealthInsuranceCreateRequest]

        consents : typing.Optional[typing.Sequence[Consent]]

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

        Returns
        -------
        PostOrderResponse
            Successful Response

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

        from vital import AsyncVital, Gender, PatientAddressCompatible, PatientDetails

        client = AsyncVital(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.testkit.register(
                user_id="user_id",
                sample_id="sample_id",
                patient_details=PatientDetails(
                    first_name="first_name",
                    last_name="last_name",
                    dob=datetime.datetime.fromisoformat(
                        "2024-01-15 09:30:00+00:00",
                    ),
                    gender=Gender.FEMALE,
                    phone_number="phone_number",
                    email="email",
                ),
                patient_address=PatientAddressCompatible(
                    first_line="first_line",
                    city="city",
                    state="state",
                    zip="zip",
                    country="country",
                ),
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v3/order/testkit/register",
            method="POST",
            json={
                "user_id": user_id,
                "sample_id": sample_id,
                "patient_details": patient_details,
                "patient_address": patient_address,
                "physician": physician,
                "health_insurance": health_insurance,
                "consents": consents,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    PostOrderResponse,
                    parse_obj_as(
                        type_=PostOrderResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _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)

    async def create_order(
        self,
        *,
        user_id: str,
        lab_test_id: str,
        shipping_details: ShippingAddress,
        passthrough: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PostOrderResponse:
        """
        Creates an order for an unregistered testkit

        Parameters
        ----------
        user_id : str

        lab_test_id : str

        shipping_details : ShippingAddress

        passthrough : typing.Optional[str]

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

        Returns
        -------
        PostOrderResponse
            Successful Response

        Examples
        --------
        import asyncio

        from vital import AsyncVital, ShippingAddress

        client = AsyncVital(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.testkit.create_order(
                user_id="user_id",
                lab_test_id="lab_test_id",
                shipping_details=ShippingAddress(
                    receiver_name="receiver_name",
                    first_line="first_line",
                    city="city",
                    state="state",
                    zip="zip",
                    country="country",
                    phone_number="phone_number",
                ),
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v3/order/testkit",
            method="POST",
            json={
                "user_id": user_id,
                "lab_test_id": lab_test_id,
                "shipping_details": shipping_details,
                "passthrough": passthrough,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    PostOrderResponse,
                    parse_obj_as(
                        type_=PostOrderResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _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)
