#!/bin/bash

# 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 "=== Creating Test Events on Staging Server ==="
echo "Remote host: $UPSKILL_STAGING_IP"
echo "Remote user: $UPSKILL_STAGING_SSH_USER"
echo "WordPress path: $UPSKILL_STAGING_PATH"
echo "==============================="

# Create test events directly via WP-CLI
echo -e "\n${YELLOW}Creating test events...${NC}"

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

# Get trainer user ID
TRAINER_ID=$(wp user get test_trainer --field=ID --allow-root)

# Create Event 1: HVAC System Maintenance Workshop
wp post create \
    --post_type=tribe_events \
    --post_title="HVAC System Maintenance Workshop" \
    --post_content="Learn essential maintenance techniques for residential and commercial HVAC systems." \
    --post_status=publish \
    --post_author=$TRAINER_ID \
    --meta_input='{"_EventStartDate":"2025-02-01 09:00:00","_EventEndDate":"2025-02-01 17:00:00","_EventCost":"200"}' \
    --allow-root

# Create Event 2: Advanced HVAC Diagnostics Training
wp post create \
    --post_type=tribe_events \
    --post_title="Advanced HVAC Diagnostics Training" \
    --post_content="Master diagnostic tools and techniques for troubleshooting complex HVAC issues." \
    --post_status=publish \
    --post_author=$TRAINER_ID \
    --meta_input='{"_EventStartDate":"2025-02-15 08:30:00","_EventEndDate":"2025-02-15 18:30:00","_EventCost":"500"}' \
    --allow-root

# Create Event 3: HVAC Installation Best Practices
wp post create \
    --post_type=tribe_events \
    --post_title="HVAC Installation Best Practices" \
    --post_content="Professional installation methods and safety procedures for HVAC technicians." \
    --post_status=publish \
    --post_author=$TRAINER_ID \
    --meta_input='{"_EventStartDate":"2025-03-01 10:00:00","_EventEndDate":"2025-03-01 16:00:00","_EventCost":"100"}' \
    --allow-root

# Create Event 4: Commercial HVAC Systems Overview
wp post create \
    --post_type=tribe_events \
    --post_title="Commercial HVAC Systems Overview" \
    --post_content="Understanding large-scale commercial HVAC systems and their components." \
    --post_status=publish \
    --post_author=$TRAINER_ID \
    --meta_input='{"_EventStartDate":"2025-03-15 09:00:00","_EventEndDate":"2025-03-15 18:00:00","_EventCost":"750"}' \
    --allow-root

# Create Event 5: HVAC Energy Efficiency Certification
wp post create \
    --post_type=tribe_events \
    --post_title="HVAC Energy Efficiency Certification" \
    --post_content="Green HVAC technologies and energy-saving strategies for modern systems." \
    --post_status=publish \
    --post_author=$TRAINER_ID \
    --meta_input='{"_EventStartDate":"2025-04-01 08:00:00","_EventEndDate":"2025-04-01 17:00:00","_EventCost":"1000"}' \
    --allow-root
EOF

# Verify events were created
echo -e "\n${YELLOW}Verifying test events...${NC}"
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
"cd ${UPSKILL_STAGING_PATH} && wp post list --post_type=tribe_events --author=\$(wp user get test_trainer --field=ID --allow-root) --fields=ID,post_title,post_status --format=table --allow-root"

echo -e "\n${GREEN}Test event creation completed!${NC}"