<?php
/**
 * Debug script for find-a-trainer page issues
 */

// Bootstrap WordPress
require_once('/var/www/html/wp-config.php');

echo "🔍 DEBUGGING FIND-A-TRAINER PAGE\n";
echo "================================\n\n";

// 1. Check if trainer_profile post type is registered
echo "1. Checking trainer_profile post type registration...\n";
$post_types = get_post_types(['public' => false], 'names');
if (in_array('trainer_profile', $post_types)) {
    echo "✅ trainer_profile post type is registered\n";
} else {
    echo "❌ trainer_profile post type is NOT registered\n";
    echo "Available post types: " . implode(', ', $post_types) . "\n";
}

// 2. Check for trainer_profile posts
echo "\n2. Checking for trainer_profile posts...\n";
$profiles_query = new WP_Query([
    'post_type' => 'trainer_profile',
    'post_status' => 'publish',
    'posts_per_page' => -1
]);

echo "Found {$profiles_query->found_posts} trainer_profile posts\n";

if ($profiles_query->have_posts()) {
    while ($profiles_query->have_posts()) {
        $profiles_query->the_post();
        $profile_id = get_the_ID();
        $user_id = get_post_meta($profile_id, 'user_id', true);
        $is_public = get_post_meta($profile_id, 'is_public_profile', true);
        $trainer_name = get_post_meta($profile_id, 'trainer_display_name', true);
        
        echo "  - Profile ID: $profile_id, User ID: $user_id, Public: $is_public, Name: $trainer_name\n";
    }
}
wp_reset_postdata();

// 3. Check user account statuses
echo "\n3. Checking user account statuses...\n";
$user_query = new WP_User_Query([
    'meta_query' => [
        [
            'key' => 'account_status',
            'value' => ['approved', 'active', 'inactive'],
            'compare' => 'IN'
        ]
    ],
    'fields' => 'ID'
]);
$approved_user_ids = $user_query->get_results();
echo "Found " . count($approved_user_ids) . " approved users: " . implode(', ', $approved_user_ids) . "\n";

// 4. Check all users with HVAC roles
echo "\n4. Checking users with HVAC roles...\n";
$hvac_users = get_users([
    'role__in' => ['hvac_trainer', 'hvac_master_trainer'],
    'fields' => 'all'
]);

echo "Found " . count($hvac_users) . " HVAC users:\n";
foreach ($hvac_users as $user) {
    $account_status = get_user_meta($user->ID, 'account_status', true);
    echo "  - User ID: {$user->ID}, Name: {$user->display_name}, Role: " . implode(', ', $user->roles) . ", Status: $account_status\n";
}

// 5. Run the exact same query as the template
echo "\n5. Running exact template query...\n";
$approved_user_ids = $user_query->get_results();

if (!empty($approved_user_ids)) {
    $template_args = [
        'post_type' => 'trainer_profile',
        'posts_per_page' => 12,
        'post_status' => 'publish',
        'meta_query' => [
            'relation' => 'AND',
            [
                'key' => 'is_public_profile',
                'value' => '1',
                'compare' => '='
            ],
            [
                'key' => 'user_id',
                'value' => $approved_user_ids,
                'compare' => 'IN'
            ]
        ]
    ];
    
    $template_query = new WP_Query($template_args);
    echo "Template query found: {$template_query->found_posts} posts\n";
    
    if ($template_query->have_posts()) {
        while ($template_query->have_posts()) {
            $template_query->the_post();
            $profile_id = get_the_ID();
            $user_id = get_post_meta($profile_id, 'user_id', true);
            $name = get_post_meta($profile_id, 'trainer_display_name', true);
            echo "  - Would display: $name (Profile ID: $profile_id, User ID: $user_id)\n";
        }
    }
    wp_reset_postdata();
} else {
    echo "❌ No approved users found - template query will return empty\n";
}

// 6. Check certification manager
echo "\n6. Checking certification manager...\n";
if (class_exists('HVAC_Trainer_Certification_Manager')) {
    echo "✅ HVAC_Trainer_Certification_Manager class is available\n";
    $cert_manager = HVAC_Trainer_Certification_Manager::instance();
    
    // Test with a user ID
    if (!empty($hvac_users)) {
        $test_user = $hvac_users[0];
        $certifications = $cert_manager->get_active_trainer_certifications($test_user->ID);
        echo "  - Test user {$test_user->display_name} has " . count($certifications) . " active certifications\n";
    }
} else {
    echo "❌ HVAC_Trainer_Certification_Manager class is NOT available\n";
}

// 7. Check if find-a-trainer page exists
echo "\n7. Checking find-a-trainer page...\n";
$find_trainer_page = get_page_by_path('find-a-trainer');
if ($find_trainer_page) {
    echo "✅ find-a-trainer page exists (ID: {$find_trainer_page->ID})\n";
    echo "  - Template: " . get_page_template_slug($find_trainer_page->ID) . "\n";
    echo "  - Status: {$find_trainer_page->post_status}\n";
} else {
    echo "❌ find-a-trainer page does not exist\n";
}

echo "\n🎯 DEBUGGING SUMMARY:\n";
echo "=====================\n";
echo "This debug script should help identify why the find-a-trainer page is empty.\n";
echo "Check each section above for any ❌ errors that need to be addressed.\n";
?>