#!/bin/bash

# Run unit tests on staging server with safety measures
# Enhanced version with path validation and better error handling

# Get absolute path to this script's directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Load environment variables
ENV_FILE="$SCRIPT_DIR/../.env"
if [ ! -f "$ENV_FILE" ]; then
    echo "Error: .env file not found at: $ENV_FILE"
    exit 1
fi

source "$ENV_FILE"

# 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"
BOOTSTRAP_FILE="$PLUGIN_PATH/tests/bootstrap-staging.php"

# 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

# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo "=== Running Unit Tests on Staging ==="
echo "Remote host: $UPSKILL_STAGING_IP"
echo "Remote user: $UPSKILL_STAGING_SSH_USER"
echo "Plugin path: $PLUGIN_PATH"
echo "Bootstrap file: $BOOTSTRAP_FILE"
echo "==============================="

# Verify test files exist on staging
echo "Verifying test files on staging server..."
# Define config file path
STAGING_CONFIG="$PLUGIN_PATH/tests/wp-tests-config-staging.php"

sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" \
    "test -f $BOOTSTRAP_FILE && test -f $STAGING_CONFIG"
if [ $? -ne 0 ]; then
    echo -e "${RED}Error: Required test files not found${NC}"
    echo -e "${YELLOW}Please ensure the following files exist:${NC}"
    echo -e "${YELLOW}- Bootstrap file: $BOOTSTRAP_FILE${NC}"
    echo -e "${YELLOW}- Config file: $STAGING_CONFIG${NC}"
    exit 1
fi

# Create test results directory if it doesn't exist
echo "Creating test results directory..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" \
    "mkdir -p $PLUGIN_PATH/test-results"

# Use staging test configuration
echo "Using staging test configuration..."
STAGING_CONFIG="$PLUGIN_PATH/tests/wp-tests-config-staging.php"

sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" \
    "cd $PLUGIN_PATH && \
    if command -v phpunit &> /dev/null; then
        PHPUNIT_CMD='phpunit'
    else
        PHPUNIT_CMD='./vendor/bin/phpunit'
    fi && \
    export WP_PHPUNIT__DIR=$PLUGIN_PATH/vendor/wp-phpunit/wp-phpunit && \
    export WP_TESTS_CONFIG_FILE_PATH=$STAGING_CONFIG && \
    export WP_TESTS_CONFIG_FILE_PATH_OVERRIDE=$STAGING_CONFIG && \
    \$PHPUNIT_CMD --bootstrap $BOOTSTRAP_FILE $PLUGIN_PATH/tests/unit/ --verbose --no-configuration" > test-output.log 2>&1

# Capture and handle test results
TEST_RESULT=$?
if [ $TEST_RESULT -eq 0 ]; then
    echo -e "${GREEN}✓ Unit tests completed successfully${NC}"
    exit 0
else
    echo -e "${RED}✗ Unit tests failed${NC}"
    
    # Capture test output for debugging
    echo "Capturing test output for debugging..."
    sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" \
        "cd $PLUGIN_PATH && \
        if command -v phpunit &> /dev/null; then
            PHPUNIT_CMD='phpunit'
        else
            PHPUNIT_CMD='./vendor/bin/phpunit'
        fi && \
        export WP_PHPUNIT__DIR=$PLUGIN_PATH/vendor/wp-phpunit/wp-phpunit && \
        export WP_TESTS_CONFIG_FILE_PATH=$STAGING_CONFIG && \
        export WP_TESTS_CONFIG_FILE_PATH_OVERRIDE=$STAGING_CONFIG && \
        \$PHPUNIT_CMD --bootstrap $BOOTSTRAP_FILE $PLUGIN_PATH/tests/unit/ --verbose --no-configuration" > test-output.log 2>&1
    
    echo -e "${YELLOW}Test output saved to test-output.log${NC}"
    exit $TEST_RESULT
fi