<?php
/**
 * Debug TEC Community Events Shortcode Issues
 * 
 * This script checks:
 * 1. TEC Community Events plugin activation
 * 2. Shortcode registration status
 * 3. PHP errors in shortcode execution
 * 4. Direct shortcode testing
 */

// WordPress bootstrap
define('WP_USE_THEMES', false);
require_once('../../../../wp-load.php');

echo "<h1>TEC Community Events Debug Report</h1>\n";
echo "<style>body { font-family: Arial, sans-serif; margin: 20px; } .pass { color: green; } .fail { color: red; } .warning { color: orange; } pre { background: #f5f5f5; padding: 10px; border-radius: 4px; }</style>\n";

// Check 1: TEC Plugin Status
echo "<h2>1. The Events Calendar Plugin Status</h2>\n";

$tec_active = is_plugin_active('the-events-calendar/the-events-calendar.php');
$tec_ce_active = is_plugin_active('the-events-calendar-community-events/tribe-community-events.php');

echo "<p><strong>The Events Calendar:</strong> " . ($tec_active ? '<span class="pass">✓ Active</span>' : '<span class="fail">✗ Not Active</span>') . "</p>\n";
echo "<p><strong>TEC Community Events:</strong> " . ($tec_ce_active ? '<span class="pass">✓ Active</span>' : '<span class="fail">✗ Not Active</span>') . "</p>\n";

// Check if TEC classes/functions exist
$tec_functions = [
    'tribe_community_events_init' => function_exists('tribe_community_events_init'),
    'tribe_is_community_edit_event_page' => function_exists('tribe_is_community_edit_event_page'),
    'tribe_community_events_list' => function_exists('tribe_community_events_list'),
];

echo "<h3>TEC Functions Available:</h3>\n";
foreach ($tec_functions as $func => $exists) {
    echo "<p><code>$func()</code>: " . ($exists ? '<span class="pass">✓ Available</span>' : '<span class="fail">✗ Not Available</span>') . "</p>\n";
}

// Check 2: Shortcode Registration
echo "<h2>2. Shortcode Registration Status</h2>\n";

global $shortcode_tags;

$hvac_shortcodes = [
    'hvac_create_event',
    'hvac_edit_event',
    'tribe_community_events'
];

foreach ($hvac_shortcodes as $shortcode) {
    $registered = shortcode_exists($shortcode);
    echo "<p><code>[$shortcode]</code>: " . ($registered ? '<span class="pass">✓ Registered</span>' : '<span class="fail">✗ Not Registered</span>') . "</p>\n";
    
    if ($registered) {
        $callback = $shortcode_tags[$shortcode];
        if (is_array($callback)) {
            echo "<p style='margin-left: 20px;'>Callback: <code>" . get_class($callback[0]) . "::" . $callback[1] . "()</code></p>\n";
        } else {
            echo "<p style='margin-left: 20px;'>Callback: <code>$callback</code></p>\n";
        }
    }
}

// Check 3: HVAC Plugin Classes
echo "<h2>3. HVAC Plugin Classes</h2>\n";

$hvac_classes = [
    'HVAC_Shortcodes',
    'HVAC_Edit_Event_Shortcode',
    'HVAC_Community_Events',
    'HVAC_Menu_System',
    'HVAC_Breadcrumbs'
];

foreach ($hvac_classes as $class) {
    $exists = class_exists($class);
    echo "<p><code>$class</code>: " . ($exists ? '<span class="pass">✓ Available</span>' : '<span class="fail">✗ Not Available</span>') . "</p>\n";
}

// Check 4: Current User Capabilities
echo "<h2>4. Current User Status</h2>\n";

if (is_user_logged_in()) {
    $user = wp_get_current_user();
    echo "<p><strong>Logged in as:</strong> {$user->user_login} (ID: {$user->ID})</p>\n";
    echo "<p><strong>Roles:</strong> " . implode(', ', $user->roles) . "</p>\n";
    
    $capabilities = [
        'hvac_trainer',
        'hvac_master_trainer',
        'edit_tribe_events',
        'manage_options'
    ];
    
    echo "<h3>Capabilities:</h3>\n";
    foreach ($capabilities as $cap) {
        $has_cap = current_user_can($cap);
        echo "<p><code>$cap</code>: " . ($has_cap ? '<span class="pass">✓ Has Permission</span>' : '<span class="fail">✗ No Permission</span>') . "</p>\n";
    }
} else {
    echo "<p><span class='warning'>⚠ Not logged in</span></p>\n";
}

// Check 5: Test Direct Shortcode Execution
echo "<h2>5. Direct Shortcode Testing</h2>\n";

