#!/bin/bash

#############################################################################
# Test Script - Verify All New Variables Are Exported
# This script tests that all new SYS_* variables are properly initialized
#############################################################################

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

echo "=========================================="
echo "Testing Variable Exports"
echo "=========================================="
echo

# Source the system detection and variable export
source "$SCRIPT_DIR/lib/common-functions.sh"
source "$SCRIPT_DIR/lib/system-detect.sh"
source "$SCRIPT_DIR/lib/log-paths.sh"
source "$SCRIPT_DIR/lib/database-paths.sh"
source "$SCRIPT_DIR/lib/service-info.sh"
source "$SCRIPT_DIR/lib/control-panel-paths.sh"
source "$SCRIPT_DIR/lib/web-server-config.sh"
source "$SCRIPT_DIR/lib/firewall-operations.sh"
source "$SCRIPT_DIR/lib/security-tools.sh"
source "$SCRIPT_DIR/lib/system-authentication.sh"
source "$SCRIPT_DIR/lib/system-variables.sh"

# Test counters
TESTS_PASSED=0
TESTS_FAILED=0

# Function to test variable existence
test_variable() {
    local var_name="$1"
    local var_value="${!var_name}"

    if [ -n "${var_value:-}" ]; then
        echo "  ✅ $var_name = $var_value"
        ((TESTS_PASSED++))
    else
        echo "  ⚠️  $var_name (empty or not set)"
    fi
}

# Function to test optional variable existence (may be empty on some systems)
test_optional_variable() {
    local var_name="$1"
    local var_value="${!var_name}"

    if [ -n "${var_value:-}" ]; then
        echo "  ✅ $var_name = $var_value"
        ((TESTS_PASSED++))
    else
        echo "  ℹ️  $var_name (not installed on this system)"
    fi
}

echo "📋 SYSTEM DETECTION VARIABLES"
echo "=============================="
test_variable "SYS_CONTROL_PANEL"
test_variable "SYS_OS_TYPE"
test_variable "SYS_WEB_SERVER"
test_variable "SYS_DB_TYPE"
test_variable "SYS_MAIL_SYSTEM"
test_variable "SYS_FIREWALL"
echo

echo "📧 MAIL COMMAND VARIABLES"
echo "=========================="
test_optional_variable "SYS_MAIL_CMD_QUEUE_COUNT"
test_optional_variable "SYS_MAIL_CMD_QUEUE_LIST"
test_optional_variable "SYS_MAIL_SPOOL"
test_optional_variable "SYS_MAIL_BIN_EXIM"
test_optional_variable "SYS_MAIL_BIN_POSTFIX"
echo

echo "💾 DATABASE COMMAND VARIABLES"
echo "============================="
test_variable "SYS_DB_CLI_COMMAND"
test_variable "SYS_DB_DUMP_COMMAND"
test_variable "SYS_DB_ADMIN_COMMAND"
test_optional_variable "SYS_DB_CHECK_COMMAND"
test_optional_variable "SYS_DB_STATUS_COMMAND"
echo

echo "🛡️  SECURITY SCANNER VARIABLES"
echo "=============================="
test_optional_variable "SYS_SCANNER_CLAMAV"
test_optional_variable "SYS_SCANNER_MALDET"
test_optional_variable "SYS_SCANNER_RKHUNTER"
test_optional_variable "SYS_SCANNER_IMUNIFY"
test_optional_variable "SYS_FAIL2BAN_CLIENT"
echo

echo "🔐 CONTROL PANEL TOOLS"
echo "======================"
test_optional_variable "SYS_CPANEL_WHMAPI"
test_optional_variable "SYS_CPANEL_UAPI"
test_optional_variable "SYS_PLESK_API"
test_optional_variable "SYS_INTERWORX_BIN"
echo

echo "📝 AUTHENTICATION FILE VARIABLES"
echo "================================"
test_variable "SYS_AUTH_PASSWD_FILE"
test_variable "SYS_AUTH_SHADOW_FILE"
test_variable "SYS_AUTH_SUDOERS_FILE"
test_variable "SYS_AUTH_CRONTAB_DIR"
test_variable "SYS_LOG_CRON"
echo

echo "👤 USER/GROUP ID VARIABLES"
echo "==========================="
echo "  Web Server UIDs:"
test_variable "SYS_WEB_UID"
test_variable "SYS_WEB_GID"
echo "  Database UIDs:"
test_variable "SYS_DB_UID"
test_variable "SYS_DB_GID"
echo "  Mail System UIDs:"
test_variable "SYS_MAIL_UID"
test_variable "SYS_MAIL_GID"
echo

echo "=========================================="
echo "SUMMARY"
echo "=========================================="
echo "Variables checked: $(($TESTS_PASSED))"
echo ""

# Additional info
echo "📊 System Information:"
echo "  Control Panel: $SYS_CONTROL_PANEL"
echo "  OS: $SYS_OS_TYPE"
echo "  Web Server: $SYS_WEB_SERVER (user: $SYS_WEB_USER)"
echo "  Database: $SYS_DB_TYPE (socket: $SYS_DB_SOCKET)"
echo "  Mail System: $SYS_MAIL_SYSTEM"
echo "  Firewall: $SYS_FIREWALL"
echo ""

# Test that functions work
echo "🧪 TESTING FIREWALL FUNCTIONS"
echo "=============================="
if declare -f firewall_block_ip >/dev/null 2>&1; then
    echo "  ✅ firewall_block_ip() is exported and available"
else
    echo "  ❌ firewall_block_ip() is NOT available"
    ((TESTS_FAILED++))
fi

if declare -f firewall_is_blocked >/dev/null 2>&1; then
    echo "  ✅ firewall_is_blocked() is exported and available"
else
    echo "  ❌ firewall_is_blocked() is NOT available"
    ((TESTS_FAILED++))
fi

if declare -f firewall_bulk_block_ips >/dev/null 2>&1; then
    echo "  ✅ firewall_bulk_block_ips() is exported and available"
else
    echo "  ❌ firewall_bulk_block_ips() is NOT available"
    ((TESTS_FAILED++))
fi

echo
echo "=========================================="
if [ $TESTS_FAILED -eq 0 ]; then
    echo "✅ ALL TESTS PASSED"
else
    echo "⚠️  FAILURES: $TESTS_FAILED"
fi
echo "=========================================="
