#!/bin/bash
# VM Configuration Verification Script
# Verifies that all VMs are properly configured for ML110-01 and R630-01

set -e

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

echo "=========================================="
echo "VM Configuration Verification"
echo "=========================================="
echo ""

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

ERRORS=0
WARNINGS=0

# Function to check VM configuration
check_vm() {
    local file=$1
    local name=$(grep "name:" "$file" | head -1 | awk '{print $2}' | tr -d '"')
    local node=$(grep "node:" "$file" | head -1 | awk '{print $2}' | tr -d '"')
    local site=$(grep "site:" "$file" | head -1 | awk '{print $2}' | tr -d '"')
    local cpu=$(grep "cpu:" "$file" | head -1 | awk '{print $2}')
    local memory=$(grep "memory:" "$file" | head -1 | awk '{print $2}' | tr -d '"')
    local storage=$(grep "storage:" "$file" | head -1 | awk '{print $2}' | tr -d '"')
    
    # Verify node and site match
    if [[ "$node" == "ml110-01" && "$site" != "site-1" ]]; then
        echo -e "${RED}ERROR:${NC} $name - Node ml110-01 but site is $site (should be site-1)"
        ((ERRORS++))
    elif [[ "$node" == "r630-01" && "$site" != "site-2" ]]; then
        echo -e "${RED}ERROR:${NC} $name - Node r630-01 but site is $site (should be site-2)"
        ((ERRORS++))
    fi
    
    # Check storage configuration
    if [[ "$node" == "r630-01" && "$storage" == "local-lvm" ]]; then
        # Only Cloudflare Tunnel should use local-lvm on R630-01
        if [[ "$name" != "cloudflare-tunnel-vm" ]]; then
            echo -e "${YELLOW}WARNING:${NC} $name on r630-01 uses local-lvm (consider ceph-fs for large disks)"
            ((WARNINGS++))
        fi
    fi
    
    echo "  ✓ $name: node=$node, site=$site, cpu=$cpu, memory=$memory, storage=$storage"
}

# Count VMs per node
echo "=== VM Distribution ==="
ML110_COUNT=$(grep -r 'node: "ml110-01"' "$EXAMPLES_DIR" --include="*.yaml" | wc -l)
R630_COUNT=$(grep -r 'node: "r630-01"' "$EXAMPLES_DIR" --include="*.yaml" | wc -l)
TOTAL_COUNT=$(find "$EXAMPLES_DIR" -name "*.yaml" -type f | wc -l)

echo "ML110-01 VMs: $ML110_COUNT"
echo "R630-01 VMs: $R630_COUNT"
echo "Total VMs: $TOTAL_COUNT"
echo ""

# Calculate CPU allocations
echo "=== CPU Allocation Summary ==="
ML110_CPU=0
R630_CPU=0

for file in "$EXAMPLES_DIR"/*.yaml "$EXAMPLES_DIR"/phoenix/*.yaml "$EXAMPLES_DIR"/smom-dbis-138/*.yaml; do
    if [[ -f "$file" ]]; then
        node=$(grep "node:" "$file" | head -1 | awk '{print $2}' | tr -d '"')
        cpu=$(grep "cpu:" "$file" | head -1 | awk '{print $2}')
        if [[ -n "$cpu" && "$cpu" =~ ^[0-9]+$ ]]; then
            if [[ "$node" == "ml110-01" ]]; then
                ML110_CPU=$((ML110_CPU + cpu))
            elif [[ "$node" == "r630-01" ]]; then
                R630_CPU=$((R630_CPU + cpu))
            fi
        fi
    fi
done

echo "ML110-01 Total CPU: $ML110_CPU cores (5 available)"
if [[ $ML110_CPU -gt 5 ]]; then
    echo -e "${YELLOW}WARNING:${NC} ML110-01 CPU allocation ($ML110_CPU) exceeds available (5)"
    ((WARNINGS++))
else
    echo -e "${GREEN}OK:${NC} ML110-01 CPU allocation within capacity"
fi

echo "R630-01 Total CPU: $R630_CPU cores (50 available)"
if [[ $R630_CPU -gt 50 ]]; then
    echo -e "${YELLOW}WARNING:${NC} R630-01 CPU allocation ($R630_CPU) exceeds available (50)"
    ((WARNINGS++))
else
    echo -e "${GREEN}OK:${NC} R630-01 CPU allocation within capacity"
fi
echo ""

# Check storage configuration
echo "=== Storage Configuration ==="
CEPH_COUNT=$(grep -r 'storage: "ceph-fs"' "$EXAMPLES_DIR" --include="*.yaml" | wc -l)
LOCAL_COUNT=$(grep -r 'storage: "local-lvm"' "$EXAMPLES_DIR" --include="*.yaml" | wc -l)
echo "Ceph-fs VMs: $CEPH_COUNT"
echo "Local-lvm VMs: $LOCAL_COUNT"
echo ""

# Verify each VM
echo "=== Individual VM Verification ==="
for file in "$EXAMPLES_DIR"/*.yaml "$EXAMPLES_DIR"/phoenix/*.yaml "$EXAMPLES_DIR"/smom-dbis-138/*.yaml; do
    if [[ -f "$file" ]]; then
        check_vm "$file"
    fi
done

echo ""
echo "=========================================="
echo "Verification Complete"
echo "=========================================="
echo "Errors: $ERRORS"
echo "Warnings: $WARNINGS"

if [[ $ERRORS -eq 0 && $WARNINGS -eq 0 ]]; then
    echo -e "${GREEN}✓ All configurations are correct!${NC}"
    exit 0
elif [[ $ERRORS -eq 0 ]]; then
    echo -e "${YELLOW}⚠ Configurations are correct with minor warnings${NC}"
    exit 0
else
    echo -e "${RED}✗ Configuration errors found${NC}"
    exit 1
fi

