#!/bin/bash

# Load environment variables
if [ -f "./.env" ]; then
    source ./.env
else
    echo "Error: .env file not found!"
    exit 1
fi

# Configuration
SSH_USER="${UPSKILL_STAGING_SSH_USER}"
SSH_HOST="${UPSKILL_STAGING_IP}"
SSH_PASS="${UPSKILL_STAGING_PASS}"
SITE_PATH="${UPSKILL_STAGING_PATH:-/home/974670.cloudwaysapps.com/uberrxmprk/public_html}"

# Check if required variables are set
if [ -z "$SSH_USER" ] || [ -z "$SSH_HOST" ] || [ -z "$SSH_PASS" ]; then
    echo "Error: Required environment variables not set. Please check your .env file."
    exit 1
fi

# Function to create a user with the trainer role
create_test_user() {
    local username=$1
    local email=$2
    local password=$3
    local first_name=$4
    local last_name=$5
    local role=$6
    local business_name=$7
    local business_phone=$8
    local business_email=$9

    echo "Creating user: $username ($email) with role: $role"
    
    # Check if user already exists
    USER_EXISTS=$(sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SSH_HOST" "cd $SITE_PATH && wp user get $username --field=ID 2>/dev/null || echo ''")
    
    if [ -n "$USER_EXISTS" ]; then
        echo "User $username already exists with ID: $USER_EXISTS"
        
        # Update user if exists
        sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SSH_HOST" "cd $SITE_PATH && wp user update $USER_EXISTS --user_pass='$password' --first_name='$first_name' --last_name='$last_name' --role='$role'"
        echo "Updated user password, name, and role"
    else
        # Create user
        USER_ID=$(sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SSH_HOST" "cd $SITE_PATH && wp user create $username $email --user_pass='$password' --first_name='$first_name' --last_name='$last_name' --role='$role' --porcelain")
        
        if [ -z "$USER_ID" ]; then
            echo "Error: Failed to create user $username"
            return 1
        fi
        
        echo "Created user $username with ID: $USER_ID"
    fi
    
    # Add user meta for business details
    if [ -n "$business_name" ]; then
        sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SSH_HOST" "cd $SITE_PATH && wp user meta update $username business_name '$business_name'"
    fi
    
    if [ -n "$business_phone" ]; then
        sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SSH_HOST" "cd $SITE_PATH && wp user meta update $username business_phone '$business_phone'"
    fi
    
    if [ -n "$business_email" ]; then
        sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SSH_HOST" "cd $SITE_PATH && wp user meta update $username business_email '$business_email'"
    fi
    
    echo "Updated business details for $username"
    return 0
}

# Create test_trainer user
echo "=== Creating test trainer user ==="
create_test_user "${TEST_USER_USERNAME:-test_trainer}" "${TEST_USER_EMAIL:-test_trainer@example.com}" "${TEST_USER_PASSWORD:-Test123!}" "Test" "Trainer" "${TEST_USER_ROLE:-hvac_trainer}" "Test HVAC Training" "555-0123" "business@testtraining.com"
echo ""

# Create admin_trainer user
echo "=== Creating admin trainer user ==="
create_test_user "admin_trainer" "admin_trainer@example.com" "${ADMIN_USER_PASSWORD:-Admin123!}" "Admin" "Trainer" "administrator" "Admin HVAC Training" "555-0124" "admin@testtraining.com"
echo ""

# Create pending_trainer user
echo "=== Creating pending trainer user ==="
create_test_user "pending_trainer" "pending_trainer@example.com" "${PENDING_USER_PASSWORD:-Pending123!}" "Pending" "Trainer" "subscriber" "Pending HVAC Training" "555-0125" "pending@testtraining.com"
echo ""

echo "Test users created successfully!"