#!/usr/bin/env bash
# Enable EIP-7702 support in Besu configuration
# EIP-7702 requires Cancun fork or later
# Usage: ./scripts/enable-eip-7702-besu.sh

set -euo pipefail

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

# Configuration
VMID=2400
PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"
BESU_CONFIG="/etc/besu/config-rpc-thirdweb.toml"
GENESIS_FILE="/genesis/genesis.json"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

log_info "═══════════════════════════════════════════════════════════"
log_info "  ENABLING EIP-7702 SUPPORT IN BESU"
log_info "═══════════════════════════════════════════════════════════"
echo ""

# Check SSH access
if ! ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${PROXMOX_HOST} "echo 'SSH OK'" &>/dev/null; then
    log_error "Cannot access $PROXMOX_HOST via SSH"
    exit 1
fi

# Check if genesis file exists
log_info "Checking genesis file..."
GENESIS_EXISTS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${PROXMOX_HOST} \
    "pct exec $VMID -- test -f $GENESIS_FILE && echo 'exists' || echo 'missing'")

if [[ "$GENESIS_EXISTS" != "exists" ]]; then
    log_error "Genesis file not found: $GENESIS_FILE"
    log_info "EIP-7702 requires Cancun fork to be enabled in the genesis file"
    exit 1
fi

# Check current genesis configuration
log_info "Checking current genesis configuration..."
GENESIS_CONFIG=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${PROXMOX_HOST} \
    "pct exec $VMID -- cat $GENESIS_FILE 2>/dev/null" || echo "")

if [[ -z "$GENESIS_CONFIG" ]]; then
    log_error "Could not read genesis file"
    exit 1
fi

# Check if Cancun fork is enabled
if echo "$GENESIS_CONFIG" | grep -qi "cancun"; then
    log_success "Cancun fork is already enabled in genesis file"
    log_info "EIP-7702 should be supported if Besu version >= 24.1.0"
elif echo "$GENESIS_CONFIG" | grep -qi "shanghai\|london\|berlin"; then
    log_warn "Genesis file has older fork configuration"
    log_info "EIP-7702 requires Cancun fork (or later)"
    log_info "To enable EIP-7702, update genesis file to include Cancun fork"
else
    log_warn "Could not determine fork configuration from genesis file"
    log_info "EIP-7702 requires Cancun fork to be enabled"
fi

# Note: EIP-7702 is typically enabled automatically if Cancun fork is active
# and Besu version supports it. No additional configuration needed in Besu config file.

log_info ""
log_info "EIP-7702 Support Status:"
log_info "  - EIP-7702 is part of Cancun fork (Ethereum mainnet block 19426587)"
log_info "  - If your network uses Cancun fork, EIP-7702 is automatically enabled"
log_info "  - Requires Besu version 24.1.0 or later"
log_info ""
log_info "To enable EIP-7702:"
log_info "  1. Ensure genesis file includes Cancun fork configuration"
log_info "  2. Ensure Besu version is 24.1.0 or later"
log_info "  3. Restart Besu service after updating genesis file"
log_info ""