if (shortcode_exists('tribe_community_events')) {
    echo "<h3>Testing [tribe_community_events] directly:</h3>\n";
    
    // Test basic shortcode
    ob_start();
    $basic_output = do_shortcode('[tribe_community_events]');
    $basic_errors = ob_get_clean();
    
    echo "<h4>Basic shortcode output:</h4>\n";
    if (!empty($basic_errors)) {
        echo "<div class='fail'><strong>PHP Errors/Warnings:</strong><pre>" . esc_html($basic_errors) . "</pre></div>\n";
    }
    echo "<div style='max-height: 200px; overflow: auto; border: 1px solid #ccc; padding: 10px;'>" . substr(esc_html($basic_output), 0, 1000) . (strlen($basic_output) > 1000 ? '...' : '') . "</div>\n";
    
    // Test with submission_form view
    echo "<h4>Testing with view='submission_form':</h4>\n";
    ob_start();
    $form_output = do_shortcode('[tribe_community_events view="submission_form"]');
    $form_errors = ob_get_clean();
    
    if (!empty($form_errors)) {
        echo "<div class='fail'><strong>PHP Errors/Warnings:</strong><pre>" . esc_html($form_errors) . "</pre></div>\n";
    }
    echo "<div style='max-height: 200px; overflow: auto; border: 1px solid #ccc; padding: 10px;'>" . substr(esc_html($form_output), 0, 1000) . (strlen($form_output) > 1000 ? '...' : '') . "</div>\n";
    
} else {
    echo "<p><span class='fail'>✗ [tribe_community_events] shortcode not available for testing</span></p>\n";
}

// Check 6: Test HVAC Shortcodes
echo "<h2>6. Testing HVAC Shortcodes</h2>\n";

if (shortcode_exists('hvac_create_event')) {
    echo "<h3>Testing [hvac_create_event]:</h3>\n";
    ob_start();
    $hvac_create_output = do_shortcode('[hvac_create_event]');
    $hvac_create_errors = ob_get_clean();
    
    if (!empty($hvac_create_errors)) {
        echo "<div class='fail'><strong>PHP Errors/Warnings:</strong><pre>" . esc_html($hvac_create_errors) . "</pre></div>\n";
    }
    echo "<div style='max-height: 200px; overflow: auto; border: 1px solid #ccc; padding: 10px;'>" . substr(esc_html($hvac_create_output), 0, 1000) . (strlen($hvac_create_output) > 1000 ? '...' : '') . "</div>\n";
} else {
    echo "<p><span class='fail'>✗ [hvac_create_event] shortcode not registered</span></p>\n";
}

if (shortcode_exists('hvac_edit_event')) {
    echo "<h3>Testing [hvac_edit_event]:</h3>\n";
    ob_start();
    $hvac_edit_output = do_shortcode('[hvac_edit_event]');
    $hvac_edit_errors = ob_get_clean();
    
    if (!empty($hvac_edit_errors)) {
        echo "<div class='fail'><strong>PHP Errors/Warnings:</strong><pre>" . esc_html($hvac_edit_errors) . "</pre></div>\n";
    }
    echo "<div style='max-height: 200px; overflow: auto; border: 1px solid #ccc; padding: 10px;'>" . substr(esc_html($hvac_edit_output), 0, 1000) . (strlen($hvac_edit_output) > 1000 ? '...' : '') . "</div>\n";
} else {
    echo "<p><span class='fail'>✗ [hvac_edit_event] shortcode not registered</span></p>\n";
}

// Check 7: WordPress Error Log
echo "<h2>7. Recent WordPress Errors</h2>\n";

$error_log_path = ini_get('error_log');
if (!$error_log_path) {
    $error_log_path = WP_CONTENT_DIR . '/debug.log';
}

if (file_exists($error_log_path)) {
    $recent_errors = shell_exec("tail -20 " . escapeshellarg($error_log_path));
    if ($recent_errors) {
        echo "<h3>Last 20 lines from error log:</h3>\n";
        echo "<pre style='max-height: 300px; overflow: auto;'>" . esc_html($recent_errors) . "</pre>\n";
    } else {
        echo "<p><span class='pass'>✓ No recent errors in log</span></p>\n";
    }
} else {
    echo "<p><span class='warning'>⚠ Error log not found at: $error_log_path</span></p>\n";
}

// Check 8: Plugin Activation Order
echo "<h2>8. Plugin Load Order Analysis</h2>\n";

$active_plugins = get_option('active_plugins');
$hvac_plugin_found = false;
$tec_plugin_found = false;

echo "<h3>Active Plugins (in load order):</h3>\n";
echo "<ol>\n";
foreach ($active_plugins as $plugin) {
    echo "<li><code>$plugin</code>";
    if (strpos($plugin, 'hvac-community-events') !== false) {
        echo " <span class='pass'>(HVAC Plugin)</span>";
        $hvac_plugin_found = true;
    } elseif (strpos($plugin, 'the-events-calendar') !== false) {
        echo " <span class='pass'>(TEC Plugin)</span>";
        $tec_plugin_found = true;
    }
    echo "</li>\n";
}
echo "</ol>\n";

if ($hvac_plugin_found && $tec_plugin_found) {
    echo "<p><span class='pass'>✓ Both HVAC and TEC plugins are active</span></p>\n";
} else {
    echo "<p><span class='fail'>✗ Missing required plugins</span></p>\n";
}

echo "<h2>Debugging Complete</h2>\n";
echo "<p>If issues persist, check the WordPress admin > Plugins page to ensure both 'The Events Calendar' and 'The Events Calendar Community Events' are properly activated.</p>\n";