#!/bin/bash

# Debug events on staging 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"

# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo "=== Debugging Events on Staging Server ==="
echo "Remote host: $UPSKILL_STAGING_IP"
echo "Remote user: $UPSKILL_STAGING_SSH_USER"
echo "==============================="

# Debug events
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

echo "Test trainer user ID:"
wp user get test_trainer --field=ID --allow-root

echo -e "\nAll events (showing post author):"
wp post list --post_type=tribe_events --fields=ID,post_title,post_author --allow-root

echo -e "\nEvents by test_trainer (using author query):"
wp post list --post_type=tribe_events --author=17 --fields=ID,post_title --allow-root

echo -e "\nDetailed info for event 5482:"
wp post get 5482 --fields=ID,post_title,post_author,post_status --allow-root

echo -e "\nOrganizer metadata for event 5482:"
wp post meta get 5482 _EventOrganizerID --allow-root

echo -e "\nAll metadata for event 5482:"
wp post meta list 5482 --format=table --allow-root | grep -E "_Event|_tribe"

echo -e "\nEvents with any author (testing query):"
wp post list --post_type=tribe_events --posts_per_page=10 --fields=ID,post_author,post_title --allow-root

echo -e "\nWorking directory events:"
wp db query "SELECT ID, post_title, post_author FROM wp_posts WHERE post_type='tribe_events' AND ID IN (5482,5483,5484,5485,5486);" --allow-root
EOF

echo -e "\n${GREEN}Debug completed!${NC}"