#!/bin/bash

# Create test events on staging
source .env

echo "=== Creating Test Events on Staging ==="

# Execute directly via SSH  
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no $UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP << 'REMOTE_COMMANDS'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

# Get test_trainer user ID
TRAINER_ID=$(wp user get test_trainer --field=ID 2>/dev/null)

if [ -z "$TRAINER_ID" ]; then
    echo "test_trainer user not found, creating..."
    wp user create test_trainer test_trainer@example.com --user_pass=TestTrainer123! --role=hvac_trainer --first_name=Test --last_name=Trainer
    TRAINER_ID=$(wp user get test_trainer --field=ID)
fi

echo "Using trainer ID: $TRAINER_ID"

# Create sample events
echo ""
echo "Creating events..."

# Event 1: Basic HVAC Training
wp post create --post_type=tribe_events --post_title="Basic HVAC System Training" --post_status=publish --post_author=$TRAINER_ID --post_content="Learn the fundamentals of HVAC systems including installation, maintenance, and troubleshooting." --porcelain

# Event 2: Advanced Diagnostics
wp post create --post_type=tribe_events --post_title="Advanced HVAC Diagnostics Workshop" --post_status=publish --post_author=$TRAINER_ID --post_content="Master advanced diagnostic techniques for commercial and residential HVAC systems." --porcelain

# Event 3: Energy Efficiency
wp post create --post_type=tribe_events --post_title="Energy Efficient HVAC Systems" --post_status=publish --post_author=$TRAINER_ID --post_content="Learn about the latest energy-efficient HVAC technologies and best practices." --porcelain

# Add event metadata for the first event
EVENT_ID=$(wp post list --post_type=tribe_events --post_status=publish --format=ids --posts_per_page=1)

if [ ! -z "$EVENT_ID" ]; then
    echo ""
    echo "Adding metadata to event $EVENT_ID..."
    
    # Set event dates
    wp post meta update $EVENT_ID _EventStartDate "2025-08-20 09:00:00"
    wp post meta update $EVENT_ID _EventEndDate "2025-08-20 17:00:00"
    wp post meta update $EVENT_ID _EventCost "299"
    wp post meta update $EVENT_ID _EventURL "https://upskill-staging.measurequick.com"
    
    # Set venue info
    wp post meta update $EVENT_ID _EventVenue "HVAC Training Center"
    wp post meta update $EVENT_ID _EventAddress "123 Main St"
    wp post meta update $EVENT_ID _EventCity "New York"
    wp post meta update $EVENT_ID _EventState "NY"
    wp post meta update $EVENT_ID _EventZip "10001"
    wp post meta update $EVENT_ID _EventCountry "United States"
fi

echo ""
echo "Verifying events..."
wp post list --post_type=tribe_events --fields=ID,post_title,post_status,post_author

echo ""
echo "Done!"
REMOTE_COMMANDS

echo ""
echo "=== Test Events Created ==="
echo ""
echo "View events at:"
echo "- TEC Network: https://upskill-staging.measurequick.com/events/network/"
echo "- Add Event: https://upskill-staging.measurequick.com/events/network/add/"
echo "- Edit Event: https://upskill-staging.measurequick.com/events/network/edit/[EVENT_ID]"