#!/bin/bash

# Debug Find a Trainer feature
# Usage: ./scripts/debug-find-trainer.sh

source .env

echo "=== Debugging Find a Trainer Feature ==="

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

echo "=== Checking if classes are loaded ==="
wp eval '
echo "HVAC_Find_Trainer_Page exists: " . (class_exists("HVAC_Find_Trainer_Page") ? "Yes" : "No") . "\n";
echo "HVAC_Trainer_Directory_Query exists: " . (class_exists("HVAC_Trainer_Directory_Query") ? "Yes" : "No") . "\n";
echo "HVAC_Contact_Form_Handler exists: " . (class_exists("HVAC_Contact_Form_Handler") ? "Yes" : "No") . "\n";
echo "HVAC_MapGeo_Integration exists: " . (class_exists("HVAC_MapGeo_Integration") ? "Yes" : "No") . "\n";
'

echo -e "\n=== Checking if shortcodes are registered ==="
wp eval '
global $shortcode_tags;
echo "hvac_trainer_directory shortcode: " . (isset($shortcode_tags["hvac_trainer_directory"]) ? "Registered" : "Not registered") . "\n";
echo "hvac_find_trainer shortcode: " . (isset($shortcode_tags["hvac_find_trainer"]) ? "Registered" : "Not registered") . "\n";
'

echo -e "\n=== Checking page content ==="
wp eval '
$page = get_page_by_path("find-a-trainer");
if ($page) {
    echo "Page ID: " . $page->ID . "\n";
    echo "Page Title: " . $page->post_title . "\n";
    echo "Page Status: " . $page->post_status . "\n";
    echo "Content length: " . strlen($page->post_content) . " characters\n";
    echo "Contains shortcode: " . (strpos($page->post_content, "[hvac_trainer_directory]") !== false ? "Yes" : "No") . "\n";
} else {
    echo "Page not found\n";
}
'

echo -e "\n=== Testing shortcode output ==="
wp eval '
if (shortcode_exists("hvac_trainer_directory")) {
    $output = do_shortcode("[hvac_trainer_directory]");
    echo "Shortcode output length: " . strlen($output) . " characters\n";
    if (strlen($output) > 0) {
        echo "First 200 chars: " . substr($output, 0, 200) . "...\n";
    } else {
        echo "Shortcode returns empty output\n";
    }
} else {
    echo "Shortcode not registered\n";
}
'

echo -e "\n=== Checking trainer profiles ==="
wp eval '
$profiles = get_posts([
    "post_type" => "trainer_profile",
    "post_status" => "publish",
    "posts_per_page" => 5
]);
echo "Found " . count($profiles) . " trainer profiles\n";
foreach ($profiles as $p) {
    $public = get_post_meta($p->ID, "is_public_profile", true);
    echo "- " . $p->post_title . " (Public: " . ($public ? "Yes" : "No") . ")\n";
}
'

echo -e "\n=== Checking PHP errors ==="
tail -n 20 /home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/debug.log 2>/dev/null || echo "No debug log found or empty"

ENDSSH

echo "=== Debug Complete ==="