<?php
/**
 * Direct geocoding status check
 * 
 * Run this with: wp eval-file test-geocoding-direct.php
 */

// Ensure we're in WordPress environment
if (!defined('ABSPATH')) {
    // Try to load WordPress
    $wp_load_paths = [
        '../../../wp-load.php',
        '../../../../wp-load.php',
        '../../../../../wp-load.php'
    ];
    
    foreach ($wp_load_paths as $path) {
        if (file_exists($path)) {
            require_once $path;
            break;
        }
    }
    
    if (!defined('ABSPATH')) {
        die("Could not load WordPress. Please run with wp eval-file command.\n");
    }
}

echo "🔍 CHECKING TRAINER GEOCODING STATUS\n";
echo "================================================================================\n";

// Check if the trainer profile settings class exists
if (!class_exists('HVAC_Trainer_Profile_Settings')) {
    echo "❌ HVAC_Trainer_Profile_Settings class not found\n";
    exit(1);
}

try {
    // Create instance and get statistics
    $settings = HVAC_Trainer_Profile_Settings::get_instance();
    
    // Use reflection to access the private method
    $reflection = new ReflectionClass($settings);
    $method = $reflection->getMethod('get_profile_statistics');
    $method->setAccessible(true);
    $stats = $method->invoke($settings);
    
    echo "📊 Trainer Profile Statistics:\n";
    echo "   Total Profiles: " . $stats['total_profiles'] . "\n";
    echo "   Public Profiles: " . $stats['public_profiles'] . "\n";
    echo "   Geocoded Profiles: " . $stats['geocoded_profiles'] . "\n";
    echo "   Sync Issues: " . $stats['sync_issues'] . "\n";
    
    // Calculate percentages
    if ($stats['total_profiles'] > 0) {
        $geocoded_percent = round(($stats['geocoded_profiles'] / $stats['total_profiles']) * 100, 1);
        $public_percent = round(($stats['public_profiles'] / $stats['total_profiles']) * 100, 1);
        
        echo "\n📈 Percentages:\n";
        echo "   Geocoded: {$geocoded_percent}%\n";
        echo "   Public: {$public_percent}%\n";
    }
    
    // Check individual profile geocoding status
    echo "\n🗺️ Individual Profile Geocoding Status:\n";
    
    global $wpdb;
    $profiles = $wpdb->get_results("
        SELECT p.ID, p.post_title, 
               pm_lat.meta_value as latitude,
               pm_lng.meta_value as longitude,
               pm_city.meta_value as trainer_city,
               pm_state.meta_value as trainer_state
        FROM {$wpdb->posts} p
        LEFT JOIN {$wpdb->postmeta} pm_lat ON p.ID = pm_lat.post_id AND pm_lat.meta_key = 'latitude'
        LEFT JOIN {$wpdb->postmeta} pm_lng ON p.ID = pm_lng.post_id AND pm_lng.meta_key = 'longitude'
        LEFT JOIN {$wpdb->postmeta} pm_city ON p.ID = pm_city.post_id AND pm_city.meta_key = 'trainer_city'
        LEFT JOIN {$wpdb->postmeta} pm_state ON p.ID = pm_state.post_id AND pm_state.meta_key = 'trainer_state'
        WHERE p.post_type = 'trainer_profile'
        AND p.post_status = 'publish'
        ORDER BY p.post_title
    ");
    
    if ($profiles) {
        foreach ($profiles as $profile) {
            $geocoded = !empty($profile->latitude) && !empty($profile->longitude);
            $location = trim(($profile->trainer_city ?: '') . ', ' . ($profile->trainer_state ?: ''), ', ');
            $status = $geocoded ? "✅ GEOCODED" : "❌ NOT GEOCODED";
            
            echo sprintf("   Profile #%d (%s): %s - %s\n", 
                $profile->ID, 
                $profile->post_title ?: 'No Title',
                $status,
                $location ?: 'No location data'
            );
            
            if ($geocoded) {
                echo sprintf("      → Coordinates: %s, %s\n", $profile->latitude, $profile->longitude);
            }
        }
    } else {
        echo "   No trainer profiles found.\n";
    }
    
} catch (Exception $e) {
    echo "❌ Error checking geocoding status: " . $e->getMessage() . "\n";
    exit(1);
}

echo "\n================================================================================\n";
echo "🎯 GEOCODING STATUS CHECK COMPLETE\n";
?>