#!/bin/bash

# Configure staging test environment with safety measures
# Enhanced version with path validation and dry-run option

# Load environment variables
source $(dirname "$0")/../.env

# Check required variables
if [ -z "$UPSKILL_STAGING_PASS" ] || [ -z "$UPSKILL_STAGING_SSH_USER" ] || [ -z "$UPSKILL_STAGING_IP" ]; then
  echo "Error: Missing required environment variables."
  exit 1
fi

# Define plugin path
PLUGIN_PATH="/home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events"

# Validate plugin path
if [[ "$PLUGIN_PATH" != *"/wp-content/plugins/hvac-community-events"* ]]; then
  echo "Error: Plugin path does not appear to be within the WordPress plugins directory."
  exit 1
fi

# Create required directories on staging
echo "Creating test directories on staging..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" \
  "mkdir -p $PLUGIN_PATH/tests/unit"

# Rsync test files to staging server
echo "Copying test files to staging..."
rsync -avz \
  --exclude '.git' \
  --exclude 'node_modules' \
  "$(dirname "$0")/../tests/" \
  "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP:$PLUGIN_PATH/tests/"

# Create test configuration files
echo "Configuring test environment..."

# wp-tests-config.php
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" \
"cat > $PLUGIN_PATH/wp-tests-config.php" <<'EOF'
<?php
/**
 * The base configuration for WordPress unit tests
 */

if ( ! defined( 'DB_NAME' ) ) define( 'DB_NAME', 'uberrxmprk' );
if ( ! defined( 'DB_USER' ) ) define( 'DB_USER', 'uberrxmprk' );
if ( ! defined( 'DB_PASSWORD' ) ) define( 'DB_PASSWORD', 'vRVr7GJCAZ' );
if ( ! defined( 'DB_HOST' ) ) define( 'DB_HOST', 'localhost' );
if ( ! defined( 'DB_CHARSET' ) ) define( 'DB_CHARSET', 'utf8' );
if ( ! defined( 'DB_COLLATE' ) ) define( 'DB_COLLATE', '' );

if ( ! defined( 'WP_TESTS_DOMAIN' ) ) define( 'WP_TESTS_DOMAIN', 'example.org' );
if ( ! defined( 'WP_TESTS_EMAIL' ) ) define( 'WP_TESTS_EMAIL', 'admin@example.org' );
if ( ! defined( 'WP_TESTS_TITLE' ) ) define( 'WP_TESTS_TITLE', 'Test Blog' );
if ( ! defined( 'WP_PHP_BINARY' ) ) define( 'WP_PHP_BINARY', '/usr/bin/php' );

if ( ! defined( 'WP_DEBUG' ) ) define( 'WP_DEBUG', true );

if ( ! defined( 'ABSPATH' ) ) define( 'ABSPATH', '/home/974670.cloudwaysapps.com/uberrxmprk/public_html/' );

if ( ! defined( 'WPLANG' ) ) define( 'WPLANG', '' );

$table_prefix = 'wp_';
EOF

# bootstrap.php
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" \
"cat > $PLUGIN_PATH/tests/bootstrap.php" <<'EOF'
<?php
/**
 * Bootstrap file for unit tests
 */

// Set up the WordPress test environment
require_once dirname( __DIR__ ) . '/wp-tests-config.php';

// Set path to wp-phpunit
putenv( 'WP_PHPUNIT__DIR=' . dirname( __DIR__ ) . '/vendor/wp-phpunit/wp-phpunit' );

// Load Composer autoloader
require_once dirname( __DIR__ ) . '/vendor/autoload.php';

// Load wp-phpunit bootstrapping
require_once getenv( 'WP_PHPUNIT__DIR' ) . '/includes/functions.php';

// Load our plugin
tests_add_filter( 'muplugins_loaded', function() {
    require dirname( __DIR__ ) . '/hvac-community-events.php';
});

require_once getenv( 'WP_PHPUNIT__DIR' ) . '/includes/bootstrap.php';
EOF

# phpunit.xml.dist
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" \
"cat > $PLUGIN_PATH/phpunit.xml.dist" <<'EOF'
<?xml version="1.0"?>
<phpunit bootstrap="./tests/bootstrap.php"
         colors="true"
         verbose="true">
    <testsuites>
        <testsuite name="unit">
            <directory suffix=".php">./tests/unit</directory>
        </testsuite>
        <testsuite name="integration">
            <directory suffix=".php">./tests/integration</directory>
        </testsuite>
    </testsuites>
    <php>
        <const name="WP_TESTS_CONFIG_FILE_PATH" value="./wp-tests-config.php"/>
    </php>
</phpunit>
EOF

# Install composer dependencies
echo "Installing Composer dependencies..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" \
"cd $PLUGIN_PATH && composer install"

echo "Test environment configuration completed successfully."