#!/bin/bash
# Check Proxmox resources via SSH

set -euo pipefail

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

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

# Load environment
load_env() {
    if [ -f "${PROJECT_ROOT}/.env" ]; then
        source "${PROJECT_ROOT}/.env"
    fi
    
    PROXMOX_1_HOST="${PROXMOX_1_HOST:-192.168.11.10}"
    PROXMOX_2_HOST="${PROXMOX_2_HOST:-192.168.11.11}"
    PROXMOX_PASS="${PROXMOX_ROOT_PASS:-L@kers2010}"
}

log() {
    echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $*"
}

log_success() {
    echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')] ✅${NC} $*"
}

log_warning() {
    echo -e "${YELLOW}[$(date +'%Y-%m-%d %H:%M:%S')] ⚠️${NC} $*"
}

log_error() {
    echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] ❌${NC} $*"
}

log_info() {
    echo -e "${CYAN}[$(date +'%Y-%m-%d %H:%M:%S')] ℹ️${NC} $*"
}

check_node_via_ssh() {
    local host=$1
    local node_name=$2
    local site_name=$3
    local password=$4
    
    log "Checking ${site_name} (${host})..."
    
    if ! command -v sshpass &> /dev/null; then
        log_error "sshpass is required. Install with: apt-get install sshpass"
        return 1
    fi
    
    # Get CPU info
    local cpu_total cpu_avail
    cpu_total=$(sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@${host} "nproc" 2>/dev/null || echo "0")
    
    # Get memory info
    local mem_total mem_available mem_used
    mem_total=$(sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@${host} "free -g | awk '/^Mem:/ {print \$2}'" 2>/dev/null || echo "0")
    mem_available=$(sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@${host} "free -g | awk '/^Mem:/ {print \$7}'" 2>/dev/null || echo "0")
    mem_used=$(sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@${host} "free -g | awk '/^Mem:/ {print \$3}'" 2>/dev/null || echo "0")
    
    # Get VM resource usage
    local vm_cpu_total vm_mem_total
    vm_cpu_total=$(sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@${host} "qm list 2>/dev/null | tail -n +2 | awk '{sum+=\$3} END {print sum+0}'" 2>/dev/null || echo "0")
    vm_mem_total=$(sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@${host} "qm list 2>/dev/null | tail -n +2 | awk '{sum+=\$4} END {print sum+0}'" 2>/dev/null || echo "0")
    
    # Calculate available
    local cpu_avail mem_avail_gib
    cpu_avail=$((cpu_total - vm_cpu_total))
    mem_avail_gib=${mem_available}
    
    log_info "=== Node: ${node_name} ==="
    echo "  CPU: ${cpu_avail} / ${cpu_total} cores available (${vm_cpu_total} used by VMs)"
    echo "  Memory: ${mem_avail_gib} GiB / ${mem_total} GiB available (${vm_mem_total} GiB used by VMs, ${mem_used} GiB system used)"
    
    # Check storage
    log_info "=== Storage Pools ==="
    sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@${host} "pvesm status 2>/dev/null" | tail -n +2 | while read -r line; do
        if [ -n "$line" ]; then
            echo "  - $line"
        fi
    done
    
    echo "${cpu_avail}|${cpu_total}|${mem_avail_gib}|${mem_total}"
}

# Required resources
REQUIRED_CPU=72
REQUIRED_RAM=140
REQUIRED_DISK=278

main() {
    log "=========================================="
    log "Proxmox Resource Quota Check (SSH Method)"
    log "=========================================="
    log ""
    log_info "Required Resources:"
    log "  CPU: ${REQUIRED_CPU} cores"
    log "  RAM: ${REQUIRED_RAM} GiB"
    log "  Disk: ${REQUIRED_DISK} GiB"
    log ""
    
    load_env
    
    # Check Site 1
    site1_result=$(check_node_via_ssh "${PROXMOX_1_HOST}" "ml110-01" "Site 1" "${PROXMOX_PASS}" 2>&1)
    site1_status=$?
    
    if [ $site1_status -eq 0 ]; then
        IFS='|' read -r cpu1_avail cpu1_total mem1_avail mem1_total <<< "$(echo "$site1_result" | tail -1)"
    else
        log_warning "Could not check Site 1"
        cpu1_avail=0
        cpu1_total=0
        mem1_avail=0
        mem1_total=0
    fi
    
    echo ""
    
    # Check Site 2
    site2_result=$(check_node_via_ssh "${PROXMOX_2_HOST}" "r630-01" "Site 2" "${PROXMOX_PASS}" 2>&1)
    site2_status=$?
    
    if [ $site2_status -eq 0 ]; then
        IFS='|' read -r cpu2_avail cpu2_total mem2_avail mem2_total <<< "$(echo "$site2_result" | tail -1)"
    else
        log_warning "Could not check Site 2"
        cpu2_avail=0
        cpu2_total=0
        mem2_avail=0
        mem2_total=0
    fi
    
    echo ""
    log "=========================================="
    log_info "=== Summary ==="
    
    total_cpu_avail=$((cpu1_avail + cpu2_avail))
    total_cpu=$((cpu1_total + cpu2_total))
    total_mem_avail=$((mem1_avail + mem2_avail))
    total_mem=$((mem1_total + mem2_total))
    
    echo "  Total CPU Available: ${total_cpu_avail} / ${total_cpu} cores"
    echo "  Total Memory Available: ${total_mem_avail} / ${total_mem} GiB"
    echo ""
    
    # Compare with requirements
    log_info "=== Resource Comparison ==="
    if [ ${total_cpu_avail} -ge ${REQUIRED_CPU} ]; then
        log_success "CPU: ${total_cpu_avail} >= ${REQUIRED_CPU} ✅"
    else
        log_error "CPU: ${total_cpu_avail} < ${REQUIRED_CPU} ❌ (Shortfall: $((REQUIRED_CPU - total_cpu_avail)) cores)"
    fi
    
    if [ ${total_mem_avail} -ge ${REQUIRED_RAM} ]; then
        log_success "RAM: ${total_mem_avail} GiB >= ${REQUIRED_RAM} GiB ✅"
    else
        log_error "RAM: ${total_mem_avail} GiB < ${REQUIRED_RAM} GiB ❌ (Shortfall: $((REQUIRED_RAM - total_mem_avail)) GiB)"
    fi
    
    echo ""
    log "=========================================="
    log_success "Quota check completed!"
}

main "$@"
