#!/bin/bash
# test-proxmox-interconnectivity.sh
# Tests connectivity between Proxmox instances

set -euo pipefail

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

# Load environment variables
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ -f "${SCRIPT_DIR}/../.env" ]; then
    set -a
    source <(grep -v '^#' "${SCRIPT_DIR}/../.env" | grep -v '^$' | sed 's/^/export /')
    set +a
fi

# Instance configurations
declare -A INSTANCES=(
    ["ml110-01"]="192.168.11.10:https://ml110-01.sankofa.nexus:8006:${PROXMOX_TOKEN_ML110_01:-}"
    ["r630-01"]="192.168.11.11:https://r630-01.sankofa.nexus:8006:${PROXMOX_TOKEN_R630_01:-}"
)

log() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1" >&2
}

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

info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

test_connectivity() {
    local from_name=$1
    local from_ip=$2
    local to_name=$3
    local to_endpoint=$4
    local to_token=$5
    
    info "Testing: ${from_name} (${from_ip}) → ${to_name}"
    
    # Test basic connectivity
    local http_code=$(curl -k -s --connect-timeout 5 -o /dev/null -w "%{http_code}" "${to_endpoint}/api2/json/version" 2>/dev/null || echo "000")
    
    case "$http_code" in
        200)
            log "  ✓ Connected (HTTP 200)"
            if [ -n "$to_token" ]; then
                local version=$(curl -k -s -H "Authorization: PVEAPIToken ${to_token}" "${to_endpoint}/api2/json/version" 2>/dev/null | jq -r '.data.version // empty')
                if [ -n "$version" ]; then
                    log "  ✓ Authenticated - Proxmox version: ${version}"
                fi
            fi
            return 0
            ;;
        401)
            warn "  ⚠ Connected but authentication required (HTTP 401)"
            return 0
            ;;
        000)
            error "  ✗ Connection failed (timeout or unreachable)"
            return 1
            ;;
        *)
            warn "  ⚠ Unexpected response: HTTP ${http_code}"
            return 1
            ;;
    esac
}

test_ping() {
    local from_name=$1
    local from_ip=$2
    local to_name=$3
    local to_ip=$4
    
    info "Testing ping: ${from_name} → ${to_name}"
    
    if command -v ping &> /dev/null; then
        if ping -c 2 -W 2 "${to_ip}" &> /dev/null; then
            log "  ✓ Ping successful"
            return 0
        else
            error "  ✗ Ping failed"
            return 1
        fi
    else
        warn "  ⚠ ping command not available"
        return 0
    fi
}

main() {
    echo ""
    echo "╔══════════════════════════════════════════════════════════════╗"
    echo "║     Proxmox Inter-Instance Connectivity Test                 ║"
    echo "╚══════════════════════════════════════════════════════════════╝"
    echo ""
    
    info "Network Configuration:"
    echo "  • Instance 1: ML110-01 at 192.168.11.10"
    echo "  • Instance 2: R630-01 at 192.168.11.11"
    echo "  • Subnet: 192.168.11.0/24"
    echo ""
    
    local total_errors=0
    local total_tests=0
    
    # Test all combinations
    for from_name in "${!INSTANCES[@]}"; do
        IFS=':' read -r from_ip from_endpoint from_token <<< "${INSTANCES[$from_name]}"
        
        for to_name in "${!INSTANCES[@]}"; do
            if [ "$from_name" != "$to_name" ]; then
                IFS=':' read -r to_ip to_endpoint to_token <<< "${INSTANCES[$to_name]}"
                
                info "=================================="
                ((total_tests++))
                
                # Test ping
                test_ping "$from_name" "$from_ip" "$to_name" "$to_ip" || ((total_errors++))
                echo ""
                
                # Test HTTP connectivity
                test_connectivity "$from_name" "$from_ip" "$to_name" "$to_endpoint" "$to_token" || ((total_errors++))
                echo ""
            fi
        done
    done
    
    echo ""
    echo "═══════════════════════════════════════════════════════════════"
    if [ $total_errors -eq 0 ]; then
        log "✓ All connectivity tests passed! (${total_tests} tests)"
        echo ""
        info "Instances can see each other and are ready for clustering"
    else
        error "✗ Found ${total_errors} issue(s) in ${total_tests} tests"
        echo ""
        warn "Instances may not be able to see each other"
        warn "Check:"
        warn "  • Network connectivity"
        warn "  • Firewall rules"
        warn "  • Routing configuration"
    fi
    echo ""
    
    info "Note: These tests are from THIS machine"
    info "To test FROM the Proxmox instances themselves:"
    info "  1. SSH into each instance"
    info "  2. Run: curl -k https://<other-instance-ip>:8006/api2/json/version"
    echo ""
}

main "$@"

