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

import typing
import uuid
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 ..transcribe.errors.authentication_error import AuthenticationError
from .errors.ocr_job_not_found import OcrJobNotFound
from .types.ocr_job_response import OcrJobResponse
from .types.ocr_job_status import OcrJobStatus

# 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 create_ocr_job(
        self,
        *,
        storage_path: str,
        job_id: typing.Optional[uuid.UUID] = OMIT,
        file_name: typing.Optional[str] = OMIT,
        content_type: typing.Optional[str] = OMIT,
        file_kind: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[OcrJobResponse]:
        """
        Create an OCR job for a file already uploaded to Supabase Storage.

        Parameters
        ----------
        storage_path : str
            Path to the uploaded file within the OCR storage bucket.

        job_id : typing.Optional[uuid.UUID]
            Optional job id. Provide this when you pre-allocate a storage path using the job id.

        file_name : typing.Optional[str]
            Original file name.

        content_type : typing.Optional[str]
            MIME type of the uploaded file (e.g., application/pdf, image/png).

        file_kind : typing.Optional[str]
            Optional hint for file type (pdf or image).

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

        Returns
        -------
        HttpResponse[OcrJobResponse]
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/ocr/jobs",
            method="POST",
            json={
                "job_id": job_id,
                "storage_path": storage_path,
                "file_name": file_name,
                "content_type": content_type,
                "file_kind": file_kind,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    OcrJobResponse,
                    parse_obj_as(
                        type_=OcrJobResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 401:
                raise AuthenticationError(headers=dict(_response.headers))
            _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 get_ocr_job(
        self, job_id: uuid.UUID, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[OcrJobStatus]:
        """
        Get OCR job status and extracted text for each page.

        Parameters
        ----------
        job_id : uuid.UUID
            OCR job id.

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

        Returns
        -------
        HttpResponse[OcrJobStatus]
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/ocr/jobs/{jsonable_encoder(job_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    OcrJobStatus,
                    parse_obj_as(
                        type_=OcrJobStatus,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 404:
                raise OcrJobNotFound(headers=dict(_response.headers))
            if _response.status_code == 401:
                raise AuthenticationError(headers=dict(_response.headers))
            _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 create_ocr_job(
        self,
        *,
        storage_path: str,
        job_id: typing.Optional[uuid.UUID] = OMIT,
        file_name: typing.Optional[str] = OMIT,
        content_type: typing.Optional[str] = OMIT,
        file_kind: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[OcrJobResponse]:
        """
        Create an OCR job for a file already uploaded to Supabase Storage.

        Parameters
        ----------
        storage_path : str
            Path to the uploaded file within the OCR storage bucket.

        job_id : typing.Optional[uuid.UUID]
            Optional job id. Provide this when you pre-allocate a storage path using the job id.

        file_name : typing.Optional[str]
            Original file name.

        content_type : typing.Optional[str]
            MIME type of the uploaded file (e.g., application/pdf, image/png).

        file_kind : typing.Optional[str]
            Optional hint for file type (pdf or image).

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

        Returns
        -------
        AsyncHttpResponse[OcrJobResponse]
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/ocr/jobs",
            method="POST",
            json={
                "job_id": job_id,
                "storage_path": storage_path,
                "file_name": file_name,
                "content_type": content_type,
                "file_kind": file_kind,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    OcrJobResponse,
                    parse_obj_as(
                        type_=OcrJobResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 401:
                raise AuthenticationError(headers=dict(_response.headers))
            _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 get_ocr_job(
        self, job_id: uuid.UUID, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[OcrJobStatus]:
        """
        Get OCR job status and extracted text for each page.

        Parameters
        ----------
        job_id : uuid.UUID
            OCR job id.

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

        Returns
        -------
        AsyncHttpResponse[OcrJobStatus]
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/ocr/jobs/{jsonable_encoder(job_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    OcrJobStatus,
                    parse_obj_as(
                        type_=OcrJobStatus,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 404:
                raise OcrJobNotFound(headers=dict(_response.headers))
            if _response.status_code == 401:
                raise AuthenticationError(headers=dict(_response.headers))
            _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)
