"""
Application settings and configuration management.
Uses Pydantic Settings for type-safe configuration with environment variable support.
"""
from pydantic_settings import BaseSettings
from typing import List, Optional
from functools import lru_cache


class Settings(BaseSettings):
    """Application configuration settings."""
    
    # Application
    app_name: str = "ProfytAI Compliance Platform"
    app_version: str = "1.0.0"
    debug: bool = False
    environment: str = "development"  # development, staging, production
    
    # Server
    host: str = "0.0.0.0"
    port: int = 4402
    
    # Database - Neo4j
    neo4j_uri: str = "bolt://localhost:7687"
    neo4j_user: str = "neo4j"
    neo4j_password: str = "password"
    neo4j_database: str = "neo4j"
    
    # Security
    secret_key: str = "your-secret-key-change-in-production"  # Should be in .env
    algorithm: str = "HS256"
    access_token_expire_minutes: int = 30
    refresh_token_expire_days: int = 7
    cors_origins: List[str] = ["http://localhost:8080", "http://localhost:3000"]
    
    # AI/ML - Ollama
    ollama_base_url: str = "http://localhost:11434"
    ollama_model: str = "qwen2.5:3b"
    embedding_model: str = "embeddinggemma:300m"
    ollama_timeout: int = 300  # 5 minutes
    
    # Storage
    upload_dir: str = "./assets/data/uploads"
    max_upload_size: int = 10 * 1024 * 1024  # 10MB
    allowed_file_extensions: List[str] = [".pdf", ".docx", ".txt"]
    
    # Rate Limiting
    rate_limit_per_minute: int = 60
    rate_limit_per_hour: int = 1000
    
    # Logging
    log_level: str = "INFO"
    log_file: str = "./logs/app.log"
    log_format: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    
    # Workflow
    max_workflow_items: int = 100
    workflow_cleanup_days: int = 90  # Auto-cleanup old workflows
    
    class Config:
        env_file = ".env"
        env_file_encoding = "utf-8"
        case_sensitive = False
        env_nested_delimiter = "__"


@lru_cache()
def get_settings() -> Settings:
    """Get cached settings instance."""
    return Settings()


# Global settings instance
settings = get_settings()
