#!/bin/bash

# Debug dashboard live on server

# Get absolute path to this script's directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Navigate to wordpress-dev directory
cd "$(dirname "$SCRIPT_DIR")" || exit 1

# Load environment variables
ENV_FILE=".env"
if [ ! -f "$ENV_FILE" ]; then
    echo "Error: .env file not found at: $ENV_FILE"
    exit 1
fi

source "$ENV_FILE"

echo "=== Debugging Dashboard Live ==="
echo "Remote host: $UPSKILL_STAGING_IP"
echo "==============================="

# Debug dashboard
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" <<'EOF'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

# Create debug script
cat > debug-dashboard.php << 'PHP'
<?php
require_once 'wp-load.php';

echo "=== DASHBOARD DEBUG ===\n\n";

// Check plugin file
$plugin_file = '/home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/includes/class-hvac-dashboard-data.php';
echo "Plugin file exists: " . (file_exists($plugin_file) ? 'Yes' : 'No') . "\n";
echo "Plugin file modified: " . date('Y-m-d H:i:s', filemtime($plugin_file)) . "\n\n";

// Get test_trainer user ID
$user = get_user_by('login', 'test_trainer');
if (!$user) {
    echo "ERROR: test_trainer user not found!\n";
    exit;
}
$user_id = $user->ID;
echo "test_trainer user ID: $user_id\n\n";

// Test dashboard data directly
require_once $plugin_file;
$dashboard = new HVAC_Dashboard_Data($user_id);

echo "Direct method calls:\n";
echo "Total events: " . $dashboard->get_total_events_count() . "\n";
echo "Upcoming events: " . $dashboard->get_upcoming_events_count() . "\n";
echo "Past events: " . $dashboard->get_past_events_count() . "\n\n";

// Test a raw query
echo "Raw query test:\n";
$args = array(
    'post_type' => 'tribe_events',
    'author' => $user_id,
    'posts_per_page' => -1,
    'post_status' => 'any'
);
$query = new WP_Query($args);
echo "Found: " . $query->found_posts . "\n";
echo "SQL: " . $query->request . "\n\n";

// Check cache
echo "Cache status:\n";
$cache_group = 'counts';
$cache_key = 'hvac_events_' . $user_id;
$cached = wp_cache_get($cache_key, $cache_group);
echo "Cached value: " . var_export($cached, true) . "\n";

// Clear cache and try again
wp_cache_flush();
echo "\nAfter cache flush:\n";
echo "Total events: " . $dashboard->get_total_events_count() . "\n";

PHP

php debug-dashboard.php
rm debug-dashboard.php
EOF

echo "Debug completed!"