#!/bin/bash
set -e

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

# Load environment variables
if [ -f .env ]; then
    export $(cat .env | sed 's/#.*//g' | xargs)
fi

echo -e "${YELLOW}Creating test users on staging server...${NC}"

# Create test_trainer user
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

# Check if test_trainer exists
if wp user get test_trainer --field=ID 2>/dev/null; then
    echo "User test_trainer already exists, updating password..."
    wp user update test_trainer --user_pass=TestTrainer123!
else
    echo "Creating test_trainer user..."
    wp user create test_trainer test@example.com --role=hvac_trainer --user_pass=TestTrainer123! --display_name="Test Trainer" --first_name="Test" --last_name="Trainer"
fi

# Create test_master user for master trainer testing
if wp user get test_master test_master@example.com --field=ID 2>/dev/null; then
    echo "User test_master already exists, updating password..."
    wp user update test_master --user_pass=TestMaster123!
else
    echo "Creating test_master user..."
    wp user create test_master test_master@example.com --role=hvac_master_trainer --user_pass=TestMaster123! --display_name="Test Master" --first_name="Test" --last_name="Master"
fi

echo "Test users created successfully!"
echo "  - test_trainer / TestTrainer123! (hvac_trainer role)"
echo "  - test_master / TestMaster123! (hvac_master_trainer role)"
EOF

echo -e "${GREEN}Test users created successfully!${NC}"
echo "Credentials:"
echo "  - Username: test_trainer"
echo "  - Password: TestTrainer123!"
echo "  - Role: hvac_trainer"
echo ""
echo "  - Username: test_master"  
echo "  - Password: TestMaster123!"
echo "  - Role: hvac_master_trainer"