#!/usr/bin/env bash
# Run Besu fixes on Proxmox host via SSH
# Uses sshpass for password authentication

set -euo pipefail

HOST="${1:-192.168.11.10}"
USER="${2:-root}"
PASS="${3:-}"
REMOTE_DIR="${4:-/opt/smom-dbis-138-proxmox}"

if [[ -z "$PASS" ]]; then
    echo "Usage: $0 [HOST] [USER] [PASSWORD] [REMOTE_DIR]"
    echo "Example: $0 192.168.11.10 root 'password' /opt/smom-dbis-138-proxmox"
    exit 1
fi

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
BESU_PROJECT="$PROJECT_ROOT/smom-dbis-138-proxmox"

echo "=== Running Besu Fixes on Proxmox Host ==="
echo "Host: $HOST"
echo "User: $USER"
echo "Remote Directory: $REMOTE_DIR"
echo ""

# Check if sshpass is installed
if ! command -v sshpass >/dev/null 2>&1; then
    echo "❌ sshpass not found. Please install it:"
    echo "   Ubuntu/Debian: sudo apt-get install sshpass"
    echo "   macOS: brew install sshpass"
    exit 1
fi

# Export password for sshpass
export SSHPASS="$PASS"

# Test connection
echo "Testing SSH connection..."
if ! sshpass -e ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 "$USER@$HOST" "echo 'Connection successful'" 2>/dev/null; then
    echo "❌ Cannot connect to $HOST"
    exit 1
fi
echo "✅ SSH connection successful"
echo ""

# Check if remote directory exists and has scripts
echo "Checking remote directory..."
if sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "test -d $REMOTE_DIR/scripts" 2>/dev/null; then
    echo "✅ Remote directory exists: $REMOTE_DIR"
    HAS_SCRIPTS=$(sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "test -f $REMOTE_DIR/scripts/fix-all-besu.sh && echo yes || echo no" 2>/dev/null)
    
    if [[ "$HAS_SCRIPTS" == "yes" ]]; then
        echo "✅ Fix scripts already exist on remote host"
        USE_REMOTE=true
    else
        echo "⚠️  Fix scripts not found, copying them..."
        USE_REMOTE=false
    fi
else
    echo "⚠️  Remote directory not found, creating it..."
    sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "mkdir -p $REMOTE_DIR" 2>/dev/null
    USE_REMOTE=false
fi

# Copy scripts if needed
if [[ "$USE_REMOTE" == "false" ]]; then
    echo "Copying fix scripts to remote host..."
    
    # Create scripts directory
    sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "mkdir -p $REMOTE_DIR/scripts" 2>/dev/null
    
    # Copy fix scripts
    for script in fix-container-ips.sh fix-besu-services.sh validate-besu-config.sh fix-all-besu.sh; do
        if [[ -f "$BESU_PROJECT/scripts/$script" ]]; then
            echo "  Copying $script..."
            sshpass -e scp -o StrictHostKeyChecking=no "$BESU_PROJECT/scripts/$script" "$USER@$HOST:$REMOTE_DIR/scripts/" 2>/dev/null
        fi
    done
    
    # Copy lib directory if it exists
    if [[ -d "$BESU_PROJECT/lib" ]]; then
        echo "  Copying lib directory..."
        sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "mkdir -p $REMOTE_DIR/lib" 2>/dev/null
        sshpass -e scp -o StrictHostKeyChecking=no -r "$BESU_PROJECT/lib"/* "$USER@$HOST:$REMOTE_DIR/lib/" 2>/dev/null
    fi
    
    # Copy config directory
    if [[ -d "$BESU_PROJECT/config" ]]; then
        echo "  Copying config directory..."
        sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "mkdir -p $REMOTE_DIR/config" 2>/dev/null
        sshpass -e scp -o StrictHostKeyChecking=no -r "$BESU_PROJECT/config"/* "$USER@$HOST:$REMOTE_DIR/config/" 2>/dev/null
    fi
    
    # Make scripts executable
    sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "chmod +x $REMOTE_DIR/scripts/*.sh" 2>/dev/null
    
    echo "✅ Scripts copied"
fi

echo ""
echo "=== Running fix-all-besu.sh on Proxmox host ==="
echo ""

# Run the fix script
sshpass -e ssh -o StrictHostKeyChecking=no "$USER@$HOST" "cd $REMOTE_DIR && bash scripts/fix-all-besu.sh"

echo ""
echo "=== Fix execution completed ==="

