#!/usr/bin/env bash
# Clear Besu transaction pool database to remove stuck transactions
# This script must be run on the Proxmox host
# Usage: ./clear-transaction-pool-database.sh

set -euo pipefail

# 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"; }

# Check if pct is available
if ! command -v pct &>/dev/null; then
    log_error "This script must be run on the Proxmox host (pct command not found)"
    exit 1
fi

echo "========================================="
echo "Clear Besu Transaction Pool Database"
echo "========================================="
echo ""
log_warn "⚠️  WARNING: This will stop Besu nodes and clear transaction pools"
log_warn "⚠️  All pending transactions will be lost"
echo ""
read -p "Continue? (yes/no): " CONFIRM
if [ "$CONFIRM" != "yes" ]; then
    log_info "Aborted"
    exit 0
fi

# All Besu nodes
VALIDATORS=(1000 1001 1002 1003 1004)
RPC_NODES=(2500 2501 2502)

log_info "Stopping all Besu nodes..."
for vmid in "${VALIDATORS[@]}"; do
    if pct status "$vmid" 2>/dev/null | grep -q "running"; then
        log_info "Stopping VMID $vmid (validator)..."
        pct exec "$vmid" -- systemctl stop besu-validator.service 2>/dev/null || true
    fi
done

for vmid in "${RPC_NODES[@]}"; do
    if pct status "$vmid" 2>/dev/null | grep -q "running"; then
        log_info "Stopping VMID $vmid (RPC)..."
        pct exec "$vmid" -- systemctl stop besu-rpc.service 2>/dev/null || true
    fi
done

sleep 5

log_info "Clearing transaction pool databases..."
for vmid in "${VALIDATORS[@]}" "${RPC_NODES[@]}"; do
    if pct status "$vmid" 2>/dev/null | grep -q "running"; then
        log_info "Clearing VMID $vmid..."
        
        # Clear caches
        pct exec "$vmid" -- rm -rf /data/besu/caches/* 2>/dev/null || true
        
        # Try to find and clear transaction pool database
        # Location varies by Besu version
        pct exec "$vmid" -- find /data/besu -type d -name "*pool*" -exec rm -rf {} \; 2>/dev/null || true
        pct exec "$vmid" -- find /data/besu -type f -name "*pool*" -delete 2>/dev/null || true
        pct exec "$vmid" -- find /data/besu -type f -name "*transaction*" -delete 2>/dev/null || true
        
        log_success "✓ VMID $vmid cleared"
    fi
done

log_info "Starting all Besu nodes..."
for vmid in "${VALIDATORS[@]}"; do
    if pct status "$vmid" 2>/dev/null | grep -q "running"; then
        log_info "Starting VMID $vmid (validator)..."
        pct exec "$vmid" -- systemctl start besu-validator.service 2>/dev/null || true
    fi
done

for vmid in "${RPC_NODES[@]}"; do
    if pct status "$vmid" 2>/dev/null | grep -q "running"; then
        log_info "Starting VMID $vmid (RPC)..."
        pct exec "$vmid" -- systemctl start besu-rpc.service 2>/dev/null || true
    fi
done

log_info "Waiting 20 seconds for services to start..."
sleep 20

log_success "========================================="
log_success "Transaction Pool Database Cleared!"
log_success "========================================="
log_info ""
log_info "Next steps:"
log_info "  1. Wait for nodes to sync"
log_info "  2. Run: ./scripts/configure-ethereum-mainnet-final.sh"
log_info ""

