#!/bin/bash

# Deploy WordPress plugin to staging server with safety measures
# Enhanced version with path validation and dry-run option

# Load configuration file
if [ -z "$1" ]; then
  echo "Usage: $0 --config <config_file> [--dry-run]"
  exit 1
fi

DRY_RUN=false

while [ "$1" != "" ]; do
  case $1 in
    --config )
      shift
      if [ -z "$1" ]; then
        echo "Error: --config requires a value"
        exit 1
      fi
      CONFIG_FILE="$1"
      shift
      ;;
    --dry-run )
      DRY_RUN=true
      shift
      ;;
    * )
      echo "Error: Invalid argument: $1"
      exit 1
  esac
done

if [ ! -f "$CONFIG_FILE" ]; then
  echo "Error: Configuration file not found: $CONFIG_FILE"
  exit 1
fi

source "$CONFIG_FILE"

# Check required variables
if [ -z "$REMOTE_HOST" ] || [ -z "$REMOTE_USER" ] || [ -z "$REMOTE_PATH_BASE" ] || [ -z "$PLUGIN_SLUG" ] || [ -z "$REMOTE_PLUGIN_PATH" ] || [ -z "$LOCAL_PLUGIN_PATH" ]; then
  echo "Error: Missing required variables in configuration file."
  exit 1
fi

# Validate paths to ensure we're only modifying plugin directory
if [[ "$REMOTE_PLUGIN_PATH" != *"/wp-content/plugins/$PLUGIN_SLUG"* ]]; then
  echo "Error: Remote plugin path does not appear to be within the WordPress plugins directory."
  echo "Expected path pattern: */wp-content/plugins/$PLUGIN_SLUG*"
  echo "Actual path: $REMOTE_PLUGIN_PATH"
  exit 1
fi

if [[ "$LOCAL_PLUGIN_PATH" != *"/wp-content/plugins/$PLUGIN_SLUG"* ]]; then
  echo "Error: Local plugin path does not appear to be within the WordPress plugins directory."
  echo "Expected path pattern: */wp-content/plugins/$PLUGIN_SLUG*"
  echo "Actual path: $LOCAL_PLUGIN_PATH"
  exit 1
fi

# Create backup of plugin directory on staging server
echo "Creating backup of plugin directory on staging server..."
if [ "$DRY_RUN" = false ]; then
  ssh "$REMOTE_USER@$REMOTE_HOST" "if [ -d \"$REMOTE_PLUGIN_PATH\" ]; then cp -r \"$REMOTE_PLUGIN_PATH\" \"${REMOTE_PLUGIN_PATH}_backup_$(date +%Y%m%d%H%M%S)\"; fi"
  if [ $? -ne 0 ]; then
    echo "Warning: Failed to create backup on staging server."
  fi
else
  echo "[DRY RUN] Would execute: ssh \"$REMOTE_USER@$REMOTE_HOST\" \"if [ -d \\\"$REMOTE_PLUGIN_PATH\\\" ]; then cp -r \\\"$REMOTE_PLUGIN_PATH\\\" \\\"${REMOTE_PLUGIN_PATH}_backup_$(date +%Y%m%d%H%M%S)\\\"; fi\""
fi

# Rsync the plugin files
echo "Deploying plugin $PLUGIN_SLUG to staging server..."
# Change to project root before rsync
# === Custom: Copy required test config files into plugin directory before rsync ===
cp -f "$PROJECT_ROOT/wordpress-dev/tests/bootstrap-staging.php" "$LOCAL_PLUGIN_PATH/bootstrap-staging.php"
cp -f "$PROJECT_ROOT/wordpress-dev/tests/wp-tests-config-staging.php" "$LOCAL_PLUGIN_PATH/wp-tests-config-staging.php"
# Only copy phpunit-staging.xml if it exists and is explicitly needed for test execution
if [ -f "$PROJECT_ROOT/wordpress-dev/tests/phpunit-staging.xml" ]; then
  cp -f "$PROJECT_ROOT/wordpress-dev/tests/phpunit-staging.xml" "$LOCAL_PLUGIN_PATH/phpunit-staging.xml"
fi
cd ../..
RSYNC_CMD="rsync -avz --delete \
  --exclude '.git' \
  --exclude 'node_modules' \
  --include 'tests/' \
  --include 'tests/unit/' \
  --include 'tests/unit/*.php' \
  --include 'tests/test-doubles.php' \
  --include 'tests/bootstrap.php' \
  --include 'composer.json' \
  --include 'composer.lock' \
  --include 'hvac-community-events.php' \
  --include 'phpunit.xml.dist' \
  --include 'wp-tests-config.php' \
  \"$LOCAL_PLUGIN_PATH/\" \
  \"$REMOTE_USER@$REMOTE_HOST:$REMOTE_PLUGIN_PATH\""

if [ "$DRY_RUN" = true ]; then
  echo "[DRY RUN] Would execute: $RSYNC_CMD"
else
  eval $RSYNC_CMD
  if [ $? -ne 0 ]; then
    echo "Error: rsync failed."
    exit 1
  fi
fi
# Change back to original directory (optional, but good practice)
# cd -

echo "Plugin deployment completed successfully."

# Optional: Install Composer dependencies on staging
echo "Installing Composer dependencies on staging server..."
if [ "$DRY_RUN" = true ]; then
  echo "[DRY RUN] Would execute: ssh \"$REMOTE_USER@$REMOTE_HOST\" \"cd $REMOTE_PLUGIN_PATH && composer install\""
else
  ssh "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PLUGIN_PATH && composer install"
  if [ $? -ne 0 ]; then
    echo "Warning: Composer installation failed."
  fi
fi

# Verify deployment
echo "Verifying deployment..."
if [ "$DRY_RUN" = true ]; then
  echo "[DRY RUN] Would execute: ssh \"$REMOTE_USER@$REMOTE_HOST\" \"ls -la $REMOTE_PLUGIN_PATH\""
else
  ssh "$REMOTE_USER@$REMOTE_HOST" "ls -la $REMOTE_PLUGIN_PATH"
  if [ $? -ne 0 ]; then
    echo "Warning: Verification failed."
  fi
fi

exit 0