#!/bin/bash

# Certificate Fixes Deployment Script
# This script deploys certificate-related fixes to the staging server

# Define colors for output
GREEN="\033[0;32m"
RED="\033[0;31m"
YELLOW="\033[0;33m"
CYAN="\033[0;36m"
NC="\033[0m" # No Color

# Function to print colorful status messages
function echo_status() {
  local color=""
  case "$2" in
    "success") color=$GREEN ;;
    "error") color=$RED ;;
    "warning") color=$YELLOW ;;
    "info") color=$CYAN ;;
    *) color=$NC ;;
  esac
  
  echo -e "${color}[$2] $1${NC}"
}

# SSH connection details
REMOTE_HOST="wordpress-974670-5399585.cloudwaysapps.com"
REMOTE_USER="master_vbwpndkhyx"
REMOTE_PLUGIN_PATH="/home/master/applications/vbwpndkhyx/public_html/wp-content/plugins/hvac-community-events"
LOCAL_PLUGIN_PATH="./wordpress/wp-content/plugins/hvac-community-events"
PLUGIN_SLUG="hvac-community-events"

echo_status "Starting certificate fixes deployment..." "info"

# Validate paths exist
if [ ! -d "$LOCAL_PLUGIN_PATH" ]; then
  echo_status "Error: Local plugin directory does not exist: $LOCAL_PLUGIN_PATH" "error"
  exit 1
fi

if [ ! -d "$LOCAL_PLUGIN_PATH/templates/certificates" ]; then
  echo_status "Error: Local certificate templates directory does not exist" "error"
  exit 1
fi

if [ ! -d "$LOCAL_PLUGIN_PATH/includes/certificates" ]; then
  echo_status "Error: Local certificate includes directory does not exist" "error"
  exit 1
fi

# Create temp directory for our fix script
echo_status "Preparing fix script..." "info"
mkdir -p "$LOCAL_PLUGIN_PATH/tmp-fixes"
cp "./bin/fix-certificate-reports.php" "$LOCAL_PLUGIN_PATH/tmp-fixes/"

# Create backup of plugin directory on staging server
echo_status "Creating backup of certificate files on staging server..." "info"
ssh "$REMOTE_USER@$REMOTE_HOST" "mkdir -p \"${REMOTE_PLUGIN_PATH}/backups\" && \
  cp -r \"${REMOTE_PLUGIN_PATH}/templates/certificates\" \"${REMOTE_PLUGIN_PATH}/backups/certificates_templates_backup_$(date +%Y%m%d%H%M%S)\" && \
  cp -r \"${REMOTE_PLUGIN_PATH}/includes/certificates\" \"${REMOTE_PLUGIN_PATH}/backups/certificates_includes_backup_$(date +%Y%m%d%H%M%S)\""

if [ $? -ne 0 ]; then
  echo_status "Warning: Failed to create backup on staging server." "warning"
fi

# Rsync the certificate files
echo_status "Deploying certificate fixes to staging server..." "info"

# Sync certificate template files
rsync -avz --delete \
  "$LOCAL_PLUGIN_PATH/templates/certificates/" \
  "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PLUGIN_PATH/templates/certificates/"

if [ $? -ne 0 ]; then
  echo_status "Error: Failed to sync certificate templates." "error"
  exit 1
fi

# Sync certificate class files
rsync -avz --delete \
  "$LOCAL_PLUGIN_PATH/includes/certificates/" \
  "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PLUGIN_PATH/includes/certificates/"

if [ $? -ne 0 ]; then
  echo_status "Error: Failed to sync certificate classes." "error"
  exit 1
fi

# Sync fix script
rsync -avz \
  "$LOCAL_PLUGIN_PATH/tmp-fixes/fix-certificate-reports.php" \
  "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PLUGIN_PATH/fix-certificate-reports.php"

if [ $? -ne 0 ]; then
  echo_status "Error: Failed to sync fix script." "error"
  exit 1
fi

# Run fix script on remote server
echo_status "Running certificate fix script on staging server..." "info"
ssh "$REMOTE_USER@$REMOTE_HOST" "cd /home/master/applications/vbwpndkhyx/public_html && php -f $REMOTE_PLUGIN_PATH/fix-certificate-reports.php"

if [ $? -ne 0 ]; then
  echo_status "Warning: Fix script execution may have encountered issues." "warning"
fi

# Clean up
echo_status "Cleaning up temporary files..." "info"
rm -rf "$LOCAL_PLUGIN_PATH/tmp-fixes"
ssh "$REMOTE_USER@$REMOTE_HOST" "rm -f $REMOTE_PLUGIN_PATH/fix-certificate-reports.php"

echo_status "Certificate fixes deployment completed successfully!" "success"

# Run final test to verify the fixes
echo_status "Running final verification test..." "info"
node ./bin/final-test.js

echo_status "All tasks completed. Please check verification results above." "success"