#!/bin/bash

# Verify existing trainer data on staging
# Usage: ./scripts/verify-trainer-data.sh

source .env

echo "=== Verifying Existing Trainer Data on Staging ==="

ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" << 'ENDSSH'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

wp eval '
// Check trainer profiles
$profiles = get_posts([
    "post_type" => "trainer_profile",
    "post_status" => "publish",
    "posts_per_page" => -1
]);

echo "=== Trainer Profile Statistics ===\n";
echo "Total trainer profiles: " . count($profiles) . "\n";

// Check for geocoded profiles
$geocoded = 0;
$public = 0;
$with_events = 0;

foreach ($profiles as $profile) {
    $lat = get_post_meta($profile->ID, "latitude", true);
    $lng = get_post_meta($profile->ID, "longitude", true);
    if ($lat && $lng) {
        $geocoded++;
    }
    
    $is_public = get_post_meta($profile->ID, "is_public_profile", true);
    if ($is_public == "1") {
        $public++;
    }
    
    $user_id = get_post_meta($profile->ID, "user_id", true);
    if ($user_id && function_exists("tribe_get_events")) {
        $events = tribe_get_events([
            "author" => $user_id,
            "posts_per_page" => 1
        ]);
        if (!empty($events)) {
            $with_events++;
        }
    }
}

echo "Geocoded profiles: $geocoded\n";
echo "Public profiles: $public\n";
echo "Profiles with events: $with_events\n";

// Check users with trainer roles
$trainers = get_users(["role" => "hvac_trainer"]);
$master_trainers = get_users(["role" => "hvac_master_trainer"]);

echo "\n=== User Statistics ===\n";
echo "Users with hvac_trainer role: " . count($trainers) . "\n";
echo "Users with hvac_master_trainer role: " . count($master_trainers) . "\n";

// Check taxonomies
$taxonomies = ["business_type", "training_formats", "training_resources", "training_audience"];
echo "\n=== Taxonomy Statistics ===\n";

foreach ($taxonomies as $tax) {
    if (taxonomy_exists($tax)) {
        $terms = get_terms(["taxonomy" => $tax, "hide_empty" => false]);
        if (!is_wp_error($terms)) {
            echo "$tax: " . count($terms) . " terms\n";
        }
    }
}

// Sample some trainer data
echo "\n=== Sample Trainer Data (First 5) ===\n";
$sample_profiles = array_slice($profiles, 0, 5);
foreach ($sample_profiles as $profile) {
    $name = get_post_meta($profile->ID, "trainer_display_name", true);
    $city = get_post_meta($profile->ID, "trainer_city", true);
    $state = get_post_meta($profile->ID, "trainer_state", true);
    $lat = get_post_meta($profile->ID, "latitude", true);
    $lng = get_post_meta($profile->ID, "longitude", true);
    
    echo "- $name ($city, $state)";
    if ($lat && $lng) {
        echo " [Geocoded]";
    }
    echo "\n";
}
'

ENDSSH

echo "=== Data Verification Complete ==="