<?php
/**
 * Test TEC Community Events Shortcode Variations
 * 
 * This script tests different shortcode parameter combinations to identify
 * the correct syntax for rendering form fields.
 */

// Simple WordPress bootstrap (adjust path if needed)
$wp_root_paths = [
    '../../../wp-load.php',      // If in plugins/plugin-name/
    '../../../../wp-load.php',   // If deeper
    '/var/www/html/wp-load.php', // Common server path
    dirname(__FILE__) . '/wp-load.php' // Local
];

$wp_loaded = false;
foreach ($wp_root_paths as $path) {
    if (file_exists($path)) {
        define('WP_USE_THEMES', false);
        require_once($path);
        $wp_loaded = true;
        break;
    }
}

if (!$wp_loaded) {
    die("WordPress not found. Please adjust the path in the script.");
}

?><!DOCTYPE html>
<html>
<head>
    <title>TEC Community Events Shortcode Test</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .test-section { border: 1px solid #ccc; margin: 20px 0; padding: 15px; }
        .test-title { background: #f5f5f5; margin: -15px -15px 15px; padding: 10px; font-weight: bold; }
        .pass { color: green; }
        .fail { color: red; }
        .warning { color: orange; }
        .output-box { border: 1px solid #ddd; padding: 10px; background: #f9f9f9; max-height: 300px; overflow: auto; }
    </style>
</head>
<body>

<h1>TEC Community Events Shortcode Test</h1>

<?php
// Check basic plugin status
echo "<div class='test-section'>";
echo "<div class='test-title'>Plugin Status Check</div>";

$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>";
echo "<p><strong>TEC Community Events:</strong> " . ($tec_ce_active ? '<span class="pass">✓ Active</span>' : '<span class="fail">✗ Not Active</span>') . "</p>";

$shortcode_exists = shortcode_exists('tribe_community_events');
echo "<p><strong>tribe_community_events shortcode:</strong> " . ($shortcode_exists ? '<span class="pass">✓ Registered</span>' : '<span class="fail">✗ Not Registered</span>') . "</p>";

// Check user status
if (is_user_logged_in()) {
    $user = wp_get_current_user();
    echo "<p><strong>Current User:</strong> {$user->user_login} (ID: {$user->ID})</p>";
    echo "<p><strong>User Roles:</strong> " . implode(', ', $user->roles) . "</p>";
} else {
    echo "<p><span class='warning'>⚠ Not logged in - this may affect form rendering</span></p>";
}

echo "</div>";

// Test different shortcode variations
if ($shortcode_exists) {
    $variations = [
        '[tribe_community_events]' => 'Basic shortcode without parameters',
        '[tribe_community_events view="submission_form"]' => 'Current implementation (with submission_form view)',
        '[tribe_community_events view="form"]' => 'Alternative form view',
        '[tribe_community_events view="edit"]' => 'Edit view without ID',
        '[tribe_community_events view="submit"]' => 'Submit view variation',
        '[tribe_community_events view="new"]' => 'New view variation'
    ];
    
    foreach ($variations as $shortcode => $description) {
        echo "<div class='test-section'>";
        echo "<div class='test-title'>Testing: $shortcode</div>";
        echo "<p><em>$description</em></p>";
        
        // Capture output and errors
        ob_start();
        $output = do_shortcode($shortcode);
        $errors = ob_get_clean();
        
        if (!empty($errors)) {
            echo "<div class='fail'><strong>PHP Errors/Warnings:</strong><br>";
            echo "<pre>" . esc_html($errors) . "</pre></div>";
        }
        
        echo "<div class='output-box'>";
        if (empty(trim($output))) {
            echo "<span class='fail'>✗ No output generated</span>";
        } else {
            // Check for form fields
            $has_inputs = preg_match('/<input[^>]*type=["\'](?:text|email|url|tel|number|date|time|textarea)["\'][^>]*>/i', $output);
            $has_textareas = preg_match('/<textarea[^>]*>/i', $output);
            $has_selects = preg_match('/<select[^>]*>/i', $output);
            $has_form = preg_match('/<form[^>]*>/i', $output);
            
            echo "<p><strong>Form Analysis:</strong></p>";
            echo "<ul>";
            echo "<li>Contains &lt;form&gt; tags: " . ($has_form ? '<span class="pass">✓ Yes</span>' : '<span class="fail">✗ No</span>') . "</li>";
            echo "<li>Contains input fields: " . ($has_inputs ? '<span class="pass">✓ Yes</span>' : '<span class="fail">✗ No</span>') . "</li>";
            echo "<li>Contains textareas: " . ($has_textareas ? '<span class="pass">✓ Yes</span>' : '<span class="fail">✗ No</span>') . "</li>";
            echo "<li>Contains select boxes: " . ($has_selects ? '<span class="pass">✓ Yes</span>' : '<span class="fail">✗ No</span>') . "</li>";
            echo "</ul>";
            
            // Show first 1000 characters of output
            $display_output = strlen($output) > 1000 ? substr($output, 0, 1000) . '...' : $output;
            echo "<p><strong>HTML Output (first 1000 chars):</strong></p>";
            echo "<pre>" . esc_html($display_output) . "</pre>";
        }
        echo "</div>";
        echo "</div>";
    }
} else {
    echo "<div class='test-section'>";
    echo "<div class='test-title fail'>Cannot Test Shortcode Variations</div>";
    echo "<p>The tribe_community_events shortcode is not registered. Please ensure TEC Community Events plugin is properly activated.</p>";
    echo "</div>";
}

// Additional debugging info
echo "<div class='test-section'>";
echo "<div class='test-title'>Additional Debug Information</div>";

// Check if we're on the correct page type
echo "<p><strong>Current page context:</strong></p>";
echo "<ul>";
echo "<li>is_admin(): " . (is_admin() ? 'true' : 'false') . "</li>";
echo "<li>is_page(): " . (is_page() ? 'true' : 'false') . "</li>";
echo "<li>is_single(): " . (is_single() ? 'true' : 'false') . "</li>";
echo "<li>Current query: " . get_query_var('pagename') . "</li>";
echo "</ul>";

// Check for relevant classes/functions
$functions_to_check = [
    'tribe_community_events_init',
    'tribe_is_community_edit_event_page',
    'Tribe__Events__Community__Main'
];

echo "<p><strong>TEC Functions/Classes:</strong></p>";
echo "<ul>";
foreach ($functions_to_check as $func) {
    if (strpos($func, 'Tribe__') === 0) {
        $exists = class_exists($func);
        $type = 'class';
    } else {
        $exists = function_exists($func);
        $type = 'function';
    }
    echo "<li>$func ($type): " . ($exists ? '<span class="pass">✓ Available</span>' : '<span class="fail">✗ Not Available</span>') . "</li>";
}
echo "</ul>";

echo "</div>";
?>

<div class="test-section">
    <div class="test-title">Next Steps</div>
    <p>Based on the test results above:</p>
    <ol>
        <li>If no shortcode variations produce form fields, the issue is likely with TEC Community Events plugin configuration or permissions</li>
        <li>If some variations work better than others, update the HVAC plugin to use the working variation</li>
        <li>Check the WordPress admin for TEC Community Events settings that might restrict form display</li>
        <li>Ensure the current user has proper permissions to create events</li>
    </ol>
</div>

</body>
</html>