#!/bin/bash

# Script to ensure ben@measurequick.com has all required roles
# Must be run on the production server

set -e

USER_EMAIL="ben@measurequick.com"
REQUIRED_ROLES=("administrator" "hvac_trainer" "hvac_master_trainer")

echo "==========================================="
echo "Updating roles for: $USER_EMAIL"
echo "==========================================="

# Check if user exists
echo "Checking if user exists..."
if ! wp user get "$USER_EMAIL" >/dev/null 2>&1; then
    echo "ERROR: User $USER_EMAIL not found!"
    exit 1
fi

# Get current roles
echo -e "\nCurrent roles:"
CURRENT_ROLES=$(wp user get "$USER_EMAIL" --field=roles)
echo "$CURRENT_ROLES"

# Add required roles
echo -e "\nAdding required roles..."
for role in "${REQUIRED_ROLES[@]}"; do
    echo "Adding role: $role"
    wp user add-role "$USER_EMAIL" "$role" 2>/dev/null || echo "  Role $role already assigned or added"
done

# Verify final roles
echo -e "\nFinal roles after update:"
wp user get "$USER_EMAIL" --field=roles

echo -e "\nRole update complete!"
echo "==========================================="