#!/bin/bash
# Verify r630-03 cluster membership and storage configuration
# Usage: ./scripts/verify-r630-03-cluster-storage.sh

set -euo pipefail

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

# 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}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }

# Host configuration
R630_03_IP="192.168.11.13"
R630_03_PASSWORD="L@kers2010"
R630_03_HOSTNAME="r630-03"

log_info "========================================="
log_info "Verifying r630-03 Cluster & Storage"
log_info "========================================="
echo ""

# Test connectivity
log_info "1. Testing connectivity to ${R630_03_IP}..."
if ping -c 2 -W 2 "$R630_03_IP" >/dev/null 2>&1; then
    log_success "Host is reachable"
else
    log_error "Host is NOT reachable"
    exit 1
fi
echo ""

# Test SSH
log_info "2. Testing SSH access..."
if sshpass -p "$R630_03_PASSWORD" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@"$R630_03_IP" "echo 'SSH OK'" >/dev/null 2>&1; then
    log_success "SSH access works"
else
    log_error "SSH access failed - password may be incorrect"
    exit 1
fi
echo ""

# Run comprehensive diagnostics
log_info "3. Running comprehensive diagnostics..."
echo ""

sshpass -p "$R630_03_PASSWORD" ssh -o StrictHostKeyChecking=no root@"$R630_03_IP" bash <<'ENDSSH'
    set -e
    
    echo "=== System Information ==="
    echo "Hostname: $(hostname)"
    echo "IP Address: $(hostname -I | awk '{print $1}')"
    echo "Uptime: $(uptime)"
    echo "Proxmox Version: $(pveversion 2>/dev/null || echo 'Not installed')"
    echo ""
    
    echo "=== Cluster Membership Status ==="
    if command -v pvecm >/dev/null 2>&1; then
        echo "Cluster Status:"
        pvecm status 2>&1 || echo "Not in cluster or cluster check failed"
        echo ""
        echo "Cluster Nodes:"
        pvecm nodes 2>&1 || echo "Cannot list cluster nodes"
        echo ""
        echo "Expected Cluster: h"
        echo "Expected Nodes: ml110, r630-01, r630-02, r630-03"
    else
        echo "pvecm command not found - Proxmox may not be installed"
    fi
    echo ""
    
    echo "=== Proxmox Services Status ==="
    for service in pve-cluster pvestatd pvedaemon pveproxy; do
        if systemctl list-unit-files | grep -q "^${service}"; then
            echo "--- ${service} ---"
            systemctl status ${service} --no-pager -l | head -10 || echo "Service status check failed"
        else
            echo "--- ${service} ---"
            echo "Service not found"
        fi
        echo ""
    done
    
    echo "=== Storage Configuration ==="
    echo "Available Storage:"
    if command -v pvesm >/dev/null 2>&1; then
        pvesm status 2>&1 || echo "Cannot list storage"
    else
        echo "pvesm command not found"
    fi
    echo ""
    
    echo "Storage Details:"
    if command -v pvesh >/dev/null 2>&1; then
        pvesh get /nodes/$(hostname)/storage 2>/dev/null | head -50 || echo "Cannot get storage details"
    else
        echo "pvesh command not found"
    fi
    echo ""
    
    echo "=== LVM Volume Groups ==="
    vgs 2>/dev/null || echo "No volume groups found or LVM not configured"
    echo ""
    
    echo "=== LVM Thin Pools ==="
    lvs -o lv_name,vg_name,lv_size,data_percent,metadata_percent,pool_lv 2>/dev/null | grep -E "LV|thin" || echo "No thin pools found"
    echo ""
    
    echo "=== Disk Space ==="
    df -h | grep -E "Filesystem|/dev|/var/lib/vz"
    echo ""
    
    echo "=== Block Devices ==="
    lsblk -o NAME,SIZE,TYPE,MOUNTPOINT,FSTYPE
    echo ""
    
    echo "=== Network Interfaces ==="
    ip addr show | grep -E "^[0-9]+:|inet " | head -20
    echo ""
    
    echo "=== Port 8006 Status ==="
    ss -tlnp | grep 8006 || echo "Port 8006 is NOT listening"
    echo ""
    
    echo "=== Web Interface Test ==="
    curl -k -s -o /dev/null -w "HTTP Status: %{http_code}\n" https://localhost:8006/ 2>&1 || echo "Cannot connect to web interface"
    echo ""
    
    echo "=== /etc/hosts Configuration ==="
    cat /etc/hosts | grep -E "127.0.0.1|$(hostname)" || echo "No relevant entries found"
    echo ""
    
    echo "=== Container/VMs Count ==="
    if command -v pct >/dev/null 2>&1; then
        echo "Containers: $(pct list 2>/dev/null | tail -n +2 | wc -l)"
    fi
    if command -v qm >/dev/null 2>&1; then
        echo "VMs: $(qm list 2>/dev/null | tail -n +2 | wc -l)"
    fi
    echo ""
ENDSSH

echo ""
log_info "4. Testing Proxmox Web Interface from remote..."
if curl -k -s -o /dev/null -w "%{http_code}" --connect-timeout 5 "https://${R630_03_IP}:8006/" | grep -q "200\|401\|302"; then
    log_success "Web interface is accessible on port 8006"
else
    log_warn "Web interface may not be accessible on port 8006"
fi
echo ""

log_success "Verification complete for r630-03"
echo ""
log_info "Summary:"
log_info "  - Check cluster membership status above"
log_info "  - Check storage configuration status above"
log_info "  - Review any warnings or errors"
echo ""
