#!/bin/bash

# Fix event dates with a mix of past and future dates

# 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 "=== Fixing Event Dates (Mixed Past/Future) ==="
echo "Remote host: $UPSKILL_STAGING_IP"
echo "==============================="

# Fix event dates
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 "Updating event dates with mix of past and future..."

# Event 5482: Past event (2 months ago)
wp post meta update 5482 _EventStartDate "2025-03-15 09:00:00" --allow-root
wp post meta update 5482 _EventEndDate "2025-03-15 17:00:00" --allow-root
echo "Event 5482: Set to past (March 2025)"

# Event 5483: Past event (1 month ago)  
wp post meta update 5483 _EventStartDate "2025-04-15 10:00:00" --allow-root
wp post meta update 5483 _EventEndDate "2025-04-15 18:00:00" --allow-root
echo "Event 5483: Set to past (April 2025)"

# Event 5484: Future event (1 month from now)
wp post meta update 5484 _EventStartDate "2025-06-15 09:00:00" --allow-root
wp post meta update 5484 _EventEndDate "2025-06-15 16:00:00" --allow-root
echo "Event 5484: Set to future (June 2025)"

# Event 5485: Future event (2 months from now)
wp post meta update 5485 _EventStartDate "2025-07-20 13:00:00" --allow-root
wp post meta update 5485 _EventEndDate "2025-07-20 17:00:00" --allow-root
echo "Event 5485: Set to future (July 2025)"

# Event 5486: Future event (3 months from now)
wp post meta update 5486 _EventStartDate "2025-08-25 08:00:00" --allow-root
wp post meta update 5486 _EventEndDate "2025-08-25 17:00:00" --allow-root
echo "Event 5486: Set to future (August 2025)"

echo -e "\nEvent dates updated!"

# Verify the updates
echo -e "\nVerifying updated dates:"
for EVENT_ID in 5482 5483 5484 5485 5486; do
    TITLE=$(wp post get $EVENT_ID --field=post_title --allow-root)
    START_DATE=$(wp post meta get $EVENT_ID _EventStartDate --allow-root)
    echo "Event $EVENT_ID ($TITLE): $START_DATE"
done

# Test query for all events
echo -e "\nAll events by test_trainer:"
wp post list --post_type=tribe_events --author=17 --post_status=any --fields=ID,post_title --allow-root

# Test past events query
echo -e "\nPast events (should be 2):"
wp db query "SELECT COUNT(*) as count FROM wp_posts p 
    JOIN wp_postmeta m ON p.ID = m.post_id 
    WHERE p.post_type='tribe_events' 
    AND p.post_author=17 
    AND m.meta_key='_EventEndDate' 
    AND m.meta_value < NOW()" --allow-root

# Test future events query
echo -e "\nFuture events (should be 3):"
wp db query "SELECT COUNT(*) as count FROM wp_posts p 
    JOIN wp_postmeta m ON p.ID = m.post_id 
    WHERE p.post_type='tribe_events' 
    AND p.post_author=17 
    AND m.meta_key='_EventStartDate' 
    AND m.meta_value > NOW()" --allow-root
EOF

echo "Fix completed!"