#!/bin/bash

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

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

source "$ENV_FILE"

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

REMOTE_HOST="${UPSKILL_STAGING_IP}"
REMOTE_USER="${UPSKILL_STAGING_SSH_USER}"
REMOTE_PATH_BASE="/home/974670.cloudwaysapps.com/uberrxmprk/public_html"
PLUGIN_SLUG="hvac-community-events"
REMOTE_PLUGIN_PATH="${REMOTE_PATH_BASE}/wp-content/plugins/${PLUGIN_SLUG}"

echo "=== Deploying Test Configuration ==="
echo "Remote host: $REMOTE_HOST"
echo "Remote user: $REMOTE_USER"
echo "Remote plugin path: $REMOTE_PLUGIN_PATH"
echo "==============================="
# Verify local files exist
LOCAL_BOOTSTRAP="$PROJECT_ROOT/wordpress-dev/tests/bootstrap-staging.php"
LOCAL_CONFIG="$PROJECT_ROOT/wordpress-dev/tests/wp-tests-config-staging.php"
if [ ! -f "$LOCAL_BOOTSTRAP" ] || [ ! -f "$LOCAL_CONFIG" ]; then
    echo "✗ Required files not found:"
    [ ! -f "$LOCAL_BOOTSTRAP" ] && echo "- Bootstrap file: $LOCAL_BOOTSTRAP"
    [ ! -f "$LOCAL_CONFIG" ] && echo "- Config file: $LOCAL_CONFIG"
    exit 1
fi

# Create and verify tests directory
echo "Creating tests directory..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" \
    "mkdir -p $REMOTE_PLUGIN_PATH/tests && \
     [ -d $REMOTE_PLUGIN_PATH/tests ] && echo 'Directory created successfully'"

if [ $? -ne 0 ]; then
    echo "✗ Failed to create or verify tests directory"
    exit 1
fi

# Set and verify directory permissions
echo "Setting directory permissions..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" \
    "chmod 755 $REMOTE_PLUGIN_PATH/tests && \
     [ -r $REMOTE_PLUGIN_PATH/tests ] && echo 'Permissions set successfully'"

if [ $? -ne 0 ]; then
    echo "✗ Failed to create tests directory"
    exit 1
fi
# Create and verify the bootstrap file using SSH
echo "Creating bootstrap file..."
REMOTE_BOOTSTRAP="$REMOTE_PLUGIN_PATH/tests/bootstrap-staging.php"

# Read local file content
BOOTSTRAP_CONTENT=$(cat "$LOCAL_BOOTSTRAP")

# Use SSH to create the file directly
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" \
    "cat > $REMOTE_BOOTSTRAP << 'EOL'
$BOOTSTRAP_CONTENT
EOL"

if [ $? -ne 0 ]; then
    echo "✗ Failed to create bootstrap file"
    exit 1
fi

# Verify file exists and set permissions
echo "Setting and verifying file permissions..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" \
    "if [ -f $REMOTE_BOOTSTRAP ]; then \
        chmod 644 $REMOTE_BOOTSTRAP && \
        [ -r $REMOTE_BOOTSTRAP ] && \
        echo 'File created and permissions set successfully'; \
     else \
        echo 'Bootstrap file not found after creation'; \
        exit 1; \
     fi"

if [ $? -ne 0 ]; then
    echo "✗ Failed to verify or set file permissions"
    exit 1
fi

# Deploy wp-tests-config-staging.php
echo "Deploying test configuration file..."
REMOTE_CONFIG="$REMOTE_PLUGIN_PATH/tests/wp-tests-config-staging.php"

# Read local config content
CONFIG_CONTENT=$(cat "$LOCAL_CONFIG")

# Use SSH to create the config file
# Ensure tests directory exists
echo "Creating tests directory..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" \
    "mkdir -p $REMOTE_PLUGIN_PATH/tests && chmod 755 $REMOTE_PLUGIN_PATH/tests"

# Create config file using SSH to avoid path issues
echo "Creating config file..."
CONFIG_CONTENT=$(cat "$LOCAL_CONFIG")
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" \
    "cat > $REMOTE_CONFIG << 'EOL'
$CONFIG_CONTENT
EOL"

# Set proper permissions
echo "Setting file permissions..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" \
    "chmod 644 $REMOTE_CONFIG"

if [ $? -ne 0 ]; then
    echo "✗ Failed to create config file"
    exit 1
fi

# Set config file permissions
echo "Setting config file permissions..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" \
    "chmod 644 $REMOTE_CONFIG && \
     [ -r $REMOTE_CONFIG ] && echo 'Config file permissions set successfully'"

if [ $? -ne 0 ]; then
    echo "✗ Failed to set config file permissions"
    exit 1
fi


# Verify deployment
echo "Verifying deployment..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" \
    "test -f $REMOTE_PLUGIN_PATH/tests/bootstrap-staging.php"

if [ $? -eq 0 ]; then
    echo "✓ Test configuration deployed successfully"
    exit 0
else
    echo "✗ Failed to verify test configuration deployment"
    exit 1
fi