# 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.parse_error import ParsingError
from ....core.request_options import RequestOptions
from ....core.unchecked_base_model import construct_type
from ....errors.unprocessable_entity_error import UnprocessableEntityError
from ....types.dubbing_source_segment_update_response import DubbingSourceSegmentUpdateResponse
from ....types.dubbing_source_transcript_response import DubbingSourceTranscriptResponse
from ....types.dubbing_transcript_revision_response import DubbingTranscriptRevisionResponse
from pydantic import ValidationError

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


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

    def get(
        self, project_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[DubbingSourceTranscriptResponse]:
        """
        The project's source transcript, as editable segments.

        Parameters
        ----------
        project_id : str
            Identifier of the dubbing project.

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

        Returns
        -------
        HttpResponse[DubbingSourceTranscriptResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/dubbing/project/{jsonable_encoder(project_id)}/transcript",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DubbingSourceTranscriptResponse,
                    construct_type(
                        type_=DubbingSourceTranscriptResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # 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)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def delete_segment(
        self, project_id: str, segment_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[DubbingTranscriptRevisionResponse]:
        """
        Remove a source segment from the transcript.

        Parameters
        ----------
        project_id : str
            Identifier of the dubbing project.

        segment_id : str
            Identifier of the segment to remove.

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

        Returns
        -------
        HttpResponse[DubbingTranscriptRevisionResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/dubbing/project/{jsonable_encoder(project_id)}/transcript/segment/{jsonable_encoder(segment_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DubbingTranscriptRevisionResponse,
                    construct_type(
                        type_=DubbingTranscriptRevisionResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # 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)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def update_segment(
        self,
        project_id: str,
        segment_id: str,
        *,
        text: typing.Optional[str] = OMIT,
        speaker_id: typing.Optional[str] = OMIT,
        start_s: typing.Optional[float] = OMIT,
        end_s: typing.Optional[float] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[DubbingSourceSegmentUpdateResponse]:
        """
        Edit a source segment's text, speaker, or timing.

        Parameters
        ----------
        project_id : str
            Identifier of the dubbing project.

        segment_id : str
            Identifier of the segment to edit.

        text : typing.Optional[str]
            New text for the segment.

        speaker_id : typing.Optional[str]
            New speaker id for the segment.

        start_s : typing.Optional[float]
            New start time, in seconds.

        end_s : typing.Optional[float]
            New end time, in seconds.

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

        Returns
        -------
        HttpResponse[DubbingSourceSegmentUpdateResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/dubbing/project/{jsonable_encoder(project_id)}/transcript/segment/{jsonable_encoder(segment_id)}",
            method="PATCH",
            json={
                "text": text,
                "speaker_id": speaker_id,
                "start_s": start_s,
                "end_s": end_s,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DubbingSourceSegmentUpdateResponse,
                    construct_type(
                        type_=DubbingSourceSegmentUpdateResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # 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)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def create_segment(
        self,
        project_id: str,
        *,
        text: str,
        speaker_id: str,
        start_s: float,
        end_s: float,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[DubbingSourceSegmentUpdateResponse]:
        """
        Add a new source segment to the transcript.

        Parameters
        ----------
        project_id : str
            Identifier of the dubbing project.

        text : str
            The text of the new segment.

        speaker_id : str
            Identifier of the segment's speaker.

        start_s : float
            Start time of the segment, in seconds.

        end_s : float
            End time of the segment, in seconds.

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

        Returns
        -------
        HttpResponse[DubbingSourceSegmentUpdateResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/dubbing/project/{jsonable_encoder(project_id)}/transcript/segment",
            method="POST",
            json={
                "text": text,
                "speaker_id": speaker_id,
                "start_s": start_s,
                "end_s": end_s,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DubbingSourceSegmentUpdateResponse,
                    construct_type(
                        type_=DubbingSourceSegmentUpdateResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # 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)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


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

    async def get(
        self, project_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[DubbingSourceTranscriptResponse]:
        """
        The project's source transcript, as editable segments.

        Parameters
        ----------
        project_id : str
            Identifier of the dubbing project.

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

        Returns
        -------
        AsyncHttpResponse[DubbingSourceTranscriptResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/dubbing/project/{jsonable_encoder(project_id)}/transcript",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DubbingSourceTranscriptResponse,
                    construct_type(
                        type_=DubbingSourceTranscriptResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # 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)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def delete_segment(
        self, project_id: str, segment_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[DubbingTranscriptRevisionResponse]:
        """
        Remove a source segment from the transcript.

        Parameters
        ----------
        project_id : str
            Identifier of the dubbing project.

        segment_id : str
            Identifier of the segment to remove.

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

        Returns
        -------
        AsyncHttpResponse[DubbingTranscriptRevisionResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/dubbing/project/{jsonable_encoder(project_id)}/transcript/segment/{jsonable_encoder(segment_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DubbingTranscriptRevisionResponse,
                    construct_type(
                        type_=DubbingTranscriptRevisionResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # 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)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def update_segment(
        self,
        project_id: str,
        segment_id: str,
        *,
        text: typing.Optional[str] = OMIT,
        speaker_id: typing.Optional[str] = OMIT,
        start_s: typing.Optional[float] = OMIT,
        end_s: typing.Optional[float] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[DubbingSourceSegmentUpdateResponse]:
        """
        Edit a source segment's text, speaker, or timing.

        Parameters
        ----------
        project_id : str
            Identifier of the dubbing project.

        segment_id : str
            Identifier of the segment to edit.

        text : typing.Optional[str]
            New text for the segment.

        speaker_id : typing.Optional[str]
            New speaker id for the segment.

        start_s : typing.Optional[float]
            New start time, in seconds.

        end_s : typing.Optional[float]
            New end time, in seconds.

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

        Returns
        -------
        AsyncHttpResponse[DubbingSourceSegmentUpdateResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/dubbing/project/{jsonable_encoder(project_id)}/transcript/segment/{jsonable_encoder(segment_id)}",
            method="PATCH",
            json={
                "text": text,
                "speaker_id": speaker_id,
                "start_s": start_s,
                "end_s": end_s,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DubbingSourceSegmentUpdateResponse,
                    construct_type(
                        type_=DubbingSourceSegmentUpdateResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # 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)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def create_segment(
        self,
        project_id: str,
        *,
        text: str,
        speaker_id: str,
        start_s: float,
        end_s: float,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[DubbingSourceSegmentUpdateResponse]:
        """
        Add a new source segment to the transcript.

        Parameters
        ----------
        project_id : str
            Identifier of the dubbing project.

        text : str
            The text of the new segment.

        speaker_id : str
            Identifier of the segment's speaker.

        start_s : float
            Start time of the segment, in seconds.

        end_s : float
            End time of the segment, in seconds.

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

        Returns
        -------
        AsyncHttpResponse[DubbingSourceSegmentUpdateResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/dubbing/project/{jsonable_encoder(project_id)}/transcript/segment",
            method="POST",
            json={
                "text": text,
                "speaker_id": speaker_id,
                "start_s": start_s,
                "end_s": end_s,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DubbingSourceSegmentUpdateResponse,
                    construct_type(
                        type_=DubbingSourceSegmentUpdateResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # 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)
        except ValidationError as e:
            raise ParsingError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e
            )
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
