#!/usr/bin/env bash
#
# Test script to validate the ThrillWiki directory validation fix
#

set -e

# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEPLOY_COMPLETE_SCRIPT="$SCRIPT_DIR/deploy-complete.sh"

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

test_log() {
    echo -e "${BLUE}[TEST]${NC} $1"
}

test_success() {
    echo -e "${GREEN}[PASS]${NC} $1"
}

test_fail() {
    echo -e "${RED}[FAIL]${NC} $1"
}

test_warning() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

echo ""
echo -e "${BLUE}🧪 Testing ThrillWiki Directory Validation Fix${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# Test 1: Check that SSH_OPTIONS is properly defined
test_log "Test 1: Checking SSH_OPTIONS definition in deploy-complete.sh"

if grep -q "SSH_OPTIONS.*IdentitiesOnly.*StrictHostKeyChecking.*UserKnownHostsFile.*ConnectTimeout" "$DEPLOY_COMPLETE_SCRIPT"; then
    test_success "SSH_OPTIONS properly defined with deployment-consistent options"
else
    test_fail "SSH_OPTIONS not properly defined"
    exit 1
fi

# Test 2: Check that BatchMode=yes is removed from validation functions
test_log "Test 2: Checking that BatchMode=yes is removed from validation functions"

# Check if BatchMode=yes is still used in actual SSH commands (not comments)
if grep -n "BatchMode=yes" "$DEPLOY_COMPLETE_SCRIPT" | grep -v "Use deployment-consistent SSH options" | grep -v "# " > /dev/null; then
    test_fail "BatchMode=yes still found in actual SSH commands"
    grep -n "BatchMode=yes" "$DEPLOY_COMPLETE_SCRIPT" | grep -v "Use deployment-consistent SSH options" | grep -v "# "
    exit 1
else
    test_success "No BatchMode=yes found in actual SSH commands (only in comments)"
fi

# Test 3: Check that validation functions use SSH_OPTIONS
test_log "Test 3: Checking that validation functions use SSH_OPTIONS variable"

validation_functions=("test_remote_thrillwiki_installation" "test_remote_services" "test_django_application")
all_use_ssh_options=true

for func in "${validation_functions[@]}"; do
    if grep -A10 "$func" "$DEPLOY_COMPLETE_SCRIPT" | grep -q "SSH_OPTIONS"; then
        test_success "Function $func uses SSH_OPTIONS"
    else
        test_fail "Function $func does not use SSH_OPTIONS"
        all_use_ssh_options=false
    fi
done

if [ "$all_use_ssh_options" = false ]; then
    exit 1
fi

# Test 4: Check that enhanced debugging is present
test_log "Test 4: Checking that enhanced debugging is present in validation"

if grep -q "Enhanced debugging for ThrillWiki directory validation" "$DEPLOY_COMPLETE_SCRIPT"; then
    test_success "Enhanced debugging present in validation function"
else
    test_fail "Enhanced debugging not found in validation function"
    exit 1
fi

# Test 5: Check that alternative path checking is present
test_log "Test 5: Checking that alternative path validation is present"

if grep -q "Checking alternative ThrillWiki paths for debugging" "$DEPLOY_COMPLETE_SCRIPT"; then
    test_success "Alternative path checking present"
else
    test_fail "Alternative path checking not found"
    exit 1
fi

# Test 6: Test SSH command construction (simulation)
test_log "Test 6: Testing SSH command construction"

# Source the SSH_OPTIONS definition
SSH_OPTIONS="-o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=30"
REMOTE_PORT="22"
REMOTE_USER="thrillwiki"
SSH_KEY="/home/test/.ssh/***REMOVED***"
test_host="192.168.20.65"

# Simulate the SSH command construction from the fixed validation function
ssh_cmd="ssh $SSH_OPTIONS -i '$SSH_KEY' -p $REMOTE_PORT $REMOTE_USER@$test_host"

# Check individual components
components_to_check=(
    "IdentitiesOnly=yes"
    "StrictHostKeyChecking=no"
    "UserKnownHostsFile=/dev/null"
    "ConnectTimeout=30"
    "thrillwiki@192.168.20.65"
    "/home/test/.ssh/***REMOVED***"
)

test_success "Constructed SSH command: $ssh_cmd"

for component in "${components_to_check[@]}"; do
    if echo "$ssh_cmd" | grep -q -F "$component"; then
        test_success "SSH command contains: $component"
    else
        test_fail "SSH command missing: $component"
        exit 1
    fi
done

# Check for -i flag separately (without the space that causes grep issues)
if echo "$ssh_cmd" | grep -q "\-i "; then
    test_success "SSH command contains: -i flag"
else
    test_fail "SSH command missing: -i flag"
    exit 1
fi

# Check for -p flag separately
if echo "$ssh_cmd" | grep -q "\-p 22"; then
    test_success "SSH command contains: -p 22"
else
    test_fail "SSH command missing: -p 22"
    exit 1
fi

# Test 7: Verify no BatchMode in constructed command
if echo "$ssh_cmd" | grep -q "BatchMode"; then
    test_fail "SSH command incorrectly contains BatchMode"
    exit 1
else
    test_success "SSH command correctly excludes BatchMode"
fi

echo ""
echo -e "${GREEN}✅ All validation fix tests passed successfully!${NC}"
echo ""
echo "Summary of changes:"
echo "• ✅ Removed BatchMode=yes from all validation SSH commands"
echo "• ✅ Added SSH_OPTIONS variable for deployment consistency"
echo "• ✅ Enhanced debugging for better troubleshooting"
echo "• ✅ Added alternative path checking for robustness"
echo "• ✅ Consistent SSH command construction across all validation functions"
echo ""
echo "Expected behavior:"
echo "• Validation SSH commands now allow interactive authentication"
echo "• SSH connection methods match successful deployment patterns"
echo "• Enhanced debugging will show exact paths and SSH commands"
echo "• Alternative path detection will help diagnose directory location issues"
echo ""