#!/usr/bin/env bash
# Remove stuck transaction using Besu txpool_besuTransactions
# This script finds and removes the stuck transaction with nonce 23
# Usage: ./remove-stuck-transaction-besu.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_PROJECT="/home/intlc/projects/smom-dbis-138"

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

# Load environment variables
if [ -f "$SOURCE_PROJECT/.env" ]; then
    source "$SOURCE_PROJECT/.env"
else
    log_error ".env file not found in $SOURCE_PROJECT"
    exit 1
fi

RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
DEPLOYER=$(cast wallet address --private-key "$PRIVATE_KEY" 2>/dev/null || echo "")

if [ -z "$DEPLOYER" ]; then
    log_error "Failed to get deployer address"
    exit 1
fi

log_info "========================================="
log_info "Remove Stuck Transaction (Besu QBFT)"
log_info "========================================="
log_info ""
log_info "RPC URL: $RPC_URL"
log_info "Deployer: $DEPLOYER"
log_info ""

# Get current nonce
CURRENT_NONCE=$(cast nonce "$DEPLOYER" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
log_info "Current nonce: $CURRENT_NONCE"

# Get all transactions in pool
log_info "Fetching transactions from pool..."
TX_POOL=$(curl -s -X POST -H "Content-Type: application/json" \
    --data '{"jsonrpc":"2.0","method":"txpool_besuTransactions","params":[],"id":1}' \
    "$RPC_URL" 2>/dev/null || echo "")

if [ -z "$TX_POOL" ] || echo "$TX_POOL" | jq -e '.error' >/dev/null 2>&1; then
    ERROR_MSG=$(echo "$TX_POOL" | jq -r '.error.message // "Unknown error"' 2>/dev/null || echo "Failed to get transactions")
    log_error "Failed to get transaction pool: $ERROR_MSG"
    exit 1
fi

TX_COUNT=$(echo "$TX_POOL" | jq -r '.result | length' 2>/dev/null || echo "0")
log_info "Found $TX_COUNT transactions in pool"

if [ "$TX_COUNT" = "0" ]; then
    log_warn "⚠ No transactions in pool"
    log_info "Transaction may have been processed or cleared"
    exit 0
fi

# Find transactions for deployer with nonce 23
log_info "Searching for stuck transaction (nonce 23)..."
STUCK_TX=$(echo "$TX_POOL" | jq -r --arg deployer "$DEPLOYER" \
    '.result[] | select(.from == $deployer and (.nonce // "0" | tonumber) == 23)' 2>/dev/null || echo "")

if [ -z "$STUCK_TX" ] || [ "$STUCK_TX" = "null" ]; then
    log_warn "⚠ No transaction found with nonce 23 for deployer"
    log_info "Showing all deployer transactions:"
    echo "$TX_POOL" | jq -r --arg deployer "$DEPLOYER" \
        '.result[] | select(.from == $deployer) | "  Nonce: \(.nonce // "N/A"), Hash: \(.hash // "N/A"), Gas Price: \(.gasPrice // "N/A")"' 2>/dev/null || echo "  None found"
    exit 0
fi

STUCK_TX_HASH=$(echo "$STUCK_TX" | jq -r '.hash // .transactionHash' 2>/dev/null || echo "")
STUCK_TX_NONCE=$(echo "$STUCK_TX" | jq -r '.nonce // "N/A"' 2>/dev/null || echo "")
STUCK_TX_GAS=$(echo "$STUCK_TX" | jq -r '.gasPrice // "N/A"' 2>/dev/null || echo "")

log_warn "⚠ Found stuck transaction:"
log_info "  Hash: $STUCK_TX_HASH"
log_info "  Nonce: $STUCK_TX_NONCE"
log_info "  Gas Price: $STUCK_TX_GAS"
log_info ""

# Try to remove transaction using admin_removeTransaction (if ADMIN enabled)
log_info "Attempting to remove transaction..."

# Check if ADMIN is enabled
RPC_MODULES=$(curl -s -X POST -H "Content-Type: application/json" \
    --data '{"jsonrpc":"2.0","method":"rpc_modules","params":[],"id":1}' \
    "$RPC_URL" 2>/dev/null || echo "")

if echo "$RPC_MODULES" | jq -r '.result | keys[]' 2>/dev/null | grep -qi "admin"; then
    log_info "ADMIN module is enabled, trying admin_removeTransaction..."
    
    REMOVE_RESULT=$(curl -s -X POST -H "Content-Type: application/json" \
        --data "{\"jsonrpc\":\"2.0\",\"method\":\"admin_removeTransaction\",\"params\":[\"$STUCK_TX_HASH\"],\"id\":1}" \
        "$RPC_URL" 2>/dev/null || echo "")
    
    if echo "$REMOVE_RESULT" | jq -e '.result == true' >/dev/null 2>&1; then
        log_success "✓ Transaction removed successfully"
        sleep 2
        
        # Verify nonce
        NEW_NONCE=$(cast nonce "$DEPLOYER" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
        log_info "Nonce after removal: $NEW_NONCE"
        exit 0
    elif echo "$REMOVE_RESULT" | jq -e '.error' >/dev/null 2>&1; then
        ERROR_MSG=$(echo "$REMOVE_RESULT" | jq -r '.error.message // "Unknown error"' 2>/dev/null || echo "Unknown")
        log_warn "⚠ admin_removeTransaction failed: $ERROR_MSG"
    else
        log_warn "⚠ Unexpected response from admin_removeTransaction"
    fi
else
    log_warn "⚠ ADMIN module is not enabled"
    log_info "Cannot remove transaction via RPC"
fi

# Alternative: Restart Besu to clear pool
log_info ""
log_warn "⚠ Automatic removal not available"
log_info ""
log_info "Alternative solutions:"
log_info "  1. Enable ADMIN on RPC node and retry"
log_info "  2. Restart Besu RPC node (clears mempool)"
log_info "  3. Use a different deployer account"
log_info "  4. Wait for transaction retention period to expire"
log_info ""
log_info "To enable ADMIN, add to RPC config:"
log_info "  rpc-http-api=[\"ETH\",\"NET\",\"WEB3\",\"TXPOOL\",\"ADMIN\"]"
log_info "Then restart: systemctl restart besu-rpc"

