#!/bin/bash

# Comprehensive debug of event queries

# 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 "=== Comprehensive Event Query Debug ==="
echo "Remote host: $UPSKILL_STAGING_IP"
echo "==============================="

# Create comprehensive debug script
cat << 'EOF' > /tmp/debug-events.php
<?php
// Load WordPress
require_once dirname(dirname(__FILE__)) . '/public_html/wp-load.php';

echo "=== COMPREHENSIVE DEBUG ===\n\n";

// 1. User info
echo "1. User Information:\n";
$user = get_user_by('ID', 17);
echo "User ID: " . $user->ID . "\n";
echo "User login: " . $user->user_login . "\n";
echo "User roles: " . implode(', ', $user->roles) . "\n\n";

// 2. Direct DB query to see all events
echo "2. All tribe_events in database (direct query):\n";
global $wpdb;
$all_events = $wpdb->get_results("SELECT ID, post_title, post_author, post_status FROM {$wpdb->posts} WHERE post_type='tribe_events' ORDER BY ID DESC LIMIT 10");
foreach ($all_events as $event) {
    echo "ID: {$event->ID}, Title: {$event->post_title}, Author: {$event->post_author}, Status: {$event->post_status}\n";
}
echo "\n";

// 3. Our specific events
echo "3. Our created events (5482-5486):\n";
$our_events = $wpdb->get_results("SELECT ID, post_title, post_author, post_status FROM {$wpdb->posts} WHERE ID IN (5482,5483,5484,5485,5486)");
foreach ($our_events as $event) {
    echo "ID: {$event->ID}, Title: {$event->post_title}, Author: {$event->post_author}, Status: {$event->post_status}\n";
}
echo "\n";

// 4. Test WP_Query with various approaches
echo "4. Testing WP_Query approaches:\n";

// Test A: Simple author query
echo "Test A - Simple author query:\n";
$args_a = array(
    'post_type' => 'tribe_events',
    'author' => 17,
    'posts_per_page' => -1,
    'post_status' => 'any'
);
$query_a = new WP_Query($args_a);
echo "Found: " . $query_a->found_posts . "\n";
echo "SQL: " . $query_a->request . "\n\n";

// Test B: With suppress_filters
echo "Test B - With suppress_filters:\n";
$args_b = $args_a;
$args_b['suppress_filters'] = true;
$query_b = new WP_Query($args_b);
echo "Found: " . $query_b->found_posts . "\n";
echo "SQL: " . $query_b->request . "\n\n";

// Test C: Direct get_posts
echo "Test C - Direct get_posts:\n";
$posts_c = get_posts(array(
    'post_type' => 'tribe_events',
    'author' => 17,
    'posts_per_page' => -1,
    'post_status' => 'any',
    'suppress_filters' => true
));
echo "Found: " . count($posts_c) . "\n\n";

// 5. Check TEC-specific methods
echo "5. TEC-specific methods:\n";
if (class_exists('Tribe__Events__Query')) {
    echo "Using tribe_get_events:\n";
    $tribe_events = tribe_get_events(array(
        'author' => 17,
        'posts_per_page' => -1,
        'post_status' => 'any'
    ));
    echo "Found: " . count($tribe_events) . "\n";
}
echo "\n";

// 6. Check active filters
echo "6. Active filters on pre_get_posts:\n";
global $wp_filter;
if (isset($wp_filter['pre_get_posts'])) {
    foreach ($wp_filter['pre_get_posts'] as $priority => $callbacks) {
        foreach ($callbacks as $callback) {
            if (is_array($callback['function'])) {
                $class = is_object($callback['function'][0]) ? get_class($callback['function'][0]) : $callback['function'][0];
                $method = $callback['function'][1];
                echo "Priority $priority: {$class}::{$method}\n";
            } else {
                echo "Priority $priority: " . $callback['function'] . "\n";
            }
        }
    }
}
echo "\n";

// 7. Check what happens when we query by ID
echo "7. Query by post ID (should work):\n";
$args_id = array(
    'post_type' => 'tribe_events',
    'post__in' => array(5482, 5483, 5484, 5485, 5486),
    'posts_per_page' => -1,
    'post_status' => 'any'
);
$query_id = new WP_Query($args_id);
echo "Found: " . $query_id->found_posts . "\n";
foreach ($query_id->posts as $post) {
    echo "ID: {$post->ID}, Author: {$post->post_author}, Title: {$post->post_title}\n";
}
echo "\n";

// 8. Test dashboard data class
echo "8. Testing HVAC Dashboard Data class:\n";
require_once '/home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/includes/class-hvac-dashboard-data.php';
$dashboard = new HVAC_Dashboard_Data(17);
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";

echo "\n=== END DEBUG ===\n";
EOF

# Upload and execute
sshpass -p "${UPSKILL_STAGING_PASS}" scp /tmp/debug-events.php "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}:/home/974670.cloudwaysapps.com/uberrxmprk/"
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" "cd /home/974670.cloudwaysapps.com/uberrxmprk && php debug-events.php"

# Clean up
rm /tmp/debug-events.php
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" "rm /home/974670.cloudwaysapps.com/uberrxmprk/debug-events.php"

echo "Debug completed!"