#!/bin/bash

# Create test users on staging server
source .env

echo "=== Creating Test Users on Staging Server ==="
echo "Server: $UPSKILL_STAGING_IP"
echo "Path: $UPSKILL_STAGING_PATH"

# Create PHP script to add test users
cat << 'EOF' > /tmp/create-test-users.php
<?php
// Load WordPress
require_once('wp-load.php');

echo "Creating test users...\n";

// Create test_trainer user
$user_id = wp_create_user('test_trainer', 'TestTrainer123!', 'test_trainer@example.com');
if (!is_wp_error($user_id)) {
    $user = new WP_User($user_id);
    $user->set_role('hvac_trainer');
    update_user_meta($user_id, 'first_name', 'Test');
    update_user_meta($user_id, 'last_name', 'Trainer');
    update_user_meta($user_id, 'hvac_account_status', 'active');
    echo "✅ Created test_trainer (password: TestTrainer123!)\n";
} else {
    echo "⚠️  test_trainer may already exist\n";
}

// Create test_master user
$user_id = wp_create_user('test_master', 'TestMaster123!', 'test_master@example.com');
if (!is_wp_error($user_id)) {
    $user = new WP_User($user_id);
    $user->set_role('hvac_master_trainer');
    update_user_meta($user_id, 'first_name', 'Test');
    update_user_meta($user_id, 'last_name', 'Master');
    update_user_meta($user_id, 'hvac_account_status', 'active');
    echo "✅ Created test_master (password: TestMaster123!)\n";
} else {
    echo "⚠️  test_master may already exist\n";
}

// Also ensure Joe accounts exist
$joe_user = get_user_by('email', 'JoeMedosch@gmail.com');
if (!$joe_user) {
    $user_id = wp_create_user('joemedosch', 'JoeTrainer2025@', 'JoeMedosch@gmail.com');
    if (!is_wp_error($user_id)) {
        $user = new WP_User($user_id);
        $user->set_role('hvac_master_trainer');
        update_user_meta($user_id, 'first_name', 'Joe');
        update_user_meta($user_id, 'last_name', 'Medosch');
        update_user_meta($user_id, 'hvac_account_status', 'active');
        echo "✅ Created JoeMedosch@gmail.com (password: JoeTrainer2025@)\n";
    }
} else {
    echo "⚠️  JoeMedosch@gmail.com already exists\n";
}

// Check joe@upskillhvac.com
$joe_upskill = get_user_by('email', 'joe@upskillhvac.com');
if ($joe_upskill) {
    // Ensure has both roles
    $joe_upskill->add_role('hvac_trainer');
    $joe_upskill->add_role('hvac_master_trainer');
    update_user_meta($joe_upskill->ID, 'hvac_account_status', 'active');
    echo "✅ Updated joe@upskillhvac.com with both roles\n";
}

echo "\nTest users ready!\n";
EOF

# Copy and execute on staging
echo "Uploading script to staging server..."
sshpass -p "$UPSKILL_STAGING_SSH_PASS" scp -o StrictHostKeyChecking=no /tmp/create-test-users.php $UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP:/tmp/

echo "Executing script on staging server..."
sshpass -p "$UPSKILL_STAGING_SSH_PASS" ssh -o StrictHostKeyChecking=no $UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP << REMOTE_COMMANDS
cd $UPSKILL_STAGING_PATH
cp /tmp/create-test-users.php ./
wp eval-file create-test-users.php
rm create-test-users.php
rm /tmp/create-test-users.php
REMOTE_COMMANDS

echo "=== Test Users Created ==="
echo ""
echo "You can now log in with:"
echo "1. test_trainer / TestTrainer123! (Regular trainer)"
echo "2. test_master / TestMaster123! (Master trainer)"
echo "3. JoeMedosch@gmail.com / JoeTrainer2025@ (Master trainer)"
echo ""
echo "Login at: https://upskill-staging.measurequick.com/training-login/"