from __future__ import annotations

import os
from unittest import mock
import unittest

from app.providers import tts as tts_module
from app.providers.tts import FakeTTS, build_tts_provider_from_env


class ProviderTTSTests(unittest.TestCase):
    def test_build_tts_provider_from_env_returns_reason_when_provider_is_unsupported(self) -> None:
        with mock.patch.dict(os.environ, {}, clear=True):
            provider, reason = build_tts_provider_from_env("azure")

        self.assertIsNone(provider)
        self.assertEqual(reason, "unsupported_tts_provider:azure")

    def test_build_tts_provider_from_env_returns_reason_when_elevenlabs_sdk_is_missing(self) -> None:
        with mock.patch.dict(
            os.environ,
            {
                "ELEVENLABS_API_KEY": "key-123",
                "ELEVENLABS_VOICE_ID": "voice-123",
            },
            clear=True,
        ):
            with mock.patch.object(tts_module, "_is_elevenlabs_available", return_value=False):
                provider, reason = build_tts_provider_from_env("elevenlabs")

        self.assertIsNone(provider)
        self.assertEqual(reason, "missing_elevenlabs_sdk")

    def test_build_tts_provider_from_env_returns_fake_provider(self) -> None:
        with mock.patch.dict(
            os.environ,
            {
                "FAKE_TTS_TONE_HZ": "512",
                "FAKE_TTS_CHAR_DURATION_MS": "18",
            },
            clear=True,
        ):
            provider, reason = build_tts_provider_from_env("fake")

        self.assertIsInstance(provider, FakeTTS)
        self.assertIsNone(reason)
        self.assertGreater(len(provider.synthesize_pcm16k("teste fake")), 0)


if __name__ == "__main__":
    unittest.main()
