<?php
/**
 * Direct test of Find a Trainer functionality
 * Upload to staging server and access via browser to test
 */

// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);

echo "<h1>Find a Trainer Direct Test</h1>";

// Try to load WordPress
$wp_load_paths = [
    __DIR__ . '/../../../wp-load.php',
    __DIR__ . '/../../../../wp-load.php',
    dirname(__DIR__) . '/../../../wp-load.php'
];

$wp_loaded = false;
foreach ($wp_load_paths as $path) {
    if (file_exists($path)) {
        try {
            require_once $path;
            $wp_loaded = true;
            echo "<p style='color:green'>✓ WordPress loaded from: $path</p>";
            break;
        } catch (Exception $e) {
            echo "<p style='color:red'>✗ Failed to load WordPress from: $path - " . $e->getMessage() . "</p>";
        }
    } else {
        echo "<p style='color:orange'>- wp-load.php not found at: $path</p>";
    }
}

if (!$wp_loaded) {
    die('<p style="color:red">FATAL: Could not load WordPress</p>');
}

// Test WordPress basics
echo "<h2>WordPress Environment Test</h2>";
echo "<p>WordPress version: " . get_bloginfo('version') . "</p>";
echo "<p>Site URL: " . get_site_url() . "</p>";
echo "<p>Plugin directory: " . WP_PLUGIN_DIR . "</p>";

// Test plugin loading
echo "<h2>Plugin Loading Test</h2>";

// Check if HVAC plugin is active
if (is_plugin_active('hvac-community-events/hvac-community-events.php')) {
    echo "<p style='color:green'>✓ HVAC plugin is active</p>";
} else {
    echo "<p style='color:red'>✗ HVAC plugin is NOT active</p>";
}

// Check if HVAC plugin function exists
if (function_exists('hvac_community_events')) {
    echo "<p style='color:green'>✓ hvac_community_events function exists</p>";
    
    try {
        $plugin = hvac_community_events();
        echo "<p style='color:green'>✓ Plugin instance created: " . get_class($plugin) . "</p>";
    } catch (Exception $e) {
        echo "<p style='color:red'>✗ Plugin instantiation failed: " . $e->getMessage() . "</p>";
    }
} else {
    echo "<p style='color:red'>✗ hvac_community_events function does not exist</p>";
}

// Test Find a Trainer class
echo "<h2>Find a Trainer Class Test</h2>";

if (class_exists('HVAC_Find_Trainer_Page')) {
    echo "<p style='color:green'>✓ HVAC_Find_Trainer_Page class exists</p>";
    
    try {
        $find_trainer = HVAC_Find_Trainer_Page::get_instance();
        echo "<p style='color:green'>✓ Find a Trainer instance created</p>";
    } catch (Exception $e) {
        echo "<p style='color:red'>✗ Find a Trainer instantiation failed: " . $e->getMessage() . "</p>";
    }
} else {
    echo "<p style='color:red'>✗ HVAC_Find_Trainer_Page class does not exist</p>";
}

// Check file existence
echo "<h2>File Existence Test</h2>";

$critical_files = [
    'hvac-community-events.php',
    'includes/class-hvac-plugin.php',
    'includes/find-trainer/class-hvac-find-trainer-page.php',
    'includes/find-trainer/class-hvac-mapgeo-integration.php',
    'assets/css/find-trainer.css',
    'assets/js/find-trainer.js'
];

$plugin_dir = WP_PLUGIN_DIR . '/hvac-community-events/';

foreach ($critical_files as $file) {
    $full_path = $plugin_dir . $file;
    if (file_exists($full_path)) {
        echo "<p style='color:green'>✓ $file exists</p>";
    } else {
        echo "<p style='color:red'>✗ $file MISSING</p>";
    }
}

// Test page existence
echo "<h2>Page Existence Test</h2>";

$page = get_page_by_path('find-a-trainer');
if ($page) {
    echo "<p style='color:green'>✓ find-a-trainer page exists (ID: {$page->ID})</p>";
    echo "<p>Page status: {$page->post_status}</p>";
    echo "<p>Page title: {$page->post_title}</p>";
} else {
    echo "<p style='color:red'>✗ find-a-trainer page does not exist in database</p>";
}

// Test direct URL
echo "<h2>URL Test</h2>";
$find_trainer_url = home_url('/find-a-trainer/');
echo "<p>Expected URL: <a href='$find_trainer_url' target='_blank'>$find_trainer_url</a></p>";

// Test shortcodes
echo "<h2>Shortcode Test</h2>";

if (shortcode_exists('hvac_find_trainer')) {
    echo "<p style='color:green'>✓ hvac_find_trainer shortcode exists</p>";
} else {
    echo "<p style='color:red'>✗ hvac_find_trainer shortcode does not exist</p>";
}

if (shortcode_exists('hvac_trainer_directory')) {
    echo "<p style='color:green'>✓ hvac_trainer_directory shortcode exists</p>";
} else {
    echo "<p style='color:red'>✗ hvac_trainer_directory shortcode does not exist</p>";
}

echo "<h2>Memory and Environment</h2>";
echo "<p>Memory usage: " . number_format(memory_get_usage() / 1024 / 1024, 2) . " MB</p>";
echo "<p>Memory limit: " . ini_get('memory_limit') . "</p>";
echo "<p>Max execution time: " . ini_get('max_execution_time') . "</p>";

echo "<h2>Test Complete</h2>";
echo "<p>If all tests pass but the page still shows error, check the server error logs for specific PHP errors.</p>";