#!/bin/bash

## Description: Full initialization for automated testing - installs Varbase if needed with the same components and provisioning the GitLab CI varbase-e2e job uses, adds testing users, and prepares the site
## Usage: init-full-automated-testing
## Example: ddev init-full-automated-testing
## Aliases: varbase:init-full-automated-testing

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

# ------------------------------------------------------------------
# 0. Check and install Drupal if not already installed
# ------------------------------------------------------------------
echo ""
echo "--- Checking if Drupal is installed ---"

BOOTSTRAP=$(drush status --field=bootstrap 2>/dev/null)
if [ "$BOOTSTRAP" != "Successful" ]; then
  echo "  Drupal is not installed. Running full Varbase install..."
  echo ""

  drush site:install varbase --yes \
    --account-name=webmaster \
    --account-pass="dD.123123ddd" \
    --account-mail="webmaster@vardot.com" \
    --site-name="$DDEV_PROJECT" \
    --locale=en \
    --no-interaction \
    varbase_multilingual_configuration.enable_multilingual=true \
    varbase_extra_components.vmi=true \
    varbase_extra_components.varbase_heroslider_media=true \
    varbase_extra_components.varbase_carousels=true \
    varbase_extra_components.varbase_search=true \
    varbase_extra_components.varbase_blog=true \
    varbase_extra_components.varbase_auth=true \
    install_configure_form.enable_update_status_emails=NULL

  if [[ $? -ne 0 ]]; then
    echo "  Error: Varbase installation failed."
    exit 1
  fi

  echo ""
  echo "  Rebuilding cache after install..."
  drush cache:rebuild

  # ------------------------------------------------------------------
  # 1. Enable the full Varbase module set (classic profile)
  # ------------------------------------------------------------------
  echo ""
  echo "--- Enabling the full Varbase module set ---"
  # One module at a time: a single incompatible module must not abort the batch.
  drush pm:enable varbase_development --yes || true
  drush pm:enable varbase_styleguide --yes || true
  drush pm:enable varbase_landing --yes || true
  drush pm:enable varbase_api --yes || true
  drush pm:enable varbase_media_instagram --yes || true
  drush pm:enable varbase_media_twitter --yes || true
  drush pm:enable editoria11y --yes || true
  drush pm:enable symfony_mailer --yes || true
  drush pm:enable mailer_policy --yes || true
  drush pm:enable reroute_email --yes || true
  drush pm:enable varbase_content_planner --yes || true
  drush pm:enable social_auth_google social_auth_facebook --yes || true

  # Seed a user-owned Content Kanban activity so the Content Planner dashboard
  # "Recent Kanban Activities" widget renders (same as CI).
  if [ -f /var/www/html/tests/scripts/seed_content_planner_activity.php ]; then
    drush php:script /var/www/html/tests/scripts/seed_content_planner_activity.php || true
  fi

  # ------------------------------------------------------------------
  # 2. Arabic locale (the suite asserts Arabic translations)
  # ------------------------------------------------------------------
  echo ""
  echo "--- Importing the Arabic locale ---"
  drush locale-import ar --autocreate-language /var/www/html/docroot/profiles/contrib/varbase/translations/ar.po \
    || drush locale-import ar --autocreate-language /var/www/html/docroot/profiles/varbase/translations/ar.po || true
  drush pm:enable locale --yes || true
  drush locale:update || true

  echo ""
  echo "--- Configuring reroute email (dev catch-all) ---"
  drush config:set reroute_email.settings enable true --yes || true
  drush config:set reroute_email.settings address 'dev-catchall@vardot.com' --yes || true
  drush config:set reroute_email.settings allowed '*@vardot.com' --yes || true

  echo ""
  echo "--- Rebuilding cache after provisioning ---"
  drush cache:rebuild
else
  echo "  Drupal is already installed. Skipping site install."
fi


# ------------------------------------------------------------------
# 3. 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

# ------------------------------------------------------------------
# 4. 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 "--- Relaxing login flood limits for automated testing ---"
# The varbase-e2e suite logs in many times in a row; the default flood
# limits (ip_limit 5 / user_limit 4) trip "Too many failed login attempts"
# and cascade-fail every login-dependent scenario. Raise the ceilings so the
# suite starts from a clean slate.
drush config:set user.flood ip_limit 1000000 --yes
drush config:set user.flood user_limit 1000000 --yes

echo ""
echo "--- Disabling Honeypot time trap for automated testing ---"
# Honeypot's time_limit rejects fast form submissions ("Please wait N seconds
# and try again"), which breaks scripted form posts. Zero it out; the hidden
# honeypot field protection stays in place.
drush config:set honeypot.settings time_limit 0 --yes || true

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 "=== Full 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 ""
