#!/bin/bash

## Description: Minimal initialization for automated testing on an already installed site - adds testing users and prepares the site without applying extra recipes
## Usage: init-minimal-automated-testing
## Example: ddev init-minimal-automated-testing
## Aliases: varbase:init-minimal-automated-testing

echo "=== Minimal automated testing initialization for Varbase ==="

# ------------------------------------------------------------------
# 0. Check that Drupal is installed
# ------------------------------------------------------------------
echo ""
echo "--- Checking if Drupal is installed ---"

BOOTSTRAP=$(drush status --field=bootstrap 2>/dev/null)
if [ "$BOOTSTRAP" != "Successful" ]; then
  echo "  Error: Drupal is not installed."
  echo "  Please install Varbase first:"
  echo "    ddev install-varbase minimal"
  echo "    ddev install-varbase full"
  echo ""
  exit 1
fi
echo "  Drupal is installed. Proceeding..."

# ------------------------------------------------------------------
# 1. Add testing users
# ------------------------------------------------------------------
echo ""
echo "--- Adding testing users ---"

current_path=$(pwd);

# Include the Bash YAML library.
source ${current_path}/.ddev/commands/web/libs/bash-yaml.sh || exit 1 ;

# Load the list of default users for Varbase.
eval $(parse_yaml ${current_path}/.ddev/commands/web/assets/varbase.users.yml);

for user in ${users[@]}
do
  user_name="user_${user}_name";
  user_mail="user_${user}_mail";
  user_password="user_${user}_password";
  user_role="user_${user}_role";

  echo " ---------------------------------------------------------------- ";
  echo "      User name: ${!user_name}";
  echo "      User mail: ${!user_mail}";
  echo "  User password: ${!user_password}";
  echo "      User role: ${!user_role}";
  echo " ================================================================= ";

  drush user:create "${!user_name}" --mail="${!user_mail}" --password="${!user_password}" ;
  if [ "${!user_role}" == '_none_' ] ; then
    echo "   No user role for this user" ;
  else
    drush user:role:add "${!user_role}" "${!user_name}" ;
  fi

done

# ------------------------------------------------------------------
# 2. Prepare site for automated testing
# ------------------------------------------------------------------
echo ""
echo "--- Disabling antibot module ---"
drush pm:uninstall antibot --yes 2>/dev/null || echo "  Antibot module not found or already disabled"

echo ""
echo "--- Disabling CSS/JS aggregation ---"
drush config:set system.performance css.preprocess 0 --yes
drush config:set system.performance js.preprocess 0 --yes

echo ""
echo "--- Setting verbose error logging ---"
drush config:set system.logging error_level all --yes

echo ""
echo "--- Rebuilding cache ---"
drush cache:rebuild

echo ""
echo "=== Minimal automated testing initialization complete ==="
echo ""
echo "Now run on the host:"
echo "  ddev yarn install"
echo "  ddev npx playwright install-deps"
echo "  ddev npx playwright install"
echo "  ddev yarn test:chromium"
echo ""
