<?php
/**
 * Debug script to identify Find a Trainer page error
 * Upload to staging wp-content/plugins/hvac-community-events/ directory
 */

// Enable WordPress debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Start output buffering to catch any errors
ob_start();

// Try to load WordPress
$wp_config_path = dirname(__DIR__) . '/../../../wp-config.php';
if (!file_exists($wp_config_path)) {
    $wp_config_path = dirname(__DIR__) . '/../../../../wp-config.php';
}

if (file_exists($wp_config_path)) {
    require_once $wp_config_path;
    
    echo "WordPress loaded successfully.\n";
    echo "Plugin directory: " . WP_PLUGIN_DIR . "\n";
    echo "HVAC Plugin path: " . WP_PLUGIN_DIR . '/hvac-community-events/hvac-community-events.php' . "\n";
    
    // Check if HVAC plugin file exists
    $hvac_plugin_file = WP_PLUGIN_DIR . '/hvac-community-events/hvac-community-events.php';
    echo "HVAC plugin file exists: " . (file_exists($hvac_plugin_file) ? 'YES' : 'NO') . "\n";
    
    // Check if Find a Trainer class file exists
    $find_trainer_file = WP_PLUGIN_DIR . '/hvac-community-events/includes/find-trainer/class-hvac-find-trainer-page.php';
    echo "Find Trainer class file exists: " . (file_exists($find_trainer_file) ? 'YES' : 'NO') . "\n";
    
    // Test instantiation of main plugin class
    try {
        echo "Testing plugin instantiation...\n";
        if (function_exists('hvac_community_events')) {
            $plugin = hvac_community_events();
            echo "Main plugin instance created: " . get_class($plugin) . "\n";
        } else {
            echo "ERROR: hvac_community_events function not found\n";
        }
        
        // Test Find a Trainer class
        echo "Testing Find a Trainer class...\n";
        if (class_exists('HVAC_Find_Trainer_Page')) {
            $find_trainer = HVAC_Find_Trainer_Page::get_instance();
            echo "Find a Trainer instance created: " . get_class($find_trainer) . "\n";
        } else {
            echo "ERROR: HVAC_Find_Trainer_Page class not found\n";
        }
        
        // Test if find-a-trainer page exists
        echo "Testing find-a-trainer page...\n";
        $page = get_page_by_path('find-a-trainer');
        if ($page) {
            echo "Find a trainer page exists: ID " . $page->ID . "\n";
            echo "Page status: " . $page->post_status . "\n";
        } else {
            echo "ERROR: find-a-trainer page not found in database\n";
        }
        
    } catch (Exception $e) {
        echo "EXCEPTION: " . $e->getMessage() . "\n";
        echo "File: " . $e->getFile() . "\n";
        echo "Line: " . $e->getLine() . "\n";
        echo "Stack trace:\n" . $e->getTraceAsString() . "\n";
    } catch (Error $e) {
        echo "FATAL ERROR: " . $e->getMessage() . "\n";
        echo "File: " . $e->getFile() . "\n";
        echo "Line: " . $e->getLine() . "\n";
        echo "Stack trace:\n" . $e->getTraceAsString() . "\n";
    }
    
} else {
    echo "ERROR: WordPress wp-config.php not found\n";
    echo "Checked paths:\n";
    echo "- " . dirname(__DIR__) . '/../../../wp-config.php' . "\n";
    echo "- " . dirname(__DIR__) . '/../../../../wp-config.php' . "\n";
}

$output = ob_get_clean();
echo "<pre>" . htmlspecialchars($output) . "</pre>";