#!/bin/bash
# Dry-run test of deployment scripts (validates structure without executing)
# Run this locally before deploying to Proxmox host

set -euo pipefail

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

echo "========================================="
echo "Dry-Run Test - Script Validation"
echo "========================================="
echo ""

ERRORS=0

# Test 1: Script syntax
echo "1. Testing script syntax..."
for script in \
    "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/network/bootstrap-network.sh" \
    "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/validation/validate-validator-set.sh" \
    "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/deployment/deploy-validated-set.sh" \
    "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/deployment/bootstrap-quick.sh" \
    "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/health/check-node-health.sh"; do
    if bash -n "$script" 2>&1; then
        echo "  ✓ $(basename $script)"
    else
        echo "  ✗ $(basename $script) - Syntax error"
        ERRORS=$((ERRORS + 1))
    fi
done

# Test 2: Script executability
echo ""
echo "2. Checking script permissions..."
for script in \
    "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/network/bootstrap-network.sh" \
    "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/validation/validate-validator-set.sh" \
    "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/deployment/deploy-validated-set.sh" \
    "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/deployment/bootstrap-quick.sh" \
    "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/health/check-node-health.sh"; do
    if [[ -x "$script" ]]; then
        echo "  ✓ $(basename $script) is executable"
    else
        echo "  ✗ $(basename $script) is not executable"
        ERRORS=$((ERRORS + 1))
    fi
done

# Test 3: Required dependencies
echo ""
echo "3. Checking required dependencies..."
REQUIRED_FILES=(
    "$PROJECT_ROOT/smom-dbis-138-proxmox/lib/common.sh"
    "$PROJECT_ROOT/smom-dbis-138-proxmox/config/proxmox.conf.example"
)

for file in "${REQUIRED_FILES[@]}"; do
    if [[ -f "$file" ]]; then
        echo "  ✓ $(basename $file)"
    else
        echo "  ✗ $(basename $file) - Missing"
        ERRORS=$((ERRORS + 1))
    fi
done

# Test 4: Help/usage messages
echo ""
echo "4. Testing help/usage messages..."
if bash "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/deployment/deploy-validated-set.sh" --help >/dev/null 2>&1; then
    echo "  ✓ deploy-validated-set.sh --help works"
else
    echo "  ✗ deploy-validated-set.sh --help failed"
    ERRORS=$((ERRORS + 1))
fi

if bash "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/health/check-node-health.sh" 2>&1 | grep -q "Usage:"; then
    echo "  ✓ check-node-health.sh usage message works"
else
    echo "  ✗ check-node-health.sh usage message failed"
    ERRORS=$((ERRORS + 1))
fi

# Summary
echo ""
echo "========================================="
if [[ $ERRORS -eq 0 ]]; then
    echo "✓ All tests passed!"
    echo "Scripts are ready for deployment."
    exit 0
else
    echo "✗ Found $ERRORS error(s)"
    echo "Please fix errors before deployment."
    exit 1
fi
