# -*- coding: utf-8 -*-
"""
Analytics Management Package
===========================

Management package for the Adtlas Analytics module.
Contains Django management commands for analytics operations,
data processing, maintenance, and administrative tasks.

Management Commands:
- Data Processing Commands:
  - process_analytics_data: Process raw analytics data
  - aggregate_metrics: Aggregate performance metrics
  - generate_reports: Generate analytics reports
  - cleanup_old_data: Clean up old analytics data
  - migrate_analytics_data: Migrate analytics data

- Maintenance Commands:
  - rebuild_analytics_cache: Rebuild analytics cache
  - validate_analytics_data: Validate data integrity
  - optimize_analytics_db: Optimize database performance
  - backup_analytics_data: Backup analytics data
  - restore_analytics_data: Restore analytics data

- Integration Commands:
  - sync_sfr_data: Synchronize SFR analytics data
  - sync_bouygues_data: Synchronize Bouygues analytics data
  - import_external_data: Import external analytics data
  - export_analytics_data: Export analytics data

- Monitoring Commands:
  - check_analytics_health: Check system health
  - monitor_performance: Monitor performance metrics
  - alert_thresholds: Check alert thresholds
  - generate_health_report: Generate health reports

Key Features:
- Automated data processing
- Scheduled maintenance tasks
- Data validation and integrity checks
- Performance optimization
- External system integration
- Health monitoring and alerting
- Backup and recovery operations
- Report generation
- Cache management
- Database optimization

Command Categories:
- Processing: Data processing and transformation
- Maintenance: System maintenance and optimization
- Integration: External system integration
- Monitoring: Health monitoring and alerting
- Reporting: Report generation and export
- Utilities: General utility commands

Usage Examples:
- python manage.py process_analytics_data --date=2024-01-01
- python manage.py aggregate_metrics --period=daily
- python manage.py generate_reports --type=performance
- python manage.py cleanup_old_data --days=90
- python manage.py sync_sfr_data --incremental

Scheduling:
- Commands can be scheduled using cron or Celery
- Recommended scheduling for regular maintenance
- Performance monitoring commands
- Data processing pipelines
- Report generation workflows

Configuration:
- Command-specific settings
- Processing parameters
- Integration credentials
- Performance thresholds
- Notification settings

Author: Adtlas Development Team
Version: 1.0.0
Last Updated: 2024
"""

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

# Package metadata
__version__ = '1.0.0'
__author__ = 'Adtlas Development Team'
__description__ = 'Analytics management commands for Adtlas platform'

# Command categories for organization
COMMAND_CATEGORIES = {
    'processing': [
        'process_analytics_data',
        'aggregate_metrics',
        'generate_reports',
        'cleanup_old_data',
        'migrate_analytics_data'
    ],
    'maintenance': [
        'rebuild_analytics_cache',
        'validate_analytics_data',
        'optimize_analytics_db',
        'backup_analytics_data',
        'restore_analytics_data'
    ],
    'integration': [
        'sync_sfr_data',
        'sync_bouygues_data',
        'import_external_data',
        'export_analytics_data'
    ],
    'monitoring': [
        'check_analytics_health',
        'monitor_performance',
        'alert_thresholds',
        'generate_health_report'
    ]
}

# Default command configurations
DEFAULT_CONFIGS = {
    'batch_size': 1000,
    'timeout': 3600,  # 1 hour
    'retry_attempts': 3,
    'log_level': 'INFO',
    'dry_run': False,
    'verbose': False
}

# Command scheduling recommendations
SCHEDULE_RECOMMENDATIONS = {
    'process_analytics_data': 'Every 15 minutes',
    'aggregate_metrics': 'Hourly',
    'generate_reports': 'Daily at 2 AM',
    'cleanup_old_data': 'Weekly on Sunday',
    'sync_sfr_data': 'Every 30 minutes',
    'sync_bouygues_data': 'Every 30 minutes',
    'check_analytics_health': 'Every 5 minutes',
    'monitor_performance': 'Every 10 minutes',
    'backup_analytics_data': 'Daily at 3 AM'
}

# Performance thresholds for monitoring
PERFORMANCE_THRESHOLDS = {
    'response_time_warning': 1.0,  # seconds
    'response_time_critical': 5.0,  # seconds
    'error_rate_warning': 0.05,  # 5%
    'error_rate_critical': 0.10,  # 10%
    'memory_usage_warning': 0.80,  # 80%
    'memory_usage_critical': 0.90,  # 90%
    'disk_usage_warning': 0.85,  # 85%
    'disk_usage_critical': 0.95  # 95%
}

# Data retention policies
RETENTION_POLICIES = {
    'raw_analytics_data': 90,  # days
    'aggregated_metrics': 365,  # days
    'performance_logs': 30,  # days
    'error_logs': 60,  # days
    'audit_logs': 730,  # days (2 years)
    'backup_files': 30  # days
}

# Export formats supported
EXPORT_FORMATS = [
    'csv',
    'json',
    'xlsx',
    'pdf',
    'xml'
]

# Integration endpoints
INTEGRATION_ENDPOINTS = {
    'sfr': {
        'base_url': 'https://api.sfr.fr',
        'timeout': 30,
        'retry_attempts': 3
    },
    'bouygues': {
        'base_url': 'https://api.bouyguestelecom.fr',
        'timeout': 30,
        'retry_attempts': 3
    }
}

# Notification settings
NOTIFICATION_SETTINGS = {
    'email_enabled': True,
    'slack_enabled': False,
    'webhook_enabled': False,
    'alert_levels': ['WARNING', 'ERROR', 'CRITICAL'],
    'notification_throttle': 300  # seconds
}


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 with command information
    """
    # Find command category
    category = None
    for cat, commands in COMMAND_CATEGORIES.items():
        if command_name in commands:
            category = cat
            break
    
    return {
        'name': command_name,
        'category': category,
        'schedule': SCHEDULE_RECOMMENDATIONS.get(command_name),
        'config': DEFAULT_CONFIGS.copy()
    }


def get_commands_by_category(category: str) -> list:
    """
    Get list of commands in a specific category.
    
    Args:
        category: Command category name
        
    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_retention_policy(data_type: str) -> int:
    """
    Get retention policy for a specific data type.
    
    Args:
        data_type: Type of data
        
    Returns:
        Retention period in days
    """
    return RETENTION_POLICIES.get(data_type, 30)  # Default 30 days


def get_performance_threshold(metric: str) -> dict:
    """
    Get performance thresholds for a specific metric.
    
    Args:
        metric: Performance metric name
        
    Returns:
        Dictionary with warning and critical thresholds
    """
    warning_key = f"{metric}_warning"
    critical_key = f"{metric}_critical"
    
    return {
        'warning': PERFORMANCE_THRESHOLDS.get(warning_key),
        'critical': PERFORMANCE_THRESHOLDS.get(critical_key)
    }


# Package exports
__all__ = [
    'COMMAND_CATEGORIES',
    'DEFAULT_CONFIGS',
    'SCHEDULE_RECOMMENDATIONS',
    'PERFORMANCE_THRESHOLDS',
    'RETENTION_POLICIES',
    'EXPORT_FORMATS',
    'INTEGRATION_ENDPOINTS',
    'NOTIFICATION_SETTINGS',
    'get_command_info',
    'get_commands_by_category',
    'get_all_commands',
    'get_retention_policy',
    'get_performance_threshold'
]