#!/bin/bash

# Debug template rendering

# 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 Template Rendering ==="
echo "Remote host: $UPSKILL_STAGING_IP"
echo "==============================="

# Debug template
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-template.php << 'PHP'
<?php
// Set up WordPress environment
require_once 'wp-load.php';

// Force login as test_trainer
$user = get_user_by('login', 'test_trainer');
wp_set_current_user($user->ID);
wp_set_auth_cookie($user->ID);

echo "=== TEMPLATE DEBUG ===\n\n";

// Check current user
echo "Current user: " . get_current_user_id() . "\n";
echo "Can view dashboard: " . (current_user_can('view_hvac_dashboard') ? 'Yes' : 'No') . "\n\n";

// Load dashboard data
require_once '/home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/includes/class-hvac-dashboard-data.php';
$dashboard_data = new HVAC_Dashboard_Data(get_current_user_id());

// Get data
$total_events = $dashboard_data->get_total_events_count();
$upcoming_events = $dashboard_data->get_upcoming_events_count();
$past_events = $dashboard_data->get_past_events_count();
$total_sold = $dashboard_data->get_total_tickets_sold();
$total_revenue = $dashboard_data->get_total_revenue();

echo "Dashboard data:\n";
echo "Total events: $total_events\n";
echo "Upcoming events: $upcoming_events\n";
echo "Past events: $past_events\n";
echo "Total sold: $total_sold\n";
echo "Total revenue: $total_revenue\n\n";

// Check if variables are being overridden
echo "Checking global scope:\n";
$GLOBALS['total_events'] = isset($GLOBALS['total_events']) ? $GLOBALS['total_events'] : 'not set';
echo "Global total_events: " . $GLOBALS['total_events'] . "\n\n";

// Test template directly
echo "Testing template include:\n";
global $total_events_test;
$total_events_test = $total_events;
echo "Set test variable to: $total_events_test\n";

// Check theme template redirect
echo "\nChecking template redirect:\n";
$template = locate_template('template-hvac-dashboard.php');
echo "Theme template found: " . ($template ? $template : 'No') . "\n";

PHP

php debug-template.php
rm debug-template.php
EOF

echo "Debug completed!"