#!/usr/bin/env bash
# Verify files synced to ml110 match local verified files

set -euo pipefail

REMOTE_HOST="192.168.11.10"
REMOTE_USER="root"
REMOTE_BASE="/opt"
LOCAL_PROJECT_ROOT="/home/intlc/projects/proxmox"
SOURCE_PROJECT="$LOCAL_PROJECT_ROOT/smom-dbis-138-proxmox"

log_info() { echo "[INFO] $1"; }
log_success() { echo "[✓] $1"; }
log_warn() { echo "[⚠] $1"; }
log_error() { echo "[✗] $1"; }

log_info "=== Verifying ml110 Sync ==="
log_info ""

# Check critical files
CRITICAL_FILES=(
    "smom-dbis-138-proxmox/config/proxmox.conf"
    "smom-dbis-138-proxmox/config/network.conf"
    "smom-dbis-138-proxmox/config/inventory.example"
    "smom-dbis-138-proxmox/scripts/fix-container-ips.sh"
    "smom-dbis-138-proxmox/scripts/fix-besu-services.sh"
    "smom-dbis-138-proxmox/scripts/validate-besu-config.sh"
    "smom-dbis-138-proxmox/scripts/fix-all-besu.sh"
    "smom-dbis-138-proxmox/deploy-all.sh"
    "smom-dbis-138/config/genesis.json"
    "smom-dbis-138/config/permissions-nodes.toml"
    "smom-dbis-138/keys/validators/validator-1/key.priv"
)

log_info "Checking critical files..."
missing=0
for file in "${CRITICAL_FILES[@]}"; do
    if sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no "${REMOTE_USER}@${REMOTE_HOST}" \
        "test -f ${REMOTE_BASE}/${file}" 2>/dev/null; then
        log_success "${file}"
    else
        log_error "${file} - MISSING"
        missing=$((missing + 1))
    fi
done

echo ""
log_info "Checking validator keys..."
key_count=$(sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no "${REMOTE_USER}@${REMOTE_HOST}" \
    "find ${REMOTE_BASE}/smom-dbis-138/keys/validators -mindepth 1 -maxdepth 1 -type d 2>/dev/null | wc -l" | tr -d ' ')

if [[ $key_count -eq 4 ]]; then
    log_warn "Found $key_count validator keys (expected 5 - validator-5 missing)"
elif [[ $key_count -eq 5 ]]; then
    log_success "Found $key_count validator keys"
else
    log_error "Unexpected validator key count: $key_count"
fi

echo ""
if [[ $missing -eq 0 ]]; then
    log_success "All critical files present!"
    exit 0
else
    log_error "$missing critical files missing"
    exit 1
fi

