<?php
/**
 * Real-time Email Debug Monitor
 * 
 * This script monitors the debug log for email notifications in real-time.
 * Run this in terminal: php debug-email-log.php
 */

// Check if running from command line
if (php_sapi_name() !== 'cli') {
    die('This script must be run from command line.');
}

$debug_log = __DIR__ . '/../../../debug.log';

if (!file_exists($debug_log)) {
    echo "❌ Debug log not found at: $debug_log\n";
    echo "💡 Enable WP_DEBUG_LOG in wp-config.php first\n";
    exit(1);
}

echo "📧 Monitoring email notifications...\n";
echo "📂 Watching: $debug_log\n";
echo "🔄 Press Ctrl+C to stop\n\n";

// Get current file size
$last_size = filesize($debug_log);

while (true) {
    clearstatcache();
    $current_size = filesize($debug_log);
    
    if ($current_size > $last_size) {
        // File has grown, read new content
        $handle = fopen($debug_log, 'r');
        fseek($handle, $last_size);
        
        while (($line = fgets($handle)) !== false) {
            // Only show email-related logs
            if (strpos($line, 'Content notification') !== false || 
                strpos($line, 'Content Manager') !== false ||
                strpos($line, 'wp_mail') !== false) {
                
                $timestamp = date('H:i:s');
                echo "[$timestamp] " . trim($line) . "\n";
            }
        }
        
        fclose($handle);
        $last_size = $current_size;
    }
    
    sleep(1);
}
?>
