#!/bin/bash

# Exit on error
set -e

# Source environment variables
source .env

echo "Checking dashboard data on staging..."

# Get test trainer user ID
USER_ID=$(sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && wp user get test_trainer --field=ID")
echo "Test trainer user ID: $USER_ID"

# Check events associated with this user
echo -e "\nEvents by author (post_author = $USER_ID):"
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && wp db query \"SELECT ID, post_title, post_status, post_author FROM wp_posts WHERE post_type = 'tribe_events' AND post_author = $USER_ID\""

echo -e "\nEvents by organizer (meta _EventOrganizerID = $USER_ID):"
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && wp db query \"SELECT p.ID, p.post_title, p.post_status, p.post_author, pm.meta_value as organizer_id FROM wp_posts p JOIN wp_postmeta pm ON p.ID = pm.post_id WHERE p.post_type = 'tribe_events' AND pm.meta_key = '_EventOrganizerID' AND pm.meta_value = '$USER_ID'\""

echo -e "\nAll events with their author and organizer:"
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && wp db query \"SELECT p.ID, p.post_title, p.post_status, p.post_author, GROUP_CONCAT(CASE WHEN pm.meta_key = '_EventOrganizerID' THEN pm.meta_value END) as organizer_id FROM wp_posts p LEFT JOIN wp_postmeta pm ON p.ID = pm.post_id WHERE p.post_type = 'tribe_events' GROUP BY p.ID\""

echo -e "\nUser capabilities for test_trainer:"
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && wp user meta get $USER_ID wp_capabilities"

echo -e "\nDebug: Clear cache and test dashboard data class:"
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && wp cache flush"

# Create a PHP test script
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && cat > test-dashboard-data.php << 'EOF'
<?php
// Load WordPress
require_once('wp-load.php');

// Get test user ID
\$user_id = get_user_by('login', 'test_trainer')->ID;
echo \"User ID: \$user_id\\n\";

// Check if the class exists
if (class_exists('HVAC_Dashboard_Data')) {
    echo \"Using original HVAC_Dashboard_Data\\n\";
    \$dashboard_data = new HVAC_Dashboard_Data(\$user_id);
} elseif (class_exists('HVAC_Dashboard_Data_Refactored')) {
    echo \"Using refactored HVAC_Dashboard_Data_Refactored\\n\";
    \$dashboard_data = new HVAC_Dashboard_Data_Refactored(\$user_id);
} else {
    die(\"Dashboard data class not found\\n\");
}

// Get stats
\$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_tickets = \$dashboard_data->get_total_tickets_sold();
\$total_revenue = \$dashboard_data->get_total_revenue();

echo \"Total Events: \$total_events\\n\";
echo \"Upcoming Events: \$upcoming_events\\n\";
echo \"Past Events: \$past_events\\n\";
echo \"Total Tickets: \$total_tickets\\n\";
echo \"Total Revenue: \$total_revenue\\n\";

// Direct query test
echo \"\\nDirect query test:\\n\";
\$count = \$wpdb->get_var(\$wpdb->prepare(
    \"SELECT COUNT(ID) FROM wp_posts WHERE post_type = %s AND post_author = %d AND post_status IN ('publish', 'future', 'draft', 'pending', 'private')\",
    'tribe_events',
    \$user_id
));
echo \"Direct count by author: \$count\\n\";

// Check with meta query
\$count2 = \$wpdb->get_var(\$wpdb->prepare(
    \"SELECT COUNT(DISTINCT p.ID) FROM wp_posts p JOIN wp_postmeta pm ON p.ID = pm.post_id WHERE p.post_type = %s AND pm.meta_key = '_EventOrganizerID' AND pm.meta_value = %s\",
    'tribe_events',
    \$user_id
));
echo \"Count by organizer ID: \$count2\\n\";
EOF"

echo -e "\nRunning PHP test script:"
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && php test-dashboard-data.php"

# Clean up
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && rm test-dashboard-data.php"