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

# --- Diagnostic: Plugin Activation Status ---
echo "Checking HVAC Community Events plugin activation status on staging..."
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
    "cd $UPSKILL_STAGING_PATH && wp plugin is-active hvac-community-events"
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ HVAC Community Events plugin is ACTIVE${NC}"
else
    echo -e "${RED}✗ HVAC Community Events plugin is NOT ACTIVE${NC}"
fi

# --- Diagnostic: Required Pages Existence ---
REQUIRED_PAGES=("community-login" "trainer-registration" "hvac-dashboard")
for PAGE_SLUG in "${REQUIRED_PAGES[@]}"; do
    echo "Checking existence of page: /$PAGE_SLUG/ ..."
    PAGE_ID=$(sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" \
        "cd $UPSKILL_STAGING_PATH && wp post list --post_type=page --name=$PAGE_SLUG --field=ID --format=ids")
    if [ -n "$PAGE_ID" ]; then
        echo -e "${GREEN}✓ Page '/$PAGE_SLUG/' exists (ID: $PAGE_ID)${NC}"
    else
        echo -e "${RED}✗ Page '/$PAGE_SLUG/' does NOT exist${NC}"
    fi
done

# --- Diagnostic: Environment Variable Check ---
echo "Staging URL: $UPSKILL_STAGING_URL"
echo "Staging Path: $UPSKILL_STAGING_PATH"
echo "Staging SSH User: $UPSKILL_STAGING_SSH_USER"
echo "Staging SSH IP: $UPSKILL_STAGING_IP"
fi

echo "=== Verifying Plugin on Staging ==="
echo "Remote host: $UPSKILL_STAGING_IP"
echo "Remote user: $UPSKILL_STAGING_SSH_USER"
echo "Remote path: /home/974670.cloudwaysapps.com/uberrxmprk/public_html"
echo "==============================="

# Activate required plugins
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...${NC}"
for plugin in "${REQUIRED_PLUGINS[@]}"; do
    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 plugin activate $plugin --allow-root"
    check_status "Plugin activation: $plugin"
done

# Check plugin status
echo -e "\n${YELLOW}Checking plugin status...${NC}"
for plugin in "${REQUIRED_PLUGINS[@]}"; do
    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 plugin list --allow-root | grep $plugin"
    check_status "Plugin status check: $plugin"
done

# Check debug log file
echo -e "\n${YELLOW}Checking 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 && cat wp-content/debug.log | tail -n 50"
check_status "Debug log check"

# Check if required pages exist
echo -e "\n${YELLOW}Verifying required pages...${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 post list --post_type=page --fields=ID,post_name,post_status --format=table --allow-root | grep -E 'community-login|trainer-registration|hvac-dashboard|manage-event|my-events'"
check_status "Page verification"