#!/bin/bash

# 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"

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

# Function to check if a command was successful
check_status() {
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}✓ $1${NC}"
        return 0
    else
        echo -e "${RED}✗ $1${NC}"
        return 1
    fi
}

# --- Ensure wp-cli is installed on remote server ---
REMOTE_PLUGIN_PATH="/home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events"
LOCAL_WPCLI_INSTALL_SCRIPT="$SCRIPT_DIR/install-and-verify-wp-cli.sh"
if [ -f "$LOCAL_WPCLI_INSTALL_SCRIPT" ]; then
    echo "Copying wp-cli install script to remote server..."
    sshpass -p "${UPSKILL_STAGING_PASS}" scp -o StrictHostKeyChecking=no "$LOCAL_WPCLI_INSTALL_SCRIPT" "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}:$REMOTE_PLUGIN_PATH/"
    echo "Running wp-cli install script on remote server..."
    sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
        "cd $REMOTE_PLUGIN_PATH && bash install-and-verify-wp-cli.sh"
fi

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

echo "=== Running Tests on Staging Server ==="
echo "Remote host: $UPSKILL_STAGING_IP"
echo "Remote user: $UPSKILL_STAGING_SSH_USER"
echo "Plugin path: $PLUGIN_PATH"
echo "==============================="

# Verify plugin directory exists
echo -e "\n${YELLOW}Verifying plugin directory...${NC}"
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
"[ -d \"$PLUGIN_PATH\" ] || mkdir -p \"$PLUGIN_PATH\""
check_status "Plugin directory verification"

# Copy composer.json to staging
echo -e "\n${YELLOW}Copying composer.json to staging...${NC}"
sshpass -p "${UPSKILL_STAGING_PASS}" scp "$SCRIPT_DIR/staging-composer.json" \
    "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}:${PLUGIN_PATH}/composer.json"
check_status "Composer.json copy"

# Install Composer with custom home directory
echo -e "\n${YELLOW}Setting up test environment...${NC}"
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
"cd $PLUGIN_PATH && \
export COMPOSER_HOME=\"/tmp/composer\" && \
mkdir -p \"/tmp/composer\" && \
if [ ! -f 'composer.phar' ]; then \
    curl -sS https://getcomposer.org/installer | php; \
fi && \
php composer.phar install"
check_status "Composer installation"

# Create PHPUnit configuration
echo -e "\n${YELLOW}Creating PHPUnit configuration...${NC}"
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
"cd $PLUGIN_PATH && \
echo '<?xml version=\"1.0\"?>
<phpunit
    bootstrap=\"tests/bootstrap.php\"
    backupGlobals=\"false\"
    colors=\"true\"
    convertErrorsToExceptions=\"true\"
    convertNoticesToExceptions=\"true\"
    convertWarningsToExceptions=\"true\"
    >
    <testsuites>
        <testsuite name=\"unit\">
            <directory prefix=\"test-\" suffix=\".php\">./tests/unit/</directory>
        </testsuite>
        <testsuite name=\"integration\">
            <directory prefix=\"test-\" suffix=\".php\">./tests/integration/</directory>
        </testsuite>
    </testsuites>
</phpunit>' > phpunit.xml.dist"
check_status "PHPUnit configuration"

# Copy test files to staging
echo -e "\n${YELLOW}Copying test files to staging...${NC}"
sshpass -p "${UPSKILL_STAGING_PASS}" rsync -avz \
    "$SCRIPT_DIR/../tests/" \
    "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}:${PLUGIN_PATH}/tests/"
check_status "Test files copy"

# Activate required plugins on staging
REQUIRED_PLUGINS=(
    "the-events-calendar"
    "events-calendar-pro"
    "the-events-calendar-community-events"
    "event-tickets"
    "event-tickets-plus"
    "hvac-community-events"
)

echo -e "\n${YELLOW}Activating required plugins on staging...${NC}"
for plugin in "${REQUIRED_PLUGINS[@]}"; do
    sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
    "cd ${UPSKILL_STAGING_PATH} && wp plugin activate $plugin --allow-root"
    check_status "Plugin activation on staging: $plugin"
done

# Flush rewrite rules on staging
echo -e "\n${YELLOW}Flushing rewrite rules on staging...${NC}"
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
"cd ${UPSKILL_STAGING_PATH} && wp rewrite flush --hard --allow-root"
check_status "Flushing rewrite rules on staging"

# Run unit tests
echo -e "\n${YELLOW}Running unit tests...${NC}"
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
"cd $PLUGIN_PATH && \
./vendor/bin/phpunit --testsuite unit --verbose"
check_status "Unit tests"

# Run integration tests
echo -e "\n${YELLOW}Running integration tests...${NC}"
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
"cd $PLUGIN_PATH && \
./vendor/bin/phpunit --testsuite integration --verbose"
check_status "Integration tests"

echo -e "\n${GREEN}Testing completed!${NC}"
echo "Check the output above for test results and any failures."