<?php
/*******************************************************************************************************************
| Software Name        : EasyStream
| Software Description : High End YouTube Clone Script with Videos, Shorts, Streams, Images, Audio, Documents, Blogs
| Software Author      : (c) Sami Ahmed
|*******************************************************************************************************************
|
|*******************************************************************************************************************
| This source file is subject to the EasyStream Proprietary License Agreement.
| 
| By using this software, you acknowledge having read this Agreement and agree to be bound thereby.
|*******************************************************************************************************************
| Copyright (c) 2025 Sami Ahmed. All rights reserved.
|*******************************************************************************************************************/

define('_ISVALID', true);

// Include core configuration
require_once __DIR__ . '/../config.core.php';

/**
 * Queue Monitoring and Management Tool
 */
class QueueMonitor
{
    private $queueManager;
    private $logger;
    
    public function __construct()
    {
        $this->queueManager = new VQueueManager();
        $this->logger = VLogger::getInstance();
    }
    
    /**
     * Display queue dashboard
     */
    public function showDashboard()
    {
        echo "\n" . str_repeat("=", 80) . "\n";
        echo "                    EASYSTREAM QUEUE MONITOR\n";
        echo str_repeat("=", 80) . "\n\n";
        
        // Health check
        $health = $this->queueManager->healthCheck();
        $this->displayHealthStatus($health);
        
        // Queue statistics
        $stats = $this->queueManager->getQueueStatistics();
        $this->displayQueueStats($stats);
        
        // Failed jobs
        $failedJobs = $this->queueManager->getFailedJobs(10);
        $this->displayFailedJobs($failedJobs);
        
        echo "\n" . str_repeat("=", 80) . "\n";
    }
    
    /**
     * Display health status
     */
    private function displayHealthStatus($health)
    {
        echo "SYSTEM HEALTH\n";
        echo str_repeat("-", 40) . "\n";
        
        $statusColor = $this->getStatusColor($health['status']);
        echo "Status: {$statusColor}{$health['status']}\033[0m\n";
        echo "Backend: {$health['backend']}\n";
        
        if (!empty($health['issues'])) {
            echo "Issues:\n";
            foreach ($health['issues'] as $issue) {
                echo "  - \033[33m{$issue}\033[0m\n";
            }
        }
        
        echo "\n";
    }
    
    /**
     * Display queue statistics
     */
    private function displayQueueStats($stats)
    {
        echo "QUEUE STATISTICS\n";
        echo str_repeat("-", 40) . "\n";
        
        if (empty($stats)) {
            echo "No queue data available\n\n";
            return;
        }
        
        // Table header
        printf("%-15s %-10s %-10s %-10s %-10s\n", 
               "Queue", "Pending", "Processing", "Completed", "Failed");
        echo str_repeat("-", 65) . "\n";
        
        foreach ($stats as $queue => $data) {
            printf("%-15s %-10d %-10d %-10d %-10d\n",
                   $queue,
                   $data['pending'] ?? 0,
                   $data['processing'] ?? 0,
                   $data['completed'] ?? 0,
                   $data['failed'] ?? 0
            );
        }
        
        echo "\n";
    }
    
    /**
     * Display failed jobs
     */
    private function displayFailedJobs($failedJobs)
    {
        echo "RECENT FAILED JOBS\n";
        echo str_repeat("-", 40) . "\n";
        
        if (empty($failedJobs)) {
            echo "No failed jobs found\n\n";
            return;
        }
        
        foreach ($failedJobs as $job) {
            echo "Job ID: {$job['id']}\n";
            echo "Class: {$job['class']}\n";
            echo "Queue: {$job['queue']}\n";
            echo "Attempts: {$job['attempts']}/{$job['max_attempts']}\n";
            echo "Error: " . substr($job['error_message'] ?? 'Unknown error', 0, 100) . "\n";
            echo "Failed: {$job['updated_at']}\n";
            echo str_repeat("-", 40) . "\n";
        }
        
        echo "\n";
    }
    
    /**
     * Clean up old jobs
     */
    public function cleanup($hours = 24)
    {
        echo "Cleaning up jobs older than {$hours} hours...\n";
        
        $deletedCount = $this->queueManager->cleanupOldJobs($hours);
        
        echo "Cleaned up {$deletedCount} old jobs\n";
    }
    
    /**
     * Retry failed jobs
     */
    public function retryFailedJobs($limit = 10)
    {
        echo "Retrying failed jobs (limit: {$limit})...\n";
        
        $failedJobs = $this->queueManager->getFailedJobs($limit);
        $retryCount = 0;
        
        foreach ($failedJobs as $job) {
            if ($this->queueManager->retryFailedJob($job['id'])) {
                echo "Retried job: {$job['id']} ({$job['class']})\n";
                $retryCount++;
            } else {
                echo "Failed to retry job: {$job['id']}\n";
            }
        }
        
        echo "Retried {$retryCount} jobs\n";
    }
    
    /**
     * Monitor queue in real-time
     */
    public function monitor($interval = 5)
    {
        echo "Starting real-time queue monitoring (refresh every {$interval}s)\n";
        echo "Press Ctrl+C to stop\n\n";
        
        while (true) {
            // Clear screen
            system('clear');
            
            $this->showDashboard();
            
            echo "Last updated: " . date('Y-m-d H:i:s') . "\n";
            echo "Refreshing in {$interval} seconds...\n";
            
            sleep($interval);
        }
    }
    
    /**
     * Get status color for terminal output
     */
    private function getStatusColor($status)
    {
        switch ($status) {
            case 'healthy':
                return "\033[32m"; // Green
            case 'warning':
                return "\033[33m"; // Yellow
            case 'unhealthy':
                return "\033[31m"; // Red
            default:
                return "\033[0m";  // Default
        }
    }
    
    /**
     * Show help information
     */
    public function showHelp()
    {
        echo "\nEasyStream Queue Monitor\n";
        echo str_repeat("=", 30) . "\n\n";
        echo "Usage: php queue-monitor.php [command] [options]\n\n";
        echo "Commands:\n";
        echo "  dashboard              Show queue dashboard (default)\n";
        echo "  monitor [interval]     Real-time monitoring (default: 5s)\n";
        echo "  cleanup [hours]        Clean up old jobs (default: 24h)\n";
        echo "  retry [limit]          Retry failed jobs (default: 10)\n";
        echo "  help                   Show this help\n\n";
        echo "Examples:\n";
        echo "  php queue-monitor.php\n";
        echo "  php queue-monitor.php monitor 10\n";
        echo "  php queue-monitor.php cleanup 48\n";
        echo "  php queue-monitor.php retry 5\n\n";
    }
}

// CLI execution
if (php_sapi_name() === 'cli') {
    $monitor = new QueueMonitor();
    
    $command = $argv[1] ?? 'dashboard';
    
    switch ($command) {
        case 'dashboard':
            $monitor->showDashboard();
            break;
            
        case 'monitor':
            $interval = (int)($argv[2] ?? 5);
            $monitor->monitor($interval);
            break;
            
        case 'cleanup':
            $hours = (int)($argv[2] ?? 24);
            $monitor->cleanup($hours);
            break;
            
        case 'retry':
            $limit = (int)($argv[2] ?? 10);
            $monitor->retryFailedJobs($limit);
            break;
            
        case 'help':
        case '--help':
        case '-h':
            $monitor->showHelp();
            break;
            
        default:
            echo "Unknown command: {$command}\n";
            $monitor->showHelp();
            exit(1);
    }
}
?>