"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from .basesdk import BaseSDK
from supertone import errors, models, utils
from supertone._hooks import HookContext
from supertone.types import OptionalNullable, UNSET
from supertone.utils.unmarshal_json_response import unmarshal_json_response
from typing import Mapping, Optional


class Voices(BaseSDK):
    r"""Voice Library API endpoints"""

    def list_voices(
        self,
        *,
        page_size: Optional[float] = None,
        next_page_token: Optional[str] = None,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.GetAPICharacterListResponse:
        r"""Gets available voices

        Gets a paginated list of voices available to the user based on internal group logic, using token-based pagination.

        :param page_size: Number of items per page (default: 20, min: 10, max: 100)
        :param next_page_token: Token for pagination (obtained from the previous page's response)
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.ListVoicesRequest(
            page_size=page_size,
            next_page_token=next_page_token,
        )

        req = self._build_request(
            method="GET",
            path="/v1/voices",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=False,
            request_has_path_params=False,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="application/json",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = self.do_request(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="list_voices",
                oauth2_scopes=[],
                security_source=self.sdk_configuration.security,
            ),
            request=req,
            error_status_codes=["401", "404", "4XX", "500", "5XX"],
            retry_config=retry_config,
        )

        if utils.match_response(http_res, "200", "application/json"):
            return unmarshal_json_response(models.GetAPICharacterListResponse, http_res)
        if utils.match_response(http_res, ["401", "404", "4XX"], "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise errors.SupertoneDefaultError(
                "API error occurred", http_res, http_res_text
            )
        if utils.match_response(http_res, ["500", "5XX"], "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise errors.SupertoneDefaultError(
                "API error occurred", http_res, http_res_text
            )

        raise errors.SupertoneDefaultError("Unexpected response received", http_res)

    async def list_voices_async(
        self,
        *,
        page_size: Optional[float] = None,
        next_page_token: Optional[str] = None,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.GetAPICharacterListResponse:
        r"""Gets available voices

        Gets a paginated list of voices available to the user based on internal group logic, using token-based pagination.

        :param page_size: Number of items per page (default: 20, min: 10, max: 100)
        :param next_page_token: Token for pagination (obtained from the previous page's response)
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.ListVoicesRequest(
            page_size=page_size,
            next_page_token=next_page_token,
        )

        req = self._build_request_async(
            method="GET",
            path="/v1/voices",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=False,
            request_has_path_params=False,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="application/json",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = await self.do_request_async(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="list_voices",
                oauth2_scopes=[],
                security_source=self.sdk_configuration.security,
            ),
            request=req,
            error_status_codes=["401", "404", "4XX", "500", "5XX"],
            retry_config=retry_config,
        )

        if utils.match_response(http_res, "200", "application/json"):
            return unmarshal_json_response(models.GetAPICharacterListResponse, http_res)
        if utils.match_response(http_res, ["401", "404", "4XX"], "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise errors.SupertoneDefaultError(
                "API error occurred", http_res, http_res_text
            )
        if utils.match_response(http_res, ["500", "5XX"], "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise errors.SupertoneDefaultError(
                "API error occurred", http_res, http_res_text
            )

        raise errors.SupertoneDefaultError("Unexpected response received", http_res)

    def search_voices(
        self,
        *,
        page_size: Optional[float] = None,
        next_page_token: Optional[str] = None,
        name: Optional[str] = None,
        description: Optional[str] = None,
        language: Optional[str] = None,
        gender: Optional[str] = None,
        age: Optional[str] = None,
        use_case: Optional[str] = None,
        use_cases: Optional[str] = None,
        style: Optional[str] = None,
        model: Optional[str] = None,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.GetAPICharacterListResponse:
        r"""Search voices.

        Search and filter voices based on various parameters.

        :param page_size: Number of items per page (default: 20, min: 10, max: 100)
        :param next_page_token: Token for pagination (obtained from the previous page's response)
        :param name: Search across name. Space separated.
        :param description: Search across description. Space separated.
        :param language: Filter by language (comma-separated)
        :param gender: Filter by gender (comma-separated)
        :param age: Filter by age (comma-separated)
        :param use_case: Filter by use case (comma-separated)
        :param use_cases: Filter by use cases array (comma-separated for OR logic)
        :param style: Filter by style (comma-separated for OR, semicolon-separated for AND). Mixing comma and semicolon is invalid and will result in 400. Note: AND semantics apply across styles on a single character; cloned voices have a single style and will only match AND when exactly one style is requested and equals the cloned voice style.
        :param model: Filter by model (comma-separated)
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.SearchVoicesRequest(
            page_size=page_size,
            next_page_token=next_page_token,
            name=name,
            description=description,
            language=language,
            gender=gender,
            age=age,
            use_case=use_case,
            use_cases=use_cases,
            style=style,
            model=model,
        )

        req = self._build_request(
            method="GET",
            path="/v1/voices/search",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=False,
            request_has_path_params=False,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="application/json",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = self.do_request(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="search_voices",
                oauth2_scopes=[],
                security_source=self.sdk_configuration.security,
            ),
            request=req,
            error_status_codes=["400", "401", "404", "4XX", "500", "5XX"],
            retry_config=retry_config,
        )

        if utils.match_response(http_res, "200", "application/json"):
            return unmarshal_json_response(models.GetAPICharacterListResponse, http_res)
        if utils.match_response(http_res, ["400", "401", "404", "4XX"], "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise errors.SupertoneDefaultError(
                "API error occurred", http_res, http_res_text
            )
        if utils.match_response(http_res, ["500", "5XX"], "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise errors.SupertoneDefaultError(
                "API error occurred", http_res, http_res_text
            )

        raise errors.SupertoneDefaultError("Unexpected response received", http_res)

    async def search_voices_async(
        self,
        *,
        page_size: Optional[float] = None,
        next_page_token: Optional[str] = None,
        name: Optional[str] = None,
        description: Optional[str] = None,
        language: Optional[str] = None,
        gender: Optional[str] = None,
        age: Optional[str] = None,
        use_case: Optional[str] = None,
        use_cases: Optional[str] = None,
        style: Optional[str] = None,
        model: Optional[str] = None,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.GetAPICharacterListResponse:
        r"""Search voices.

        Search and filter voices based on various parameters.

        :param page_size: Number of items per page (default: 20, min: 10, max: 100)
        :param next_page_token: Token for pagination (obtained from the previous page's response)
        :param name: Search across name. Space separated.
        :param description: Search across description. Space separated.
        :param language: Filter by language (comma-separated)
        :param gender: Filter by gender (comma-separated)
        :param age: Filter by age (comma-separated)
        :param use_case: Filter by use case (comma-separated)
        :param use_cases: Filter by use cases array (comma-separated for OR logic)
        :param style: Filter by style (comma-separated for OR, semicolon-separated for AND). Mixing comma and semicolon is invalid and will result in 400. Note: AND semantics apply across styles on a single character; cloned voices have a single style and will only match AND when exactly one style is requested and equals the cloned voice style.
        :param model: Filter by model (comma-separated)
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.SearchVoicesRequest(
            page_size=page_size,
            next_page_token=next_page_token,
            name=name,
            description=description,
            language=language,
            gender=gender,
            age=age,
            use_case=use_case,
            use_cases=use_cases,
            style=style,
            model=model,
        )

        req = self._build_request_async(
            method="GET",
            path="/v1/voices/search",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=False,
            request_has_path_params=False,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="application/json",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = await self.do_request_async(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="search_voices",
                oauth2_scopes=[],
                security_source=self.sdk_configuration.security,
            ),
            request=req,
            error_status_codes=["400", "401", "404", "4XX", "500", "5XX"],
            retry_config=retry_config,
        )

        if utils.match_response(http_res, "200", "application/json"):
            return unmarshal_json_response(models.GetAPICharacterListResponse, http_res)
        if utils.match_response(http_res, ["400", "401", "404", "4XX"], "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise errors.SupertoneDefaultError(
                "API error occurred", http_res, http_res_text
            )
        if utils.match_response(http_res, ["500", "5XX"], "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise errors.SupertoneDefaultError(
                "API error occurred", http_res, http_res_text
            )

        raise errors.SupertoneDefaultError("Unexpected response received", http_res)

    def get_voice(
        self,
        *,
        voice_id: str,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.GetCharacterByIDResponse:
        r"""Get voice details by ID

        Gets detailed information about a specific voice by its voice ID. Only supports preset voices.

        :param voice_id:
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.GetVoiceRequest(
            voice_id=voice_id,
        )

        req = self._build_request(
            method="GET",
            path="/v1/voices/{voice_id}",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=False,
            request_has_path_params=True,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="application/json",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = self.do_request(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="get_voice",
                oauth2_scopes=[],
                security_source=self.sdk_configuration.security,
            ),
            request=req,
            error_status_codes=["401", "404", "4XX", "500", "5XX"],
            retry_config=retry_config,
        )

        if utils.match_response(http_res, "200", "application/json"):
            return unmarshal_json_response(models.GetCharacterByIDResponse, http_res)
        if utils.match_response(http_res, ["401", "404", "4XX"], "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise errors.SupertoneDefaultError(
                "API error occurred", http_res, http_res_text
            )
        if utils.match_response(http_res, ["500", "5XX"], "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise errors.SupertoneDefaultError(
                "API error occurred", http_res, http_res_text
            )

        raise errors.SupertoneDefaultError("Unexpected response received", http_res)

    async def get_voice_async(
        self,
        *,
        voice_id: str,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.GetCharacterByIDResponse:
        r"""Get voice details by ID

        Gets detailed information about a specific voice by its voice ID. Only supports preset voices.

        :param voice_id:
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.GetVoiceRequest(
            voice_id=voice_id,
        )

        req = self._build_request_async(
            method="GET",
            path="/v1/voices/{voice_id}",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=False,
            request_has_path_params=True,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="application/json",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = await self.do_request_async(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="get_voice",
                oauth2_scopes=[],
                security_source=self.sdk_configuration.security,
            ),
            request=req,
            error_status_codes=["401", "404", "4XX", "500", "5XX"],
            retry_config=retry_config,
        )

        if utils.match_response(http_res, "200", "application/json"):
            return unmarshal_json_response(models.GetCharacterByIDResponse, http_res)
        if utils.match_response(http_res, ["401", "404", "4XX"], "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise errors.SupertoneDefaultError(
                "API error occurred", http_res, http_res_text
            )
        if utils.match_response(http_res, ["500", "5XX"], "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise errors.SupertoneDefaultError(
                "API error occurred", http_res, http_res_text
            )

        raise errors.SupertoneDefaultError("Unexpected response received", http_res)
