from __future__ import annotations

import unittest

from app.livekit.azure_speech import resolve_azure_speech_tts_config


class AzureSpeechTTSTests(unittest.TestCase):
    def test_resolve_uses_region_voice_and_optional_language(self) -> None:
        config, missing = resolve_azure_speech_tts_config(
            {},
            environ={
                "AZURE_SPEECH_KEY": "key-123",
                "AZURE_SPEECH_REGION": "brazilsouth",
                "AZURE_SPEECH_VOICE": "pt-BR-FranciscaNeural",
                "AZURE_SPEECH_LANGUAGE": "pt-BR",
            },
        )

        self.assertEqual(missing, [])
        self.assertEqual(config["speech_key"], "key-123")
        self.assertEqual(config["speech_region"], "brazilsouth")
        self.assertIsNone(config["speech_endpoint"])
        self.assertEqual(config["voice"], "pt-BR-FranciscaNeural")
        self.assertEqual(config["language"], "pt-BR")
        self.assertIsNone(config["deployment_id"])

    def test_resolve_keeps_explicit_custom_endpoint_for_standard_voice(self) -> None:
        config, missing = resolve_azure_speech_tts_config(
            {},
            environ={
                "AZURE_SPEECH_KEY": "key-123",
                "AZURE_SPEECH_REGION": "brazilsouth",
                "AZURE_SPEECH_ENDPOINT": "https://speech.example.cognitiveservices.azure.com/",
                "AZURE_SPEECH_VOICE": "pt-BR-FranciscaNeural",
            },
        )

        self.assertEqual(missing, [])
        self.assertEqual(config["speech_region"], "brazilsouth")
        self.assertEqual(
            config["speech_endpoint"],
            "https://speech.example.cognitiveservices.azure.com/tts/cognitiveservices/v1",
        )

    def test_resolve_prefers_endpoint_and_maps_legacy_override_names(self) -> None:
        config, missing = resolve_azure_speech_tts_config(
            {
                "voice_id": "pt-BR-FranciscaNeural",
                "model_id": "deployment-42",
            },
            environ={
                "AZURE_SPEECH_KEY": "key-123",
                "AZURE_SPEECH_REGION": "brazilsouth",
                "AZURE_SPEECH_ENDPOINT": "https://speech.example.cognitiveservices.azure.com/",
            },
        )

        self.assertEqual(missing, [])
        self.assertEqual(config["deployment_id"], "deployment-42")
        self.assertEqual(
            config["speech_endpoint"],
            "https://speech.example.cognitiveservices.azure.com/voice/cognitiveservices/v1",
        )
        self.assertEqual(config["speech_region"], "brazilsouth")

    def test_resolve_accepts_host_alias_and_auth_token(self) -> None:
        config, missing = resolve_azure_speech_tts_config(
            {},
            environ={
                "AZURE_SPEECH_AUTH_TOKEN": "token-123",
                "AZURE_SPEECH_HOST": "https://speech.example.cognitiveservices.azure.com/",
                "AZURE_SPEECH_VOICE": "pt-BR-FranciscaNeural",
            },
        )

        self.assertEqual(missing, [])
        self.assertIsNone(config["speech_key"])
        self.assertEqual(config["speech_auth_token"], "token-123")
        self.assertEqual(
            config["speech_endpoint"],
            "https://speech.example.cognitiveservices.azure.com/tts/cognitiveservices/v1",
        )

    def test_resolve_keeps_full_endpoint_path(self) -> None:
        config, missing = resolve_azure_speech_tts_config(
            {},
            environ={
                "AZURE_SPEECH_KEY": "key-123",
                "AZURE_SPEECH_ENDPOINT": "https://speech.example.cognitiveservices.azure.com/tts/cognitiveservices/v1",
                "AZURE_SPEECH_VOICE": "pt-BR-FranciscaNeural",
            },
        )

        self.assertEqual(missing, [])
        self.assertEqual(
            config["speech_endpoint"],
            "https://speech.example.cognitiveservices.azure.com/tts/cognitiveservices/v1",
        )

    def test_resolve_reports_missing_required_settings(self) -> None:
        config, missing = resolve_azure_speech_tts_config({}, environ={})

        self.assertEqual(config, {})
        self.assertEqual(
            missing,
            [
                "AZURE_SPEECH_VOICE",
                "AZURE_SPEECH_ENDPOINT|AZURE_SPEECH_HOST|AZURE_SPEECH_REGION",
                "AZURE_SPEECH_KEY|AZURE_SPEECH_AUTH_TOKEN",
            ],
        )


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