#!/usr/bin/env bash
# Deploy Besu temporary VM on ml110
# This script runs the temporary VM deployment on the remote Proxmox host

set -euo pipefail

REMOTE_HOST="192.168.11.10"
REMOTE_USER="root"
REMOTE_PASS="L@kers2010"
SOURCE_PROJECT="/opt/smom-dbis-138"

echo "=== Besu Temporary VM Deployment on ml110 ==="
echo ""
echo "Target: ${REMOTE_USER}@${REMOTE_HOST}"
echo "Source Project: $SOURCE_PROJECT"
echo ""

# Test connection
if ! sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 \
    "${REMOTE_USER}@${REMOTE_HOST}" "echo 'Connected'" 2>/dev/null; then
    echo "ERROR: Cannot connect to ${REMOTE_HOST}"
    exit 1
fi

echo "✓ Connection successful"
echo ""

# Check if deployment script exists
if ! sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no \
    "${REMOTE_USER}@${REMOTE_HOST}" \
    "test -f /opt/smom-dbis-138-proxmox/scripts/deployment/deploy-besu-temp-vm-complete.sh" 2>/dev/null; then
    echo "ERROR: deploy-besu-temp-vm-complete.sh not found on ${REMOTE_HOST}"
    exit 1
fi

echo "✓ Deployment script found"
echo ""

# Check if source project exists
if ! sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no \
    "${REMOTE_USER}@${REMOTE_HOST}" \
    "test -d $SOURCE_PROJECT" 2>/dev/null; then
    echo "ERROR: Source project $SOURCE_PROJECT not found on ${REMOTE_HOST}"
    exit 1
fi

echo "✓ Source project found"
echo ""

echo "Starting temporary VM deployment..."
echo "This will:"
echo "  1. Create a VM (VMID 9000) with 32GB RAM, 8 cores, 500GB disk"
echo "  2. Install Docker and set up directory structure"
echo "  3. Copy configuration files and keys"
echo "  4. Start all 12 Besu containers"
echo ""
echo "This may take 15-30 minutes"
echo ""

# Auto-confirm if AUTO_CONFIRM is set
if [[ "${AUTO_CONFIRM:-}" != "true" ]]; then
    read -p "Continue? [y/N]: " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo "Deployment cancelled"
        exit 0
    fi
else
    echo "Auto-confirming deployment (AUTO_CONFIRM=true)"
fi

# Run deployment with timeout (1 hour max)
sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no \
    "${REMOTE_USER}@${REMOTE_HOST}" \
    "cd /opt/smom-dbis-138-proxmox && \
     chmod +x ./scripts/deployment/deploy-besu-temp-vm-complete.sh && \
     timeout 3600 ./scripts/deployment/deploy-besu-temp-vm-complete.sh $SOURCE_PROJECT" 2>&1

EXIT_CODE=$?

if [[ $EXIT_CODE -eq 0 ]]; then
    echo ""
    echo "✅ Temporary VM deployment completed successfully!"
    echo ""
    echo "VM Details:"
    echo "  IP: 192.168.11.90"
    echo "  VMID: 9000"
    echo ""
    echo "RPC Endpoints:"
    echo "  http://192.168.11.90:8545"
    echo "  http://192.168.11.90:8547"
    echo "  http://192.168.11.90:8549"
    echo ""
    echo "Next steps:"
    echo "  1. Validate: ssh root@192.168.11.10 'cd /opt/smom-dbis-138-proxmox && ./scripts/validation/validate-besu-temp-vm.sh'"
    echo "  2. Monitor: ssh root@192.168.11.90 'docker compose logs -f'"
    echo "  3. When ready, migrate to LXC containers"
elif [[ $EXIT_CODE -eq 124 ]]; then
    echo ""
    echo "⚠ Deployment timed out (1 hour)"
    echo "Check the deployment status manually"
else
    echo ""
    echo "❌ Deployment failed with exit code: $EXIT_CODE"
    echo "Check the output above for errors"
fi

exit $EXIT_CODE

