"""GL Speech Python client library for interacting with the Prosa Speech API.

This library provides a simple interface to interact with the Prosa Speech API,
supporting Speech-to-Text (STT), Text-to-Speech (TTS), and Webhook operations.

Example:
    >>> client = SpeechClient(api_key="your-api-key")
    >>> # Transcribe audio
    >>> result = client.stt.transcribe(
    ...     data="<base64-encoded-audio>",
    ...     model="stt-general",
    ...     wait=True
    ... )
    >>> print(result.result)
    >>>
    >>> # Synthesize speech
    >>> result = client.tts.synthesize(
    ...     text="Hello, world!",
    ...     model="tts-dimas-formal",
    ...     wait=True
    ... )
    >>> print(result.result)

Authors:
    GDP Labs

References:
    https://docs2.prosa.ai/
"""

import os

from gl_speech_sdk.stt import SpeechToText
from gl_speech_sdk.tts import TextToSpeech
from gl_speech_sdk.webhooks import Webhooks

# Ensure the URL ends with a slash; without the trailing slash, the base path will be incorrect.
DEFAULT_BASE_URL = "https://api.prosa.ai/v2/speech/"


class SpeechClient:
    """Prosa Speech API Client.

    Attributes:
        api_key (str): API key for authentication
        base_url (str): Base URL for the Prosa Speech API
        timeout (float): Request timeout in seconds
        default_headers (dict[str, str]): Default headers to include in all requests
        stt (SpeechToText): SpeechToText instance for speech-to-text operations
        tts (TextToSpeech): TextToSpeech instance for text-to-speech operations
        webhooks (Webhooks): Webhooks instance for webhook management operations
    """

    def __init__(
        self,
        api_key: str | None = None,
        base_url: str | None = None,
        timeout: float = 60.0,
        default_headers: dict[str, str] | None = None,
    ):
        """
        Initialize SpeechClient.

        Args:
            api_key (str | None): API key for authentication. If not provided,
                will try to get from GLSPEECH_API_KEY environment variable
            base_url (str | None): Base URL for the Prosa Speech API. If not provided,
                will try to get from GLSPEECH_BASE_URL environment variable,
                otherwise uses default
            timeout (float): Request timeout in seconds
            default_headers (dict[str, str] | None): Default headers to include in all requests.
                These will be merged with any extra_headers provided to individual methods.
        """
        self.api_key = api_key or os.getenv("GLSPEECH_API_KEY")
        if not self.api_key:
            raise ValueError(
                "API key is required. Provide it via 'api_key' parameter or "
                "'GLSPEECH_API_KEY' environment variable."
            )

        self.base_url = base_url or os.getenv("GLSPEECH_BASE_URL") or DEFAULT_BASE_URL
        self.timeout = timeout
        self.default_headers = default_headers or {}
        self.stt = SpeechToText(self)
        self.tts = TextToSpeech(self)
        self.webhooks = Webhooks(self)
