#!/bin/bash
# Test storage performance on r630-01 and r630-02
# Creates test containers, verifies storage works, then cleans up

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/load-physical-inventory.sh" 2>/dev/null || true

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
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"; }
log_section() { echo -e "\n${CYAN}=== $1 ===${NC}\n"; }

R630_01_IP="${PROXMOX_HOST_R630_01:-192.168.11.11}"
R630_01_PASS="${PROXMOX_PASS_R630_01:-password}"
R630_02_IP="${PROXMOX_HOST_R630_02:-192.168.11.12}"
R630_02_PASS="${PROXMOX_PASS_R630_02:-password}"

TEST_VMID=9999

cleanup() {
    log_info "Cleaning up test containers..."
    
    # Cleanup r630-01
    sshpass -p "$R630_01_PASS" ssh -o StrictHostKeyChecking=no root@"$R630_01_IP" \
        "pct destroy $TEST_VMID 2>/dev/null || true" 2>/dev/null || true
    
    # Cleanup r630-02
    sshpass -p "$R630_02_PASS" ssh -o StrictHostKeyChecking=no root@"$R630_02_IP" \
        "pct destroy $TEST_VMID 2>/dev/null || true" 2>/dev/null || true
    
    log_success "Cleanup complete"
}

trap cleanup EXIT

log_section "Storage Performance Test"

# Test r630-01 local-lvm
log_section "Testing r630-01 local-lvm Storage"

log_info "Checking if template exists..."
TEMPLATE=$(sshpass -p "$R630_01_PASS" ssh -o StrictHostKeyChecking=no root@"$R630_01_IP" \
    "pvesm list local 2>/dev/null | grep -i ubuntu | head -1 | awk '{print \$1}'" 2>/dev/null || echo "")

if [ -z "$TEMPLATE" ]; then
    log_warn "No Ubuntu template found, checking available templates..."
    TEMPLATE=$(sshpass -p "$R630_01_PASS" ssh -o StrictHostKeyChecking=no root@"$R630_01_IP" \
        "pvesm list local 2>/dev/null | head -5 | tail -1 | awk '{print \$1}'" 2>/dev/null || echo "")
fi

if [ -n "$TEMPLATE" ]; then
    log_info "Using template: $TEMPLATE"
    log_info "Creating test container on r630-01 (local-lvm)..."
    
    sshpass -p "$R630_01_PASS" ssh -o StrictHostKeyChecking=no root@"$R630_01_IP" bash <<ENDSSH
        pct create $TEST_VMID $TEMPLATE \
            --storage local-lvm \
            --hostname test-storage-r630-01 \
            --net0 name=eth0,bridge=vmbr0,ip=192.168.11.99/24 \
            --memory 512 \
            --cores 1 \
            --unprivileged 1 2>&1 | head -10
ENDSSH
    
    if sshpass -p "$R630_01_PASS" ssh -o StrictHostKeyChecking=no root@"$R630_01_IP" \
        "pct list | grep -q $TEST_VMID" 2>/dev/null; then
        log_success "Test container created on r630-01"
        sshpass -p "$R630_01_PASS" ssh -o StrictHostKeyChecking=no root@"$R630_01_IP" \
            "pct list | grep $TEST_VMID" 2>/dev/null
    else
        log_warn "Container creation may have failed (check manually)"
    fi
else
    log_warn "No template available, skipping container creation test"
    log_info "Storage status check only..."
fi

# Check storage status
log_info "Storage status on r630-01:"
sshpass -p "$R630_01_PASS" ssh -o StrictHostKeyChecking=no root@"$R630_01_IP" \
    "pvesm status local-lvm 2>/dev/null" 2>/dev/null || log_warn "Could not check storage"

# Test r630-02 thin1
log_section "Testing r630-02 thin1 Storage"

log_info "Checking if template exists..."
TEMPLATE=$(sshpass -p "$R630_02_PASS" ssh -o StrictHostKeyChecking=no root@"$R630_02_IP" \
    "pvesm list local 2>/dev/null | grep -i ubuntu | head -1 | awk '{print \$1}'" 2>/dev/null || echo "")

if [ -z "$TEMPLATE" ]; then
    log_warn "No Ubuntu template found, checking available templates..."
    TEMPLATE=$(sshpass -p "$R630_02_PASS" ssh -o StrictHostKeyChecking=no root@"$R630_02_IP" \
        "pvesm list local 2>/dev/null | head -5 | tail -1 | awk '{print \$1}'" 2>/dev/null || echo "")
fi

if [ -n "$TEMPLATE" ]; then
    log_info "Using template: $TEMPLATE"
    log_info "Creating test container on r630-02 (thin1)..."
    
    sshpass -p "$R630_02_PASS" ssh -o StrictHostKeyChecking=no root@"$R630_02_IP" bash <<ENDSSH
        pct create $TEST_VMID $TEMPLATE \
            --storage thin1 \
            --hostname test-storage-r630-02 \
            --net0 name=eth0,bridge=vmbr0,ip=192.168.11.98/24 \
            --memory 512 \
            --cores 1 \
            --unprivileged 1 2>&1 | head -10
ENDSSH
    
    if sshpass -p "$R630_02_PASS" ssh -o StrictHostKeyChecking=no root@"$R630_02_IP" \
        "pct list | grep -q $TEST_VMID" 2>/dev/null; then
        log_success "Test container created on r630-02"
        sshpass -p "$R630_02_PASS" ssh -o StrictHostKeyChecking=no root@"$R630_02_IP" \
            "pct list | grep $TEST_VMID" 2>/dev/null
    else
        log_warn "Container creation may have failed (check manually)"
    fi
else
    log_warn "No template available, skipping container creation test"
    log_info "Storage status check only..."
fi

# Check storage status
log_info "Storage status on r630-02:"
sshpass -p "$R630_02_PASS" ssh -o StrictHostKeyChecking=no root@"$R630_02_IP" \
    "pvesm status thin1 2>/dev/null" 2>/dev/null || log_warn "Could not check storage"

log_section "Storage Test Complete"
log_success "Storage performance test completed"
log_info "Test containers will be cleaned up automatically"
