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

import typing
from ...core.client_wrapper import SyncClientWrapper
from ...core.request_options import RequestOptions
from ...core.pydantic_utilities import parse_obj_as
from json.decoder import JSONDecodeError
from ...core.api_error import ApiError
from ...types.variable_requirement import VariableRequirement
from ...types.equation_validation_result import EquationValidationResult
from ...core.serialization import convert_and_respect_annotation_metadata
from ...errors.unprocessable_entity_error import UnprocessableEntityError
from ...types.http_validation_error import HttpValidationError
from ... import core
from ...types.equation_extraction_response import EquationExtractionResponse
from ...types.equation_processing_response import EquationProcessingResponse
from ...core.client_wrapper import AsyncClientWrapper

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


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

    def user_variables(self, *, request_options: typing.Optional[RequestOptions] = None) -> typing.Dict[str, str]:
        """
        Get all variables from the DB so the user can choose which variables they want to use in axtract for for their consistency checks.

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

        Returns
        -------
        typing.Dict[str, str]
            Successful Response

        Examples
        --------
        from axiomatic import Axiomatic

        client = Axiomatic(
            api_key="YOUR_API_KEY",
        )
        client.document.equation.user_variables()
        """
        _response = self._client_wrapper.httpx_client.request(
            "document/equation/user-variables",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.Dict[str, str],
                    parse_obj_as(
                        type_=typing.Dict[str, str],  # 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 validate(
        self, *, request: typing.Sequence[VariableRequirement], request_options: typing.Optional[RequestOptions] = None
    ) -> EquationValidationResult:
        """
        Validates a set of variables against stored equations to check for inconsistencies.
        Returns validation results for each relevant equation.

        Parameters
        ----------
        request : typing.Sequence[VariableRequirement]

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

        Returns
        -------
        EquationValidationResult
            Successful Response

        Examples
        --------
        from axiomatic import Axiomatic, VariableRequirement

        client = Axiomatic(
            api_key="YOUR_API_KEY",
        )
        client.document.equation.validate(
            request=[
                VariableRequirement(
                    symbol="symbol",
                    name="name",
                    value=1.1,
                    units="units",
                    tolerance=1.1,
                )
            ],
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "document/equation/validate",
            method="POST",
            json=convert_and_respect_annotation_metadata(
                object_=request, annotation=typing.Sequence[VariableRequirement], direction="write"
            ),
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    EquationValidationResult,
                    parse_obj_as(
                        type_=EquationValidationResult,  # 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 from_pdf(
        self, *, document: core.File, request_options: typing.Optional[RequestOptions] = None
    ) -> EquationExtractionResponse:
        """
        Extract equations from a document into the database

        Parameters
        ----------
        document : core.File
            See core.File for more documentation

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

        Returns
        -------
        EquationExtractionResponse
            Successful Response

        Examples
        --------
        from axiomatic import Axiomatic

        client = Axiomatic(
            api_key="YOUR_API_KEY",
        )
        client.document.equation.from_pdf()
        """
        _response = self._client_wrapper.httpx_client.request(
            "document/equation/from-document",
            method="POST",
            data={},
            files={
                "document": document,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    EquationExtractionResponse,
                    parse_obj_as(
                        type_=EquationExtractionResponse,  # 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 process(
        self,
        *,
        markdown: str,
        images: typing.Optional[typing.Dict[str, str]] = OMIT,
        interline_equations: typing.Optional[typing.Sequence[str]] = OMIT,
        inline_equations: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> EquationProcessingResponse:
        """
        Process all equations at once and return their annotation

        Parameters
        ----------
        markdown : str

        images : typing.Optional[typing.Dict[str, str]]

        interline_equations : typing.Optional[typing.Sequence[str]]

        inline_equations : typing.Optional[typing.Sequence[str]]

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

        Returns
        -------
        EquationProcessingResponse
            Successful Response

        Examples
        --------
        from axiomatic import Axiomatic

        client = Axiomatic(
            api_key="YOUR_API_KEY",
        )
        client.document.equation.process(
            markdown="markdown",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "document/equation/process",
            method="POST",
            json={
                "markdown": markdown,
                "images": images,
                "interline_equations": interline_equations,
                "inline_equations": inline_equations,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    EquationProcessingResponse,
                    parse_obj_as(
                        type_=EquationProcessingResponse,  # 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 AsyncEquationClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def user_variables(self, *, request_options: typing.Optional[RequestOptions] = None) -> typing.Dict[str, str]:
        """
        Get all variables from the DB so the user can choose which variables they want to use in axtract for for their consistency checks.

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

        Returns
        -------
        typing.Dict[str, str]
            Successful Response

        Examples
        --------
        import asyncio

        from axiomatic import AsyncAxiomatic

        client = AsyncAxiomatic(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.document.equation.user_variables()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "document/equation/user-variables",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.Dict[str, str],
                    parse_obj_as(
                        type_=typing.Dict[str, str],  # 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 validate(
        self, *, request: typing.Sequence[VariableRequirement], request_options: typing.Optional[RequestOptions] = None
    ) -> EquationValidationResult:
        """
        Validates a set of variables against stored equations to check for inconsistencies.
        Returns validation results for each relevant equation.

        Parameters
        ----------
        request : typing.Sequence[VariableRequirement]

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

        Returns
        -------
        EquationValidationResult
            Successful Response

        Examples
        --------
        import asyncio

        from axiomatic import AsyncAxiomatic, VariableRequirement

        client = AsyncAxiomatic(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.document.equation.validate(
                request=[
                    VariableRequirement(
                        symbol="symbol",
                        name="name",
                        value=1.1,
                        units="units",
                        tolerance=1.1,
                    )
                ],
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "document/equation/validate",
            method="POST",
            json=convert_and_respect_annotation_metadata(
                object_=request, annotation=typing.Sequence[VariableRequirement], direction="write"
            ),
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    EquationValidationResult,
                    parse_obj_as(
                        type_=EquationValidationResult,  # 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 from_pdf(
        self, *, document: core.File, request_options: typing.Optional[RequestOptions] = None
    ) -> EquationExtractionResponse:
        """
        Extract equations from a document into the database

        Parameters
        ----------
        document : core.File
            See core.File for more documentation

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

        Returns
        -------
        EquationExtractionResponse
            Successful Response

        Examples
        --------
        import asyncio

        from axiomatic import AsyncAxiomatic

        client = AsyncAxiomatic(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.document.equation.from_pdf()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "document/equation/from-document",
            method="POST",
            data={},
            files={
                "document": document,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    EquationExtractionResponse,
                    parse_obj_as(
                        type_=EquationExtractionResponse,  # 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 process(
        self,
        *,
        markdown: str,
        images: typing.Optional[typing.Dict[str, str]] = OMIT,
        interline_equations: typing.Optional[typing.Sequence[str]] = OMIT,
        inline_equations: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> EquationProcessingResponse:
        """
        Process all equations at once and return their annotation

        Parameters
        ----------
        markdown : str

        images : typing.Optional[typing.Dict[str, str]]

        interline_equations : typing.Optional[typing.Sequence[str]]

        inline_equations : typing.Optional[typing.Sequence[str]]

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

        Returns
        -------
        EquationProcessingResponse
            Successful Response

        Examples
        --------
        import asyncio

        from axiomatic import AsyncAxiomatic

        client = AsyncAxiomatic(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.document.equation.process(
                markdown="markdown",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "document/equation/process",
            method="POST",
            json={
                "markdown": markdown,
                "images": images,
                "interline_equations": interline_equations,
                "inline_equations": inline_equations,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    EquationProcessingResponse,
                    parse_obj_as(
                        type_=EquationProcessingResponse,  # 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)
