"""Core Management Commands Package

Django management commands for the Adtlas Core module.
Provides essential system management and maintenance commands.

Available Commands:

System Management:
- system_health: Check system health status
- cleanup_logs: Clean up old log files and audit records
- backup_system: Create system backups
- restore_system: Restore system from backup
- migrate_data: Data migration utilities

Configuration Management:
- update_config: Update system configuration
- export_config: Export system configuration
- import_config: Import system configuration
- validate_config: Validate system configuration

Maintenance Commands:
- maintenance_mode: Enable/disable maintenance mode
- clear_cache: Clear system cache
- optimize_db: Optimize database performance
- check_permissions: Check and fix permissions

Monitoring Commands:
- system_status: Display system status
- performance_report: Generate performance report
- security_audit: Perform security audit
- health_check: Comprehensive health check

Utility Commands:
- generate_secret_key: Generate new secret key
- create_test_data: Create test data for development
- export_data: Export system data
- import_data: Import system data

Usage:
    python manage.py <command_name> [options]
    
    Examples:
    python manage.py system_health
    python manage.py cleanup_logs --days 30
    python manage.py maintenance_mode --enable
    python manage.py backup_system --output /path/to/backup

Scheduling:
    Commands can be scheduled using cron or Celery:
    
    # Daily cleanup at 2 AM
    0 2 * * * cd /path/to/project && python manage.py cleanup_logs
    
    # Weekly backup on Sunday at 3 AM
    0 3 * * 0 cd /path/to/project && python manage.py backup_system
    
    # Hourly health check
    0 * * * * cd /path/to/project && python manage.py system_health

Configuration:
    Commands use configuration from Django settings:
    
    # Core management settings
    CORE_MANAGEMENT_CONFIG = {
        'backup_directory': '/var/backups/adtlas',
        'log_retention_days': 30,
        'health_check_timeout': 30,
        'maintenance_template': 'core/maintenance.html',
    }

Logging:
    All commands provide comprehensive logging:
    - Command execution logs
    - Error handling and reporting
    - Performance metrics
    - Audit trails

Security:
    Commands implement security best practices:
    - Permission checks
    - Input validation
    - Secure file handling
    - Audit logging
"""

# Management package initialization
# This file makes the management directory a Python package
# and provides comprehensive documentation for all core management commands.

__version__ = '1.0.0'
__author__ = 'Adtlas Development Team'
__description__ = 'Management commands for Adtlas Core platform'

# Command categories for organization
COMMAND_CATEGORIES = {
    'system': [
        'system_health',
        'cleanup_logs', 
        'backup_system',
        'restore_system',
        'migrate_data',
    ],
    'configuration': [
        'update_config',
        'export_config',
        'import_config', 
        'validate_config',
    ],
    'maintenance': [
        'maintenance_mode',
        'clear_cache',
        'optimize_db',
        'check_permissions',
    ],
    'monitoring': [
        'system_status',
        'performance_report',
        'security_audit',
        'health_check',
    ],
    'utilities': [
        'generate_secret_key',
        'create_test_data',
        'export_data',
        'import_data',
    ],
}

# Command metadata
COMMAND_METADATA = {
    'system_health': {
        'description': 'Check system health status',
        'category': 'system',
        'priority': 'high',
        'frequency': 'hourly',
    },
    'cleanup_logs': {
        'description': 'Clean up old log files and audit records',
        'category': 'system',
        'priority': 'medium',
        'frequency': 'daily',
    },
    'backup_system': {
        'description': 'Create system backups',
        'category': 'system',
        'priority': 'high',
        'frequency': 'daily',
    },
    'maintenance_mode': {
        'description': 'Enable/disable maintenance mode',
        'category': 'maintenance',
        'priority': 'high',
        'frequency': 'manual',
    },
    'clear_cache': {
        'description': 'Clear system cache',
        'category': 'maintenance',
        'priority': 'medium',
        'frequency': 'manual',
    },
}


def get_command_info(command_name: str) -> dict:
    """
    Get information about a specific management command.
    
    Args:
        command_name: Name of the management command
        
    Returns:
        Dictionary containing command information
    """
    return COMMAND_METADATA.get(command_name, {
        'description': 'No description available',
        'category': 'unknown',
        'priority': 'low',
        'frequency': 'manual',
    })


def get_commands_by_category(category: str) -> list:
    """
    Get list of commands in a specific category.
    
    Args:
        category: Command category
        
    Returns:
        List of command names
    """
    return COMMAND_CATEGORIES.get(category, [])


def get_all_commands() -> list:
    """
    Get list of all available management commands.
    
    Returns:
        List of all command names
    """
    all_commands = []
    for commands in COMMAND_CATEGORIES.values():
        all_commands.extend(commands)
    return all_commands


def get_command_help() -> str:
    """
    Get formatted help text for all commands.
    
    Returns:
        Formatted help text
    """
    help_text = "Available Core Management Commands:\n\n"
    
    for category, commands in COMMAND_CATEGORIES.items():
        help_text += f"{category.title()} Commands:\n"
        for command in commands:
            info = get_command_info(command)
            help_text += f"  {command:<20} - {info['description']}\n"
        help_text += "\n"
    
    return help_text


# Export public interface
__all__ = [
    'COMMAND_CATEGORIES',
    'COMMAND_METADATA',
    'get_command_info',
    'get_commands_by_category',
    'get_all_commands',
    'get_command_help',
]