#!/usr/bin/env bash
# Check connectivity status for r630-03 and r630-04
# Usage: ./scripts/check-r630-03-04-connectivity.sh

set -euo pipefail

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

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

echo ""
log_info "═══════════════════════════════════════════════════════════"
log_info "  R630-03 AND R630-04 CONNECTIVITY CHECK"
log_info "═══════════════════════════════════════════════════════════"
echo ""

# Server information
R630_03_IP="192.168.11.13"
R630_03_HOSTNAME="r630-03"
R630_03_EXTERNAL_IP="76.53.10.38"

R630_04_IP="192.168.11.14"
R630_04_HOSTNAME="r630-04"
R630_04_EXTERNAL_IP="76.53.10.39"

# Check r630-03
echo ""
log_info "Checking r630-03 ($R630_03_IP)..."
echo ""

# Ping test
if ping -c 2 -W 2 "$R630_03_IP" &>/dev/null; then
    log_success "✅ r630-03 is reachable via ping"
    
    # SSH test
    if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${R630_03_IP} "echo 'SSH OK'" &>/dev/null 2>&1; then
        log_success "✅ r630-03 SSH is accessible"
        
        # Get hostname
        HOSTNAME=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${R630_03_IP} "hostname" 2>/dev/null || echo "unknown")
        log_info "   Hostname: $HOSTNAME"
        
        # Check Proxmox
        if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${R630_03_IP} "command -v pveversion >/dev/null 2>&1" &>/dev/null; then
            PVE_VERSION=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${R630_03_IP} "pveversion 2>/dev/null | head -1" || echo "unknown")
            log_info "   Proxmox: $PVE_VERSION"
        fi
        
        # Check cluster status
        CLUSTER_STATUS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${R630_03_IP} "pvecm status 2>/dev/null | grep -E 'Node name|Quorum' | head -2" || echo "")
        if [[ -n "$CLUSTER_STATUS" ]]; then
            log_info "   Cluster Status:"
            echo "$CLUSTER_STATUS" | sed 's/^/      /'
        fi
    else
        log_warn "⚠️  r630-03 SSH is not accessible (password may be incorrect)"
    fi
    
    # Web UI test
    if curl -k -s -o /dev/null -w "%{http_code}" --connect-timeout 5 "https://${R630_03_IP}:8006/" 2>/dev/null | grep -q "200\|401\|302"; then
        log_success "✅ r630-03 Proxmox Web UI is accessible"
    else
        log_warn "⚠️  r630-03 Proxmox Web UI is not accessible"
    fi
else
    log_error "❌ r630-03 is NOT reachable"
    log_info "   Possible causes:"
    log_info "     - Server is powered off"
    log_info "     - Network cable is unplugged"
    log_info "     - Network switch port is disabled"
    log_info "     - IP address configuration issue"
    log_info ""
    log_info "   Action required:"
    log_info "     1. Check physical power status"
    log_info "     2. Verify network cable is plugged in"
    log_info "     3. Check network switch port status"
    log_info "     4. Verify IP configuration"
fi

# Check r630-04
echo ""
log_info "Checking r630-04 ($R630_04_IP)..."
echo ""

# Ping test
if ping -c 2 -W 2 "$R630_04_IP" &>/dev/null; then
    log_success "✅ r630-04 is reachable via ping"
    
    # SSH test
    if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${R630_04_IP} "echo 'SSH OK'" &>/dev/null 2>&1; then
        log_success "✅ r630-04 SSH is accessible"
        
        # Get hostname
        HOSTNAME=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${R630_04_IP} "hostname" 2>/dev/null || echo "unknown")
        log_info "   Hostname: $HOSTNAME"
        
        # Check Proxmox
        if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${R630_04_IP} "command -v pveversion >/dev/null 2>&1" &>/dev/null; then
            PVE_VERSION=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${R630_04_IP} "pveversion 2>/dev/null | head -1" || echo "unknown")
            log_info "   Proxmox: $PVE_VERSION"
        fi
        
        # Check cluster status
        CLUSTER_STATUS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${R630_04_IP} "pvecm status 2>/dev/null | grep -E 'Node name|Quorum' | head -2" || echo "")
        if [[ -n "$CLUSTER_STATUS" ]]; then
            log_info "   Cluster Status:"
            echo "$CLUSTER_STATUS" | sed 's/^/      /'
        fi
    else
        log_warn "⚠️  r630-04 SSH is not accessible"
        log_info "   Tried passwords: L@kers2010, password"
        log_info "   Action: Console access needed to reset password"
    fi
    
    # Web UI test
    if curl -k -s -o /dev/null -w "%{http_code}" --connect-timeout 5 "https://${R630_04_IP}:8006/" 2>/dev/null | grep -q "200\|401\|302"; then
        log_success "✅ r630-04 Proxmox Web UI is accessible"
    else
        log_warn "⚠️  r630-04 Proxmox Web UI is not accessible"
    fi
else
    log_error "❌ r630-04 is NOT reachable"
    log_info "   Possible causes:"
    log_info "     - Server is powered off"
    log_info "     - Network cable is unplugged"
    log_info "     - Network switch port is disabled"
    log_info "     - IP address configuration issue"
    log_info ""
    log_info "   Action required:"
    log_info "     1. Check physical power status"
    log_info "     2. Verify network cable is plugged in"
    log_info "     3. Check network switch port status"
    log_info "     4. Verify IP configuration"
fi

echo ""
log_info "═══════════════════════════════════════════════════════════"
log_info "  SUMMARY"
log_info "═══════════════════════════════════════════════════════════"
echo ""

# Summary
R630_03_STATUS="❌ Not Reachable"
R630_04_STATUS="❌ Not Reachable"

if ping -c 1 -W 1 "$R630_03_IP" &>/dev/null; then
    R630_03_STATUS="✅ Reachable"
fi

if ping -c 1 -W 1 "$R630_04_IP" &>/dev/null; then
    R630_04_STATUS="✅ Reachable"
fi

log_info "r630-03 ($R630_03_IP): $R630_03_STATUS"
log_info "r630-04 ($R630_04_IP): $R630_04_STATUS"
echo ""

if [[ "$R630_03_STATUS" == "❌ Not Reachable" ]] || [[ "$R630_04_STATUS" == "❌ Not Reachable" ]]; then
    log_warn "Physical connectivity check required:"
    log_info "  1. Verify power cables are connected"
    log_info "  2. Verify network cables are plugged in"
    log_info "  3. Check network switch port status (Omada Controller)"
    log_info "  4. Verify servers are powered on"
    echo ""
    log_info "After plugging in, run this script again to verify connectivity"
fi

echo ""
