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

import typing
from json.decoder import JSONDecodeError

from .. import core
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 ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.create_custom_voice_out import CreateCustomVoiceOut
from ..types.gender import Gender
from ..types.http_validation_error import HttpValidationError
from ..types.languages import Languages
from .types.list_voices_list_voices_get_response_item import ListVoicesListVoicesGetResponseItem

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


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

    def list_voices(
        self, *, run_id: typing.Optional[int] = None, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[typing.List[ListVoicesListVoicesGetResponseItem]]:
        """
        Parameters
        ----------
        run_id : typing.Optional[int]

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

        Returns
        -------
        HttpResponse[typing.List[ListVoicesListVoicesGetResponseItem]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "list-voices",
            method="GET",
            params={
                "run_id": run_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[ListVoicesListVoicesGetResponseItem],
                    parse_obj_as(
                        type_=typing.List[ListVoicesListVoicesGetResponseItem],  # 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(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 create_custom_voice(
        self,
        *,
        voice_name: str,
        gender: Gender,
        file: core.File,
        run_id: typing.Optional[int] = None,
        description: typing.Optional[str] = OMIT,
        publish_voice_to_market_place: typing.Optional[bool] = OMIT,
        age: typing.Optional[int] = OMIT,
        language: typing.Optional[Languages] = OMIT,
        enhance_audio: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[CreateCustomVoiceOut]:
        """
        Parameters
        ----------
        voice_name : str

        gender : Gender

        file : core.File
            See core.File for more documentation

        run_id : typing.Optional[int]

        description : typing.Optional[str]

        publish_voice_to_market_place : typing.Optional[bool]
            By setting true, you consent to making this voice available for other users in the marketplace.

        age : typing.Optional[int]

        language : typing.Optional[Languages]
            Language of the voice

        enhance_audio : typing.Optional[bool]
            Enables advanced audio processing to enhance the clarity and quality of the cloned voice output. When set to `true`, additional processing is applied to improve the overall sound experience.

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

        Returns
        -------
        HttpResponse[CreateCustomVoiceOut]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "create-custom-voice",
            method="POST",
            params={
                "run_id": run_id,
            },
            data={
                "voice_name": voice_name,
                "gender": gender,
                "description": description,
                "publish_voice_to_market_place": publish_voice_to_market_place,
                "age": age,
                "language": language,
                "enhance_audio": enhance_audio,
            },
            files={
                "file": file,
            },
            request_options=request_options,
            omit=OMIT,
            force_multipart=True,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateCustomVoiceOut,
                    parse_obj_as(
                        type_=CreateCustomVoiceOut,  # 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(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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)


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

    async def list_voices(
        self, *, run_id: typing.Optional[int] = None, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[typing.List[ListVoicesListVoicesGetResponseItem]]:
        """
        Parameters
        ----------
        run_id : typing.Optional[int]

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

        Returns
        -------
        AsyncHttpResponse[typing.List[ListVoicesListVoicesGetResponseItem]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "list-voices",
            method="GET",
            params={
                "run_id": run_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[ListVoicesListVoicesGetResponseItem],
                    parse_obj_as(
                        type_=typing.List[ListVoicesListVoicesGetResponseItem],  # 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(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 create_custom_voice(
        self,
        *,
        voice_name: str,
        gender: Gender,
        file: core.File,
        run_id: typing.Optional[int] = None,
        description: typing.Optional[str] = OMIT,
        publish_voice_to_market_place: typing.Optional[bool] = OMIT,
        age: typing.Optional[int] = OMIT,
        language: typing.Optional[Languages] = OMIT,
        enhance_audio: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[CreateCustomVoiceOut]:
        """
        Parameters
        ----------
        voice_name : str

        gender : Gender

        file : core.File
            See core.File for more documentation

        run_id : typing.Optional[int]

        description : typing.Optional[str]

        publish_voice_to_market_place : typing.Optional[bool]
            By setting true, you consent to making this voice available for other users in the marketplace.

        age : typing.Optional[int]

        language : typing.Optional[Languages]
            Language of the voice

        enhance_audio : typing.Optional[bool]
            Enables advanced audio processing to enhance the clarity and quality of the cloned voice output. When set to `true`, additional processing is applied to improve the overall sound experience.

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

        Returns
        -------
        AsyncHttpResponse[CreateCustomVoiceOut]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "create-custom-voice",
            method="POST",
            params={
                "run_id": run_id,
            },
            data={
                "voice_name": voice_name,
                "gender": gender,
                "description": description,
                "publish_voice_to_market_place": publish_voice_to_market_place,
                "age": age,
                "language": language,
                "enhance_audio": enhance_audio,
            },
            files={
                "file": file,
            },
            request_options=request_options,
            omit=OMIT,
            force_multipart=True,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateCustomVoiceOut,
                    parse_obj_as(
                        type_=CreateCustomVoiceOut,  # 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(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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)
