# 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 ..core.serialization import convert_and_respect_annotation_metadata
from ..transcribe.errors.authentication_error import AuthenticationError
from ..transcribe.errors.rate_limit_error import RateLimitError
from ..transcribe.types.language import Language
from ..transcribe.types.timestamp import Timestamp
from ..transcribe.types.transcription_id import TranscriptionId
from .types.outline_topic import OutlineTopic

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


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

    def outline(
        self,
        *,
        transcription_id: TranscriptionId,
        monotone: typing.Optional[bool] = OMIT,
        conclusion_bias: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.List[OutlineTopic]]:
        """
        Outline of topics discussed by timestamp, generated end-to-end from a transcription ID.

        This endpoint will:
        1) Fetch the transcript and word-level timestamps for the given transcription
        2) Generate chapter topics (title + starting_phrase) using an LLM from the transcript text
        3) Align each topic's starting phrase to timestamps

        Parameters
        ----------
        transcription_id : TranscriptionId
            ID of the transcription to process end-to-end

        monotone : typing.Optional[bool]
            If true, each topic is searched after the previous topic's start (with a small backoff)

        conclusion_bias : typing.Optional[bool]
            If true and a title includes the word "conclusion", search in the last third of the audio

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

        Returns
        -------
        HttpResponse[typing.List[OutlineTopic]]
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/timestamps/outline",
            method="POST",
            json={
                "transcription_id": transcription_id,
                "monotone": monotone,
                "conclusion_bias": conclusion_bias,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[OutlineTopic],
                    parse_obj_as(
                        type_=typing.List[OutlineTopic],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 401:
                raise AuthenticationError(headers=dict(_response.headers))
            if _response.status_code == 429:
                raise RateLimitError(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 update_timestamps(
        self,
        *,
        old_timestamps: typing.Sequence[Timestamp],
        edited_text: str,
        language_to_update: typing.Optional[Language] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.List[Timestamp]]:
        """
        Update the timestamps based on edited text.

        Parameters
        ----------
        old_timestamps : typing.Sequence[Timestamp]
            The original timestamps associated with the text before editing. These will be used as reference points to align the new timestamps.

        edited_text : str
            The modified version of the transcription text that needs updated timestamp alignments. This should be the complete text after your edits.

        language_to_update : typing.Optional[Language]
            If hebrew_word_format included both 'en' and 'he' (and therefor, for the same word there is both an English and a Hebrew version),this specifies which language version of the timestamps to update. Must be either 'en' for English or 'he' for Hebrew timestamps.

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

        Returns
        -------
        HttpResponse[typing.List[Timestamp]]
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/timestamps/update-timestamps",
            method="POST",
            json={
                "old_timestamps": convert_and_respect_annotation_metadata(
                    object_=old_timestamps, annotation=typing.Sequence[Timestamp], direction="write"
                ),
                "edited_text": edited_text,
                "language_to_update": language_to_update,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[Timestamp],
                    parse_obj_as(
                        type_=typing.List[Timestamp],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 401:
                raise AuthenticationError(headers=dict(_response.headers))
            if _response.status_code == 429:
                raise RateLimitError(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 AsyncRawTimestampsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def outline(
        self,
        *,
        transcription_id: TranscriptionId,
        monotone: typing.Optional[bool] = OMIT,
        conclusion_bias: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.List[OutlineTopic]]:
        """
        Outline of topics discussed by timestamp, generated end-to-end from a transcription ID.

        This endpoint will:
        1) Fetch the transcript and word-level timestamps for the given transcription
        2) Generate chapter topics (title + starting_phrase) using an LLM from the transcript text
        3) Align each topic's starting phrase to timestamps

        Parameters
        ----------
        transcription_id : TranscriptionId
            ID of the transcription to process end-to-end

        monotone : typing.Optional[bool]
            If true, each topic is searched after the previous topic's start (with a small backoff)

        conclusion_bias : typing.Optional[bool]
            If true and a title includes the word "conclusion", search in the last third of the audio

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

        Returns
        -------
        AsyncHttpResponse[typing.List[OutlineTopic]]
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/timestamps/outline",
            method="POST",
            json={
                "transcription_id": transcription_id,
                "monotone": monotone,
                "conclusion_bias": conclusion_bias,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[OutlineTopic],
                    parse_obj_as(
                        type_=typing.List[OutlineTopic],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 401:
                raise AuthenticationError(headers=dict(_response.headers))
            if _response.status_code == 429:
                raise RateLimitError(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 update_timestamps(
        self,
        *,
        old_timestamps: typing.Sequence[Timestamp],
        edited_text: str,
        language_to_update: typing.Optional[Language] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.List[Timestamp]]:
        """
        Update the timestamps based on edited text.

        Parameters
        ----------
        old_timestamps : typing.Sequence[Timestamp]
            The original timestamps associated with the text before editing. These will be used as reference points to align the new timestamps.

        edited_text : str
            The modified version of the transcription text that needs updated timestamp alignments. This should be the complete text after your edits.

        language_to_update : typing.Optional[Language]
            If hebrew_word_format included both 'en' and 'he' (and therefor, for the same word there is both an English and a Hebrew version),this specifies which language version of the timestamps to update. Must be either 'en' for English or 'he' for Hebrew timestamps.

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

        Returns
        -------
        AsyncHttpResponse[typing.List[Timestamp]]
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/timestamps/update-timestamps",
            method="POST",
            json={
                "old_timestamps": convert_and_respect_annotation_metadata(
                    object_=old_timestamps, annotation=typing.Sequence[Timestamp], direction="write"
                ),
                "edited_text": edited_text,
                "language_to_update": language_to_update,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[Timestamp],
                    parse_obj_as(
                        type_=typing.List[Timestamp],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 401:
                raise AuthenticationError(headers=dict(_response.headers))
            if _response.status_code == 429:
                raise RateLimitError(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)
