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

import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.create_tts_out import CreateTtsOut
from ..types.gender import Gender
from ..types.languages import Languages
from ..types.orchestrator_pipeline_result import OrchestratorPipelineResult
from ..types.stream_tts_inference_options import StreamTtsInferenceOptions
from ..types.stream_tts_output_configuration import StreamTtsOutputConfiguration
from ..types.stream_tts_voice_settings import StreamTtsVoiceSettings
from .raw_client import AsyncRawTextToSpeechClient, RawTextToSpeechClient
from .types.create_stream_tts_request_payload_language import CreateStreamTtsRequestPayloadLanguage
from .types.create_stream_tts_request_payload_speech_model import CreateStreamTtsRequestPayloadSpeechModel
from .types.get_tts_results_tts_results_post_response_value import GetTtsResultsTtsResultsPostResponseValue
from .types.get_tts_run_info_tts_result_run_id_get_response import GetTtsRunInfoTtsResultRunIdGetResponse

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


class TextToSpeechClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawTextToSpeechClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawTextToSpeechClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawTextToSpeechClient
        """
        return self._raw_client

    def tts(
        self,
        *,
        text: str,
        language: CreateStreamTtsRequestPayloadLanguage,
        voice_id: int,
        speech_model: typing.Optional[CreateStreamTtsRequestPayloadSpeechModel] = OMIT,
        user_instructions: typing.Optional[str] = OMIT,
        enhance_named_entities_pronunciation: typing.Optional[bool] = OMIT,
        output_configuration: typing.Optional[StreamTtsOutputConfiguration] = OMIT,
        voice_settings: typing.Optional[StreamTtsVoiceSettings] = OMIT,
        inference_options: typing.Optional[StreamTtsInferenceOptions] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Iterator[bytes]:
        """
        Parameters
        ----------
        text : str

        language : CreateStreamTtsRequestPayloadLanguage

        voice_id : int

        speech_model : typing.Optional[CreateStreamTtsRequestPayloadSpeechModel]

        user_instructions : typing.Optional[str]

        enhance_named_entities_pronunciation : typing.Optional[bool]

        output_configuration : typing.Optional[StreamTtsOutputConfiguration]

        voice_settings : typing.Optional[StreamTtsVoiceSettings]

        inference_options : typing.Optional[StreamTtsInferenceOptions]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.

        Returns
        -------
        typing.Iterator[bytes]
            Streaming audio response

        Examples
        --------
        from camb import CambApi

        client = CambApi(
            api_key="YOUR_API_KEY",
        )
        client.text_to_speech.tts(
            text="foo",
            language="ar-kw",
            voice_id=1,
        )
        """
        with self._raw_client.tts(
            text=text,
            language=language,
            voice_id=voice_id,
            speech_model=speech_model,
            user_instructions=user_instructions,
            enhance_named_entities_pronunciation=enhance_named_entities_pronunciation,
            output_configuration=output_configuration,
            voice_settings=voice_settings,
            inference_options=inference_options,
            request_options=request_options,
        ) as r:
            yield from r.data

    def create_tts(
        self,
        *,
        text: str,
        voice_id: int,
        language: Languages,
        run_id: typing.Optional[int] = None,
        project_name: typing.Optional[str] = OMIT,
        project_description: typing.Optional[str] = OMIT,
        folder_id: typing.Optional[int] = OMIT,
        gender: typing.Optional[Gender] = OMIT,
        age: typing.Optional[int] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateTtsOut:
        """
        Parameters
        ----------
        text : str

        voice_id : int

        language : Languages

        run_id : typing.Optional[int]

        project_name : typing.Optional[str]

        project_description : typing.Optional[str]

        folder_id : typing.Optional[int]

        gender : typing.Optional[Gender]

        age : typing.Optional[int]

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

        Returns
        -------
        CreateTtsOut
            Successful Response

        Examples
        --------
        from camb import CambApi

        client = CambApi(
            api_key="YOUR_API_KEY",
        )
        client.text_to_speech.create_tts(
            text="text",
            voice_id=1,
            language=1,
        )
        """
        _response = self._raw_client.create_tts(
            text=text,
            voice_id=voice_id,
            language=language,
            run_id=run_id,
            project_name=project_name,
            project_description=project_description,
            folder_id=folder_id,
            gender=gender,
            age=age,
            request_options=request_options,
        )
        return _response.data

    def get_tts_result(
        self,
        task_id: str,
        *,
        run_id: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> OrchestratorPipelineResult:
        """
        Parameters
        ----------
        task_id : str

        run_id : typing.Optional[int]

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

        Returns
        -------
        OrchestratorPipelineResult
            Successful Response

        Examples
        --------
        from camb import CambApi

        client = CambApi(
            api_key="YOUR_API_KEY",
        )
        client.text_to_speech.get_tts_result(
            task_id="task_id",
        )
        """
        _response = self._raw_client.get_tts_result(task_id, run_id=run_id, request_options=request_options)
        return _response.data

    def get_tts_run_info(
        self,
        run_id: typing.Optional[int],
        *,
        output_type: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> GetTtsRunInfoTtsResultRunIdGetResponse:
        """
        Retrieves the result of a Text To Speech (TTS) run.

        This endpoint validates the provided `run_id` and fetches the corresponding TTS-generated audio.
        The user must have valid access to the run. The function supports two output formats:
        - `RAW_BYTES`: Streams the audio file directly.
        - `FILE_URL`: Returns a pre-signed URL to download the audio file.

        Args:
            run_id (int): Unique identifier for the TTS run.
            api_key_obj (dict): API key object used for authentication and storage preferences.
            traceparent (Optional[str]): Traceparent header for distributed tracing.
            output_type (OutputType, optional): Determines the output format. Defaults to `RAW_BYTES`.

        Returns:
            StreamingResponse | GetTTSOut:
                - If `output_type = RAW_BYTES`: A streaming response containing the TTS-generated audio in FLAC format.
                - If `output_type = FILE_URL`: A URL pointing to the stored TTS-generated audio file.

        Raises:
            HTTPException:
                - 400 BAD REQUEST if the run ID is invalid or does not belong to a TTS process.
                - 500 INTERNAL SERVER ERROR if fetching or streaming the audio fails.

        Assumptions:
            - The user has valid access to the `run_id`.
            - The `run_id` corresponds to a valid TTS run.
            - There is only **one** dialogue associated with the given `run_id`.

        Parameters
        ----------
        run_id : typing.Optional[int]

        output_type : typing.Optional[str]
            Output format for the Text To Speech result

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

        Returns
        -------
        GetTtsRunInfoTtsResultRunIdGetResponse
            Successful Response

        Examples
        --------
        from camb import CambApi

        client = CambApi(
            api_key="YOUR_API_KEY",
        )
        client.text_to_speech.get_tts_run_info(
            run_id=1,
        )
        """
        _response = self._raw_client.get_tts_run_info(run_id, output_type=output_type, request_options=request_options)
        return _response.data

    def get_tts_results(
        self,
        *,
        run_ids: typing.Sequence[int],
        run_id: typing.Optional[int] = None,
        traceparent: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Dict[str, GetTtsResultsTtsResultsPostResponseValue]:
        """
        Parameters
        ----------
        run_ids : typing.Sequence[int]

        run_id : typing.Optional[int]

        traceparent : typing.Optional[str]

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

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

        Examples
        --------
        from camb import CambApi

        client = CambApi(
            api_key="YOUR_API_KEY",
        )
        client.text_to_speech.get_tts_results(
            run_ids=[1],
        )
        """
        _response = self._raw_client.get_tts_results(
            run_ids=run_ids, run_id=run_id, traceparent=traceparent, request_options=request_options
        )
        return _response.data

    def get_tts_result_discord(
        self,
        task_id: str,
        *,
        run_id: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> OrchestratorPipelineResult:
        """
        Parameters
        ----------
        task_id : str

        run_id : typing.Optional[int]

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

        Returns
        -------
        OrchestratorPipelineResult
            Successful Response

        Examples
        --------
        from camb import CambApi

        client = CambApi(
            api_key="YOUR_API_KEY",
        )
        client.text_to_speech.get_tts_result_discord(
            task_id="task_id",
        )
        """
        _response = self._raw_client.get_tts_result_discord(task_id, run_id=run_id, request_options=request_options)
        return _response.data


class AsyncTextToSpeechClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawTextToSpeechClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawTextToSpeechClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawTextToSpeechClient
        """
        return self._raw_client

    async def tts(
        self,
        *,
        text: str,
        language: CreateStreamTtsRequestPayloadLanguage,
        voice_id: int,
        speech_model: typing.Optional[CreateStreamTtsRequestPayloadSpeechModel] = OMIT,
        user_instructions: typing.Optional[str] = OMIT,
        enhance_named_entities_pronunciation: typing.Optional[bool] = OMIT,
        output_configuration: typing.Optional[StreamTtsOutputConfiguration] = OMIT,
        voice_settings: typing.Optional[StreamTtsVoiceSettings] = OMIT,
        inference_options: typing.Optional[StreamTtsInferenceOptions] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.AsyncIterator[bytes]:
        """
        Parameters
        ----------
        text : str

        language : CreateStreamTtsRequestPayloadLanguage

        voice_id : int

        speech_model : typing.Optional[CreateStreamTtsRequestPayloadSpeechModel]

        user_instructions : typing.Optional[str]

        enhance_named_entities_pronunciation : typing.Optional[bool]

        output_configuration : typing.Optional[StreamTtsOutputConfiguration]

        voice_settings : typing.Optional[StreamTtsVoiceSettings]

        inference_options : typing.Optional[StreamTtsInferenceOptions]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.

        Returns
        -------
        typing.AsyncIterator[bytes]
            Streaming audio response

        Examples
        --------
        import asyncio

        from camb import AsyncCambApi

        client = AsyncCambApi(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.text_to_speech.tts(
                text="foo",
                language="ar-kw",
                voice_id=1,
            )


        asyncio.run(main())
        """
        async with self._raw_client.tts(
            text=text,
            language=language,
            voice_id=voice_id,
            speech_model=speech_model,
            user_instructions=user_instructions,
            enhance_named_entities_pronunciation=enhance_named_entities_pronunciation,
            output_configuration=output_configuration,
            voice_settings=voice_settings,
            inference_options=inference_options,
            request_options=request_options,
        ) as r:
            async for _chunk in r.data:
                yield _chunk

    async def create_tts(
        self,
        *,
        text: str,
        voice_id: int,
        language: Languages,
        run_id: typing.Optional[int] = None,
        project_name: typing.Optional[str] = OMIT,
        project_description: typing.Optional[str] = OMIT,
        folder_id: typing.Optional[int] = OMIT,
        gender: typing.Optional[Gender] = OMIT,
        age: typing.Optional[int] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateTtsOut:
        """
        Parameters
        ----------
        text : str

        voice_id : int

        language : Languages

        run_id : typing.Optional[int]

        project_name : typing.Optional[str]

        project_description : typing.Optional[str]

        folder_id : typing.Optional[int]

        gender : typing.Optional[Gender]

        age : typing.Optional[int]

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

        Returns
        -------
        CreateTtsOut
            Successful Response

        Examples
        --------
        import asyncio

        from camb import AsyncCambApi

        client = AsyncCambApi(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.text_to_speech.create_tts(
                text="text",
                voice_id=1,
                language=1,
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create_tts(
            text=text,
            voice_id=voice_id,
            language=language,
            run_id=run_id,
            project_name=project_name,
            project_description=project_description,
            folder_id=folder_id,
            gender=gender,
            age=age,
            request_options=request_options,
        )
        return _response.data

    async def get_tts_result(
        self,
        task_id: str,
        *,
        run_id: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> OrchestratorPipelineResult:
        """
        Parameters
        ----------
        task_id : str

        run_id : typing.Optional[int]

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

        Returns
        -------
        OrchestratorPipelineResult
            Successful Response

        Examples
        --------
        import asyncio

        from camb import AsyncCambApi

        client = AsyncCambApi(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.text_to_speech.get_tts_result(
                task_id="task_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get_tts_result(task_id, run_id=run_id, request_options=request_options)
        return _response.data

    async def get_tts_run_info(
        self,
        run_id: typing.Optional[int],
        *,
        output_type: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> GetTtsRunInfoTtsResultRunIdGetResponse:
        """
        Retrieves the result of a Text To Speech (TTS) run.

        This endpoint validates the provided `run_id` and fetches the corresponding TTS-generated audio.
        The user must have valid access to the run. The function supports two output formats:
        - `RAW_BYTES`: Streams the audio file directly.
        - `FILE_URL`: Returns a pre-signed URL to download the audio file.

        Args:
            run_id (int): Unique identifier for the TTS run.
            api_key_obj (dict): API key object used for authentication and storage preferences.
            traceparent (Optional[str]): Traceparent header for distributed tracing.
            output_type (OutputType, optional): Determines the output format. Defaults to `RAW_BYTES`.

        Returns:
            StreamingResponse | GetTTSOut:
                - If `output_type = RAW_BYTES`: A streaming response containing the TTS-generated audio in FLAC format.
                - If `output_type = FILE_URL`: A URL pointing to the stored TTS-generated audio file.

        Raises:
            HTTPException:
                - 400 BAD REQUEST if the run ID is invalid or does not belong to a TTS process.
                - 500 INTERNAL SERVER ERROR if fetching or streaming the audio fails.

        Assumptions:
            - The user has valid access to the `run_id`.
            - The `run_id` corresponds to a valid TTS run.
            - There is only **one** dialogue associated with the given `run_id`.

        Parameters
        ----------
        run_id : typing.Optional[int]

        output_type : typing.Optional[str]
            Output format for the Text To Speech result

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

        Returns
        -------
        GetTtsRunInfoTtsResultRunIdGetResponse
            Successful Response

        Examples
        --------
        import asyncio

        from camb import AsyncCambApi

        client = AsyncCambApi(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.text_to_speech.get_tts_run_info(
                run_id=1,
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get_tts_run_info(
            run_id, output_type=output_type, request_options=request_options
        )
        return _response.data

    async def get_tts_results(
        self,
        *,
        run_ids: typing.Sequence[int],
        run_id: typing.Optional[int] = None,
        traceparent: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Dict[str, GetTtsResultsTtsResultsPostResponseValue]:
        """
        Parameters
        ----------
        run_ids : typing.Sequence[int]

        run_id : typing.Optional[int]

        traceparent : typing.Optional[str]

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

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

        Examples
        --------
        import asyncio

        from camb import AsyncCambApi

        client = AsyncCambApi(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.text_to_speech.get_tts_results(
                run_ids=[1],
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get_tts_results(
            run_ids=run_ids, run_id=run_id, traceparent=traceparent, request_options=request_options
        )
        return _response.data

    async def get_tts_result_discord(
        self,
        task_id: str,
        *,
        run_id: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> OrchestratorPipelineResult:
        """
        Parameters
        ----------
        task_id : str

        run_id : typing.Optional[int]

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

        Returns
        -------
        OrchestratorPipelineResult
            Successful Response

        Examples
        --------
        import asyncio

        from camb import AsyncCambApi

        client = AsyncCambApi(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.text_to_speech.get_tts_result_discord(
                task_id="task_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get_tts_result_discord(
            task_id, run_id=run_id, request_options=request_options
        )
        return _response.data
