# 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.jsonable_encoder import jsonable_encoder
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..errors.bad_request_error import BadRequestError
from ..errors.internal_server_error import InternalServerError
from ..errors.service_unavailable_error import ServiceUnavailableError
from ..errors.unauthorized_error import UnauthorizedError
from ..types.file_content_ftype import FileContentFtype
from ..types.payabli_api_response import PayabliApiResponse
from .types.payabli_api_response_ocr import PayabliApiResponseOcr
from .types.type_result import TypeResult

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


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

    def ocr_document_form(
        self,
        type_result: TypeResult,
        *,
        ftype: typing.Optional[FileContentFtype] = OMIT,
        filename: typing.Optional[str] = OMIT,
        furl: typing.Optional[str] = OMIT,
        f_content: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[PayabliApiResponseOcr]:
        """
        Use this endpoint to upload an image file for OCR processing. The accepted file formats include PDF, JPG, JPEG, PNG, and GIF. Specify the desired type of result (either 'bill' or 'invoice') in the path parameter `typeResult`. The response will contain the OCR processing results, including extracted data such as bill number, vendor information, bill items, and more.

        Parameters
        ----------
        type_result : TypeResult

        ftype : typing.Optional[FileContentFtype]

        filename : typing.Optional[str]
            The name of the file to be uploaded

        furl : typing.Optional[str]
            Optional URL link to the file

        f_content : typing.Optional[str]
            Base64-encoded file content

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

        Returns
        -------
        HttpResponse[PayabliApiResponseOcr]
            OCR document processing result
        """
        _response = self._client_wrapper.httpx_client.request(
            f"Import/ocrDocumentForm/{jsonable_encoder(type_result)}",
            method="POST",
            json={
                "ftype": ftype,
                "filename": filename,
                "furl": furl,
                "fContent": f_content,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PayabliApiResponseOcr,
                    parse_obj_as(
                        type_=PayabliApiResponseOcr,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 503:
                raise ServiceUnavailableError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        PayabliApiResponse,
                        parse_obj_as(
                            type_=PayabliApiResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _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 ocr_document_json(
        self,
        type_result: TypeResult,
        *,
        ftype: typing.Optional[FileContentFtype] = OMIT,
        filename: typing.Optional[str] = OMIT,
        furl: typing.Optional[str] = OMIT,
        f_content: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[PayabliApiResponseOcr]:
        """
        Use this endpoint to submit a Base64-encoded image file for OCR processing. The accepted file formats include PDF, JPG, JPEG, PNG, and GIF. Specify the desired type of result (either 'bill' or 'invoice') in the path parameter `typeResult`. The response will contain the OCR processing results, including extracted data such as bill number, vendor information, bill items, and more.

        Parameters
        ----------
        type_result : TypeResult

        ftype : typing.Optional[FileContentFtype]

        filename : typing.Optional[str]
            The name of the file to be uploaded

        furl : typing.Optional[str]
            Optional URL link to the file

        f_content : typing.Optional[str]
            Base64-encoded file content

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

        Returns
        -------
        HttpResponse[PayabliApiResponseOcr]
            OCR document processing result
        """
        _response = self._client_wrapper.httpx_client.request(
            f"Import/ocrDocumentJson/{jsonable_encoder(type_result)}",
            method="POST",
            json={
                "ftype": ftype,
                "filename": filename,
                "furl": furl,
                "fContent": f_content,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PayabliApiResponseOcr,
                    parse_obj_as(
                        type_=PayabliApiResponseOcr,  # 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)


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

    async def ocr_document_form(
        self,
        type_result: TypeResult,
        *,
        ftype: typing.Optional[FileContentFtype] = OMIT,
        filename: typing.Optional[str] = OMIT,
        furl: typing.Optional[str] = OMIT,
        f_content: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[PayabliApiResponseOcr]:
        """
        Use this endpoint to upload an image file for OCR processing. The accepted file formats include PDF, JPG, JPEG, PNG, and GIF. Specify the desired type of result (either 'bill' or 'invoice') in the path parameter `typeResult`. The response will contain the OCR processing results, including extracted data such as bill number, vendor information, bill items, and more.

        Parameters
        ----------
        type_result : TypeResult

        ftype : typing.Optional[FileContentFtype]

        filename : typing.Optional[str]
            The name of the file to be uploaded

        furl : typing.Optional[str]
            Optional URL link to the file

        f_content : typing.Optional[str]
            Base64-encoded file content

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

        Returns
        -------
        AsyncHttpResponse[PayabliApiResponseOcr]
            OCR document processing result
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"Import/ocrDocumentForm/{jsonable_encoder(type_result)}",
            method="POST",
            json={
                "ftype": ftype,
                "filename": filename,
                "furl": furl,
                "fContent": f_content,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PayabliApiResponseOcr,
                    parse_obj_as(
                        type_=PayabliApiResponseOcr,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 503:
                raise ServiceUnavailableError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        PayabliApiResponse,
                        parse_obj_as(
                            type_=PayabliApiResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _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 ocr_document_json(
        self,
        type_result: TypeResult,
        *,
        ftype: typing.Optional[FileContentFtype] = OMIT,
        filename: typing.Optional[str] = OMIT,
        furl: typing.Optional[str] = OMIT,
        f_content: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[PayabliApiResponseOcr]:
        """
        Use this endpoint to submit a Base64-encoded image file for OCR processing. The accepted file formats include PDF, JPG, JPEG, PNG, and GIF. Specify the desired type of result (either 'bill' or 'invoice') in the path parameter `typeResult`. The response will contain the OCR processing results, including extracted data such as bill number, vendor information, bill items, and more.

        Parameters
        ----------
        type_result : TypeResult

        ftype : typing.Optional[FileContentFtype]

        filename : typing.Optional[str]
            The name of the file to be uploaded

        furl : typing.Optional[str]
            Optional URL link to the file

        f_content : typing.Optional[str]
            Base64-encoded file content

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

        Returns
        -------
        AsyncHttpResponse[PayabliApiResponseOcr]
            OCR document processing result
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"Import/ocrDocumentJson/{jsonable_encoder(type_result)}",
            method="POST",
            json={
                "ftype": ftype,
                "filename": filename,
                "furl": furl,
                "fContent": f_content,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PayabliApiResponseOcr,
                    parse_obj_as(
                        type_=PayabliApiResponseOcr,  # 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)
