import logging
from typing import Optional
from functools import lru_cache
from pydantic import SecretStr
from pydantic_settings import BaseSettings, SettingsConfigDict

def setup_logging():
    """Configure logging for the application."""
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S'
    )

class Settings(BaseSettings):
    """Application settings that are loaded from environment variables."""
    
    # API Keys
    anthropic_api_key: SecretStr
    unsplash_api_key: SecretStr
    
    # Optional configuration
    output_directory: str = "output"
    media_directory: str = "media"
    default_deck_name: str = "German Vocabulary"
    
    # Model configuration (optional, can be expanded)
    anthropic_model: str = "claude-3-opus-20240229"
    max_tokens: int = 1000
    temperature: float = 0

    # Image configuration
    image_max_width: int = 800
    image_max_height: int = 600
    image_quality: int = 85
    
    # File handling
    file_encoding: str = "utf-8"
    
    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding="utf-8",
        case_sensitive=False,
        # This allows for both ANTHROPIC_API_KEY and anthropic_api_key in .env
        env_nested_delimiter='__'
    )

@lru_cache
def get_settings() -> Settings:
    """
    Get settings instance with caching.
    
    Returns:
        Settings: Application settings instance
    """
    setup_logging()
    return Settings()
