"""
Application configuration management
"""
import os
from datetime import timedelta

class Config:
    """Base configuration"""
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'dev-secret-key-change-in-production'
    
    # Database
    DATABASE_URL = os.environ.get('DATABASE_URL') or 'sqlite:///data/enhanced_monitoring.db'
    
    # File Upload Settings
    UPLOAD_FOLDER = 'data/uploads'
    MAX_CONTENT_LENGTH = 50 * 1024 * 1024  # 50MB max file size
    ALLOWED_EXTENSIONS = {'txt', 'log', 'conf', 'cfg', 'json', 'yml', 'yaml'}
    
    # Message Compression Settings
    ENABLE_MESSAGE_COMPRESSION = True
    COMPRESSION_THRESHOLD = 100  # Compress messages longer than 100 chars
    MAX_TEMPLATE_CACHE_SIZE = 1000
    
    # Ansible Settings
    ANSIBLE_INVENTORY_PATH = 'ansible/inventory/dynamic_inventory.yaml'
    ANSIBLE_PLAYBOOK_PATH = 'ansible/playbooks'
    SSH_KEY_PATH = os.path.expanduser('~/.ssh/ansible_key')
    
    # SSH Settings
    SSH_USERNAME = 'pi'
    SSH_PORT = 22
    SSH_TIMEOUT = 10
    
    # Performance Settings
    DATABASE_POOL_SIZE = 20
    DATABASE_POOL_TIMEOUT = 30
    LOG_RETENTION_DAYS = 90
    
    # Security Settings
    BCRYPT_LOG_ROUNDS = 12
    SESSION_COOKIE_SECURE = False  # Set to True in production with HTTPS
    SESSION_COOKIE_HTTPONLY = True
    SESSION_COOKIE_SAMESITE = 'Lax'
    PERMANENT_SESSION_LIFETIME = timedelta(days=7)
    
    # API Settings
    API_RATE_LIMIT = '1000 per hour'
    API_PAGINATION_DEFAULT = 100
    API_PAGINATION_MAX = 1000

class DevelopmentConfig(Config):
    """Development configuration"""
    DEBUG = True
    TESTING = False
    
    # More verbose logging in development
    LOG_LEVEL = 'DEBUG'
    
    # Disable some security features for development
    SESSION_COOKIE_SECURE = False
    WTF_CSRF_ENABLED = False  # Disable CSRF for API testing

class ProductionConfig(Config):
    """Production configuration"""
    DEBUG = False
    TESTING = False
    
    # Security settings for production
    SESSION_COOKIE_SECURE = True
    WTF_CSRF_ENABLED = True
    
    # Production database (if using PostgreSQL)
    DATABASE_URL = os.environ.get('DATABASE_URL') or 'sqlite:///data/enhanced_monitoring.db'
    
    # Logging
    LOG_LEVEL = 'INFO'
    LOG_FILE = 'logs/app.log'
    
    # Performance
    DATABASE_POOL_SIZE = 50

class TestingConfig(Config):
    """Testing configuration"""
    TESTING = True
    DEBUG = True
    
    # Use in-memory database for testing
    DATABASE_URL = 'sqlite:///:memory:'
    
    # Disable CSRF for testing
    WTF_CSRF_ENABLED = False

# Configuration dictionary
config = {
    'development': DevelopmentConfig,
    'production': ProductionConfig,
    'testing': TestingConfig,
    'default': DevelopmentConfig
}

def get_config(config_name=None):
    """Get configuration class"""
    if config_name is None:
        config_name = os.environ.get('FLASK_ENV', 'default')
    
    return config.get(config_name, config['default'])