#!/usr/bin/env bash
# Copy Besu configuration files to temporary VM
# This script should be run from the Proxmox host

set -euo pipefail

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

TEMP_VM_IP="${BESU_TEMP_IP:-192.168.11.90}"
SOURCE_PROJECT="${1:-/opt/smom-dbis-138}"

log_info() { echo "[INFO] $1"; }
log_success() { echo "[✓] $1"; }
log_error() { echo "[ERROR] $1"; exit 1; }

log_info "Copying configuration files to temporary VM ($TEMP_VM_IP)..."

# Check VM accessibility
if ! ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 "root@$TEMP_VM_IP" "echo 'VM accessible'" 2>/dev/null; then
    log_error "Cannot access temporary VM at $TEMP_VM_IP"
    exit 1
fi

# Copy config templates
log_info "Copying config templates..."
scp -r "$PROJECT_ROOT/templates/besu-configs" "root@$TEMP_VM_IP:/opt/besu/" || {
    log_error "Failed to copy config templates"
    exit 1
}

# Copy docker-compose file
log_info "Copying docker-compose file..."
scp "$PROJECT_ROOT/templates/docker-compose-besu-temp.yml" "root@$TEMP_VM_IP:/opt/besu/docker-compose.yml" || {
    log_error "Failed to copy docker-compose file"
    exit 1
}

# Copy config files from source project
if [[ -d "$SOURCE_PROJECT/config" ]]; then
    log_info "Copying configuration files from source project..."
    
    # Create shared directories on VM
    ssh "root@$TEMP_VM_IP" "mkdir -p /opt/besu/shared/{genesis,permissions}"
    
    # Copy genesis.json
    if [[ -f "$SOURCE_PROJECT/config/genesis.json" ]]; then
        scp "$SOURCE_PROJECT/config/genesis.json" "root@$TEMP_VM_IP:/opt/besu/shared/genesis/" || log_error "Failed to copy genesis.json"
        log_success "Copied genesis.json"
    fi
    
    # Copy static-nodes.json
    if [[ -f "$SOURCE_PROJECT/config/static-nodes.json" ]]; then
        scp "$SOURCE_PROJECT/config/static-nodes.json" "root@$TEMP_VM_IP:/opt/besu/shared/genesis/" || log_error "Failed to copy static-nodes.json"
        log_success "Copied static-nodes.json"
    fi
    
    # Copy permissions files
    if [[ -f "$SOURCE_PROJECT/config/permissions-nodes.toml" ]]; then
        scp "$SOURCE_PROJECT/config/permissions-nodes.toml" "root@$TEMP_VM_IP:/opt/besu/shared/permissions/" || log_error "Failed to copy permissions-nodes.toml"
        log_success "Copied permissions-nodes.toml"
    fi
    
    if [[ -f "$SOURCE_PROJECT/config/permissions-accounts.toml" ]]; then
        scp "$SOURCE_PROJECT/config/permissions-accounts.toml" "root@$TEMP_VM_IP:/opt/besu/shared/permissions/" || log_error "Failed to copy permissions-accounts.toml"
        log_success "Copied permissions-accounts.toml"
    fi
else
    log_error "Source project not found at $SOURCE_PROJECT"
    exit 1
fi

# Copy keys if they exist
if [[ -d "$SOURCE_PROJECT/keys" ]]; then
    log_info "Copying keys..."
    
    # Copy validator keys
    if [[ -d "$SOURCE_PROJECT/keys/validators" ]]; then
        for i in {1..5}; do
            if [[ -d "$SOURCE_PROJECT/keys/validators/validator-$i" ]]; then
                ssh "root@$TEMP_VM_IP" "mkdir -p /opt/besu/validators/validator-$i/keys"
                scp -r "$SOURCE_PROJECT/keys/validators/validator-$i"/* "root@$TEMP_VM_IP:/opt/besu/validators/validator-$i/keys/" 2>/dev/null || true
            fi
        done
        log_success "Copied validator keys"
    fi
    
    # Copy sentry keys
    if [[ -d "$SOURCE_PROJECT/keys/sentries" ]]; then
        for i in {1..4}; do
            if [[ -d "$SOURCE_PROJECT/keys/sentries/sentry-$i" ]]; then
                ssh "root@$TEMP_VM_IP" "mkdir -p /opt/besu/sentries/sentry-$i/keys"
                scp -r "$SOURCE_PROJECT/keys/sentries/sentry-$i"/* "root@$TEMP_VM_IP:/opt/besu/sentries/sentry-$i/keys/" 2>/dev/null || true
            fi
        done
        log_success "Copied sentry keys"
    fi
    
    # Copy RPC keys
    if [[ -d "$SOURCE_PROJECT/keys/rpc" ]]; then
        for i in {1..3}; do
            if [[ -d "$SOURCE_PROJECT/keys/rpc/rpc-$i" ]]; then
                ssh "root@$TEMP_VM_IP" "mkdir -p /opt/besu/rpc/rpc-$i/keys"
                scp -r "$SOURCE_PROJECT/keys/rpc/rpc-$i"/* "root@$TEMP_VM_IP:/opt/besu/rpc/rpc-$i/keys/" 2>/dev/null || true
            fi
        done
        log_success "Copied RPC keys"
    fi
fi

# Copy config files to each node directory
log_info "Setting up node configuration files..."

# Validators
for i in {1..5}; do
    ssh "root@$TEMP_VM_IP" "mkdir -p /opt/besu/validators/validator-$i/config && \
        cp /opt/besu/besu-configs/config-validator.toml /opt/besu/validators/validator-$i/config/config-validator.toml" || true
done

# Sentries
for i in {1..4}; do
    ssh "root@$TEMP_VM_IP" "mkdir -p /opt/besu/sentries/sentry-$i/config && \
        cp /opt/besu/besu-configs/config-sentry.toml /opt/besu/sentries/sentry-$i/config/config-sentry.toml" || true
done

# RPC nodes
for i in {1..3}; do
    ssh "root@$TEMP_VM_IP" "mkdir -p /opt/besu/rpc/rpc-$i/config && \
        cp /opt/besu/besu-configs/config-rpc.toml /opt/besu/rpc/rpc-$i/config/config-rpc.toml" || true
done

log_success "Configuration files copied and set up!"
log_info ""
log_info "Next steps:"
log_info "1. SSH into VM: ssh root@$TEMP_VM_IP"
log_info "2. Review configs: cd /opt/besu && ls -la"
log_info "3. Start containers: docker compose up -d"

