#!/bin/bash

# Exit on error
set -e

# Source environment variables
source .env

echo "Debugging dashboard template data..."

# Create a debug version of the dashboard template
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && cat > wp-content/plugins/hvac-community-events/templates/template-hvac-dashboard-debug.php << 'EOF'
<?php
/**
 * Debug version of dashboard template
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Ensure user is logged in and has the correct role
if ( ! is_user_logged_in() || ! current_user_can( 'view_hvac_dashboard' ) ) {
    wp_safe_redirect( home_url( '/community-login/' ) );
    exit;
}

// Get the current user ID
\$user_id = get_current_user_id();

// Include and instantiate the dashboard data class
require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-dashboard-data.php';
\$dashboard_data = new HVAC_Dashboard_Data( \$user_id );

// Fetch 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();
\$revenue_target  = \$dashboard_data->get_annual_revenue_target();

// Debug output
echo '<pre style=\"background: #f5f5f5; padding: 20px; margin: 20px;\">';
echo 'User ID: ' . \$user_id . \"\\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\";
echo 'Revenue Target: ' . \$revenue_target . \"\\n\\n\";

// Direct database query test
global \$wpdb;
\$direct_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: ' . \$direct_count . \"\\n\\n\";

// Get actual events with details
\$events_query = new WP_Query(array(
    'post_type' => 'tribe_events',
    'post_author' => \$user_id,
    'post_status' => array('publish', 'future', 'draft', 'pending', 'private'),
    'posts_per_page' => -1
));

echo 'WP_Query found posts: ' . \$events_query->found_posts . \"\\n\";
echo 'Events:\\n';
foreach (\$events_query->posts as \$event) {
    echo '  - ID: ' . \$event->ID . ', Title: ' . \$event->post_title . ', Status: ' . \$event->post_status . \"\\n\";
}

// Get the SQL query
echo \"\\nGenerated SQL:\\n\";
echo \$events_query->request . \"\\n\";

echo '</pre>';

// Include the original template
include HVAC_CE_PLUGIN_DIR . 'templates/template-hvac-dashboard.php';
EOF"

# Create a page that uses this debug template
echo -e "\nCreating debug page..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && wp post create --post_type=page --post_title='Dashboard Debug' --post_name='dashboard-debug' --post_status=publish --post_content='Debug Dashboard Page'"

# Update the template loading to use debug version temporarily
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && cat > wp-content/plugins/hvac-community-events/includes/debug-template-loader.php << 'EOF'
<?php
// Temporary debug template loader
add_filter('template_include', function(\$template) {
    if (is_page('dashboard-debug')) {
        return HVAC_CE_PLUGIN_DIR . 'templates/template-hvac-dashboard-debug.php';
    }
    return \$template;
}, 99);
EOF"

# Include the debug loader in the main plugin file
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && echo \"require_once HVAC_CE_PLUGIN_DIR . 'includes/debug-template-loader.php';\" >> wp-content/plugins/hvac-community-events/hvac-community-events.php"

echo -e "\nDebug page created. Visit: https://wordpress-974670-5399585.cloudwaysapps.com/dashboard-debug/"