#!/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
}

echo "=== Enabling Debug Mode on Staging ==="
echo "Remote host: $UPSKILL_STAGING_IP"
echo "Remote user: $UPSKILL_STAGING_SSH_USER"
echo "==============================="

# Create debug configuration
DEBUG_CONFIG="
/* Debug Settings */
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
@ini_set('display_errors', 0);

/* Script Debug */
define('SCRIPT_DEBUG', true);

/* Error Logging */
@ini_set('log_errors', 1);
@ini_set('error_log', dirname(__FILE__) . '/wp-content/debug.log');"

# Add debug configuration to wp-config.php
echo -e "\n${YELLOW}Adding debug configuration to wp-config.php...${NC}"
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
"cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html && \
if ! grep -q 'WP_DEBUG' wp-config.php; then \
    sed -i '/* That'\''s all, stop editing! Happy publishing. */i\\$DEBUG_CONFIG\n' wp-config.php && \
    echo 'Debug configuration added.'; \
else \
    echo 'Debug configuration already exists.'; \
fi"
check_status "Debug configuration"

# Create debug.log if it doesn't exist
echo -e "\n${YELLOW}Creating debug.log file...${NC}"
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
"cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content && \
touch debug.log && \
chmod 666 debug.log"
check_status "Debug log file creation"

# Verify debug settings by checking wp-config.php content
echo -e "\n${YELLOW}Verifying debug settings...${NC}"
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
"cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html && \
grep -A 5 'WP_DEBUG' wp-config.php"
check_status "Debug settings verification"

echo -e "\n${GREEN}Debug mode enabled successfully!${NC}"
echo "Debug log will be written to: wp-content/debug.log"

# Test debug logging
echo -e "\n${YELLOW}Testing debug logging...${NC}"
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
"cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html && \
wp eval 'error_log(\"Debug logging test - \" . date(\"Y-m-d H:i:s\"));' --allow-root && \
echo 'Test log entry written.' && \
echo 'Recent debug log entries:' && \
tail -n 5 wp-content/debug.log"
check_status "Debug log test"