# 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.http_response import AsyncHttpResponse, HttpResponse
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..types.detailed_health_check import DetailedHealthCheck
from ..types.health_check import HealthCheck


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

    def liveness_check(self, *, request_options: typing.Optional[RequestOptions] = None) -> HttpResponse[HealthCheck]:
        """
        Simple liveness check endpoint.

        Returns a basic status indicating the application is running.
        Useful for container orchestration liveness probes.

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

        Returns
        -------
        HttpResponse[HealthCheck]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/health/",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    HealthCheck,
                    parse_obj_as(
                        type_=HealthCheck,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def readiness_check(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[DetailedHealthCheck]:
        """
        Detailed readiness check endpoint.

        Checks the health of all application dependencies including:
        - Database connectivity
        - Cache connectivity
        - Limit enforcement system status
        - Vector store connectivity

        Useful for container orchestration readiness probes.

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

        Returns
        -------
        HttpResponse[DetailedHealthCheck]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/health/details",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DetailedHealthCheck,
                    parse_obj_as(
                        type_=DetailedHealthCheck,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def metrics(self, *, request_options: typing.Optional[RequestOptions] = None) -> HttpResponse[None]:
        """
        Expose Prometheus metrics.

        Returns metrics in Prometheus text format including:
        - transcriptions_created_total{model}: Total transcriptions created
        - synapses_consumed_total{model}: Total synapses consumed
        - synapses_consumed_per_second{model}: Current synapse consumption rate
        - pricing_lookup_latency_ms{model, source}: Pricing lookup latency histogram
        - pricing_errors_total{code}: Total pricing lookup errors

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

        Returns
        -------
        HttpResponse[None]
        """
        _response = self._client_wrapper.httpx_client.request(
            "health/metrics",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return HttpResponse(response=_response, data=None)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


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

    async def liveness_check(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[HealthCheck]:
        """
        Simple liveness check endpoint.

        Returns a basic status indicating the application is running.
        Useful for container orchestration liveness probes.

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

        Returns
        -------
        AsyncHttpResponse[HealthCheck]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/health/",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    HealthCheck,
                    parse_obj_as(
                        type_=HealthCheck,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def readiness_check(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[DetailedHealthCheck]:
        """
        Detailed readiness check endpoint.

        Checks the health of all application dependencies including:
        - Database connectivity
        - Cache connectivity
        - Limit enforcement system status
        - Vector store connectivity

        Useful for container orchestration readiness probes.

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

        Returns
        -------
        AsyncHttpResponse[DetailedHealthCheck]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/health/details",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DetailedHealthCheck,
                    parse_obj_as(
                        type_=DetailedHealthCheck,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def metrics(self, *, request_options: typing.Optional[RequestOptions] = None) -> AsyncHttpResponse[None]:
        """
        Expose Prometheus metrics.

        Returns metrics in Prometheus text format including:
        - transcriptions_created_total{model}: Total transcriptions created
        - synapses_consumed_total{model}: Total synapses consumed
        - synapses_consumed_per_second{model}: Current synapse consumption rate
        - pricing_lookup_latency_ms{model, source}: Pricing lookup latency histogram
        - pricing_errors_total{code}: Total pricing lookup errors

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

        Returns
        -------
        AsyncHttpResponse[None]
        """
        _response = await self._client_wrapper.httpx_client.request(
            "health/metrics",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return AsyncHttpResponse(response=_response, data=None)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
