#!/usr/bin/env bash
# Clear entire Besu blockchain database (NUCLEAR OPTION)
# This will require full re-sync from genesis
# Usage: ./clear-blockchain-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 Entire Blockchain Database"
echo "========================================="
echo ""
log_error "⚠️  ⚠️  ⚠️  CRITICAL WARNING ⚠️  ⚠️  ⚠️"
echo ""
log_error "This will:"
log_error "  1. DELETE the entire blockchain database"
log_error "  2. DELETE all transaction pools"
log_error "  3. DELETE all caches"
log_error "  4. Require FULL RE-SYNC from genesis"
log_error "  5. Take SIGNIFICANT TIME to re-sync"
echo ""
log_warn "This is a NUCLEAR OPTION - use only if absolutely necessary"
echo ""
read -p "Type 'DELETE DATABASE' to confirm: " CONFIRM
if [ "$CONFIRM" != "DELETE DATABASE" ]; 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 entire blockchain 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 database
        pct exec "$vmid" -- rm -rf /data/besu/database/* 2>/dev/null || true
        
        # Clear caches
        pct exec "$vmid" -- rm -rf /data/besu/caches/* 2>/dev/null || true
        
        # Clear any transaction pool files
        pct exec "$vmid" -- find /data/besu -type f -name "*pool*" -delete 2>/dev/null || true
        pct exec "$vmid" -- find /data/besu -type d -name "*pool*" -exec rm -rf {} \; 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 30 seconds for services to start..."
sleep 30

log_success "========================================="
log_success "Blockchain Database Cleared!"
log_success "========================================="
log_info ""
log_warn "⚠️  Nodes will now re-sync from genesis"
log_warn "⚠️  This may take significant time"
log_info ""
log_info "Next steps:"
log_info "  1. Wait for nodes to re-sync (monitor block numbers)"
log_info "  2. Once synced, run: ./scripts/configure-ethereum-mainnet-final.sh"
log_info ""

