#!/bin/bash
# Run the Besu DEBUG API enablement script on VMID 2500
# This script runs the enablement script remotely via pct exec

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
ENABLE_SCRIPT="$PROJECT_ROOT/scripts/enable-besu-debug-api-final.sh"
VMID=2500

echo "╔══════════════════════════════════════════════════════════════╗"
echo "║     ENABLE DEBUG API ON BESU RPC NODE (VMID $VMID)          ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""

# Check if script exists
if [ ! -f "$ENABLE_SCRIPT" ]; then
    echo "❌ Enable script not found: $ENABLE_SCRIPT"
    exit 1
fi

# Check if pct is available
if ! command -v pct &> /dev/null; then
    echo "❌ pct command not found. This script must be run on Proxmox host."
    echo ""
    echo "Alternative: Copy script manually and run on besu-rpc-1:"
    echo "  scp $ENABLE_SCRIPT root@192.168.11.250:/tmp/"
    echo "  ssh root@192.168.11.250"
    echo "  bash /tmp/enable-besu-debug-api-final.sh"
    exit 1
fi

# Copy script to container
echo "📦 Copying script to container..."
pct push "$VMID" "$ENABLE_SCRIPT" /tmp/enable-besu-debug-api-final.sh
echo "✅ Script copied"
echo ""

# Run script in container
echo "🚀 Running enablement script in container..."
echo ""
pct exec "$VMID" -- bash /tmp/enable-besu-debug-api-final.sh

echo ""
echo "✅ Script execution complete!"
echo ""
echo "📋 Verify DEBUG API is working:"
echo "   pct exec $VMID -- curl -s -X POST -H 'Content-Type: application/json' \\"
echo "     --data '{\"jsonrpc\":\"2.0\",\"method\":\"debug_traceTransaction\",\"params\":[\"0x4dc9f5eedf580c2b37457916b04048481aba19cf3c1a106ea1ee9eefa0dc03c8\",{\"tracer\":\"callTracer\"}],\"id\":1}' \\"
echo "     http://localhost:8545 | jq"

