/**
 * Descomplicar® Crescimento Digital
 * https://descomplicar.pt
 */

<?php
/**
 * Redis Configuration for Desk-Moloni v3.0
 * 
 * Configures Redis connection for queue processing and caching
 * Supports connection pooling, failover, and performance optimization
 * 
 * @package DeskMoloni\Config
 * @author  Descomplicar.pt
 * @version 3.0.0
 */

defined('BASEPATH') or exit('No direct script access allowed');

// Redis connection configuration
$config['redis'] = [
    
    // Primary Redis server configuration
    'default' => [
        'host' => get_option('desk_moloni_redis_host') ?: '127.0.0.1',
        'port' => (int)(get_option('desk_moloni_redis_port') ?: 6379),
        'password' => get_option('desk_moloni_redis_password') ?: null,
        'database' => (int)(get_option('desk_moloni_redis_database') ?: 0),
        'timeout' => 5.0,
        'read_timeout' => 60.0,
        'persistent' => true,
        'prefix' => 'desk_moloni:',
        'serializer' => 'php', // php, igbinary, json
        'compression' => 'none', // none, lzf, zstd, lz4
    ],
    
    // Queue-specific configuration
    'queue' => [
        'host' => get_option('desk_moloni_redis_queue_host') ?: '127.0.0.1',
        'port' => (int)(get_option('desk_moloni_redis_queue_port') ?: 6379),
        'password' => get_option('desk_moloni_redis_queue_password') ?: null,
        'database' => (int)(get_option('desk_moloni_redis_queue_database') ?: 1),
        'timeout' => 3.0,
        'read_timeout' => 30.0,
        'persistent' => true,
        'prefix' => 'desk_moloni:queue:',
        'serializer' => 'php',
        'compression' => 'none',
    ],
    
    // Cache-specific configuration 
    'cache' => [
        'host' => get_option('desk_moloni_redis_cache_host') ?: '127.0.0.1',
        'port' => (int)(get_option('desk_moloni_redis_cache_port') ?: 6379),
        'password' => get_option('desk_moloni_redis_cache_password') ?: null,
        'database' => (int)(get_option('desk_moloni_redis_cache_database') ?: 2),
        'timeout' => 2.0,
        'read_timeout' => 10.0,
        'persistent' => true,
        'prefix' => 'desk_moloni:cache:',
        'serializer' => 'php',
        'compression' => 'lzf', // Enable compression for cache
    ],
];

// Queue processing configuration
$config['queue_settings'] = [
    
    // Queue names and priorities
    'queues' => [
        'high_priority' => [
            'name' => 'desk_moloni:queue:high',
            'priority' => 1,
            'max_concurrent' => 5,
            'timeout' => 300, // 5 minutes
        ],
        'normal_priority' => [
            'name' => 'desk_moloni:queue:normal', 
            'priority' => 5,
            'max_concurrent' => 3,
            'timeout' => 600, // 10 minutes
        ],
        'low_priority' => [
            'name' => 'desk_moloni:queue:low',
            'priority' => 9,
            'max_concurrent' => 2,
            'timeout' => 1800, // 30 minutes
        ],
    ],
    
    // Worker configuration
    'worker' => [
        'max_jobs_per_worker' => 1000,
        'max_execution_time' => 3600, // 1 hour
        'memory_limit' => '256M',
        'sleep_duration' => 1, // seconds between queue checks
        'max_retry_attempts' => 3,
        'retry_delay' => 60, // seconds before retry
        'failed_job_ttl' => 86400, // 24 hours
    ],
    
    // Rate limiting configuration
    'rate_limiting' => [
        'moloni_api' => [
            'requests_per_minute' => 60,
            'burst_size' => 10,
            'window_size' => 60,
        ],
        'perfex_database' => [
            'requests_per_minute' => 300,
            'burst_size' => 50,
            'window_size' => 60,
        ],
    ],
    
    // Monitoring and alerts
    'monitoring' => [
        'queue_size_alert_threshold' => 100,
        'processing_time_alert_threshold' => 300, // 5 minutes
        'failed_jobs_alert_threshold' => 10,
        'worker_health_check_interval' => 300, // 5 minutes
    ],
];

// Caching configuration
$config['cache_settings'] = [
    
    // TTL settings (in seconds)
    'ttl' => [
        'moloni_company_data' => 3600, // 1 hour
        'moloni_products' => 1800, // 30 minutes
        'moloni_customers' => 900, // 15 minutes
        'entity_mappings' => 900, // 15 minutes
        'oauth_token_info' => 300, // 5 minutes
        'api_response_cache' => 300, // 5 minutes
        'perfex_data_cache' => 300, // 5 minutes
    ],
    
    // Cache keys prefixes
    'prefixes' => [
        'moloni_api' => 'moloni:api:',
        'perfex_data' => 'perfex:data:',
        'mappings' => 'mappings:',
        'sessions' => 'sessions:',
        'locks' => 'locks:',
    ],
    
    // Cache strategies
    'strategies' => [
        'write_through' => true, // Write to cache and storage simultaneously
        'write_behind' => false, // Write to cache first, storage later
        'cache_aside' => true, // Manual cache management
        'refresh_ahead' => true, // Refresh cache before expiration
    ],
];

// Connection pool configuration
$config['connection_pool'] = [
    'enabled' => true,
    'min_connections' => 2,
    'max_connections' => 10,
    'connection_timeout' => 5.0,
    'idle_timeout' => 300, // 5 minutes
    'max_retries' => 3,
    'retry_delay' => 1000, // milliseconds
];

// Failover configuration
$config['failover'] = [
    'enabled' => false, // Enable when multiple Redis instances available
    'sentinel' => [
        'enabled' => false,
        'hosts' => [
            ['host' => '127.0.0.1', 'port' => 26379],
        ],
        'master_name' => 'desk-moloni-master',
        'timeout' => 2.0,
    ],
    'cluster' => [
        'enabled' => false,
        'hosts' => [
            ['host' => '127.0.0.1', 'port' => 7000],
            ['host' => '127.0.0.1', 'port' => 7001],
            ['host' => '127.0.0.1', 'port' => 7002],
        ],
        'timeout' => 2.0,
        'read_timeout' => 10.0,
    ],
];

// Performance optimization
$config['optimization'] = [
    'pipeline_enabled' => true,
    'pipeline_batch_size' => 100,
    'lua_scripts_enabled' => true,
    'compression_enabled' => true,
    'serialization_optimized' => true,
];

// Security configuration
$config['security'] = [
    'tls_enabled' => false, // Enable for production
    'cert_file' => null,
    'key_file' => null,
    'ca_file' => null,
    'verify_peer' => true,
    'verify_peer_name' => true,
    'allow_self_signed' => false,
];

// Development and debugging
$config['development'] = [
    'debug_mode' => ENVIRONMENT === 'development',
    'log_queries' => ENVIRONMENT === 'development',
    'profiling_enabled' => false,
    'slow_query_threshold' => 100, // milliseconds
    'connection_logging' => ENVIRONMENT === 'development',
];

// Default Redis configuration if options not set
if (!get_option('desk_moloni_redis_host')) {
    $default_redis_options = [
        'desk_moloni_redis_host' => '127.0.0.1',
        'desk_moloni_redis_port' => '6379',
        'desk_moloni_redis_password' => '',
        'desk_moloni_redis_database' => '0',
        'desk_moloni_redis_queue_database' => '1',
        'desk_moloni_redis_cache_database' => '2',
    ];
    
    foreach ($default_redis_options as $key => $value) {
        if (get_option($key) === false) {
            add_option($key, $value);
        }
    }
}