"""Configuration for the voice server."""
import os
from pathlib import Path
from typing import Dict, Any
from pydantic_settings import BaseSettings
from pydantic import Field


class VoiceServerConfig(BaseSettings):
    """Voice server configuration."""
    
    host: str = Field(default="127.0.0.1", description="Server host")
    port: int = Field(default=8880, description="Server port")
    
    # Voice model configuration
    default_voice: str = Field(default="ryan", description="Default voice model")
    voices_dir: Path = Field(
        default_factory=lambda: Path.home() / ".local/share/piper-voices",
        description="Directory containing voice models"
    )
    
    # Available voice models
    available_voices: Dict[str, Dict[str, Any]] = Field(
        default_factory=lambda: {
            "ryan": {
                "model_file": "en_US-ryan-medium.onnx",
                "config_file": "en_US-ryan-medium.onnx.json",
                "language": "en-US",
                "gender": "male",
                "description": "Professional US male voice"
            },
            "alan": {
                "model_file": "en_GB-alan-medium.onnx", 
                "config_file": "en_GB-alan-medium.onnx.json",
                "language": "en-GB",
                "gender": "male",
                "description": "Sophisticated British male voice"
            },
            "lessac": {
                "model_file": "en_US-lessac-medium.onnx",
                "config_file": "en_US-lessac-medium.onnx.json", 
                "language": "en-US",
                "gender": "female",
                "description": "Natural US female voice"
            }
        }
    )
    
    # Piper TTS configuration
    piper_executable: str = Field(default="piper-tts", description="Piper TTS executable")
    audio_format: str = Field(default="wav", description="Audio output format")
    
    # Server configuration
    log_level: str = Field(default="info", description="Logging level")
    
    class Config:
        env_prefix = "VOICE_SERVER_"
        env_file = ".env"
    
    def get_voice_model_path(self, voice_name: str = None) -> Path:
        """Get the full path to a voice model file."""
        voice_name = voice_name or self.default_voice
        if voice_name not in self.available_voices:
            raise ValueError(f"Voice '{voice_name}' not found in available voices")
        
        voice_config = self.available_voices[voice_name]
        return self.voices_dir / voice_config["model_file"]
    
    def get_voice_config_path(self, voice_name: str = None) -> Path:
        """Get the full path to a voice config file."""
        voice_name = voice_name or self.default_voice
        if voice_name not in self.available_voices:
            raise ValueError(f"Voice '{voice_name}' not found in available voices")
        
        voice_config = self.available_voices[voice_name]
        return self.voices_dir / voice_config["config_file"]
    
    def validate_voice_files(self, voice_name: str = None) -> bool:
        """Check if voice model files exist."""
        voice_name = voice_name or self.default_voice
        try:
            model_path = self.get_voice_model_path(voice_name)
            config_path = self.get_voice_config_path(voice_name)
            return model_path.exists() and config_path.exists()
        except ValueError:
            return False


# Global configuration instance
config = VoiceServerConfig()