#!/bin/bash
# Configure Besu Nodes on All Backend VMs
# This script automates the configuration of Besu on all 5 backend VMs
# Requires: VPN/Bastion access to backend VMs

set -euo pipefail

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

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

echo "=========================================="
echo "Configure All Besu Nodes"
echo "=========================================="
echo ""

# Backend VM configuration
declare -A BACKEND_VMS=(
    ["centralus"]="10.3.1.4:az-p-cus-vm-besu-node-0"
    ["eastus"]="10.1.1.4:az-p-eus-vm-besu-node-0"
    ["eastus2"]="10.4.1.4:az-p-eus2-vm-besu-node-0"
    ["westus"]="10.2.1.4:az-p-wus-vm-besu-node-0"
    ["westus2"]="10.5.1.4:az-p-wus2-vm-besu-node-0"
)

# Check if genesis file exists
GENESIS_FILE="$PHASE1_DIR/config/genesis-138.json"
if [ ! -f "$GENESIS_FILE" ]; then
    echo -e "${RED}Error: Genesis file not found: $GENESIS_FILE${NC}"
    exit 1
fi

echo -e "${BLUE}Genesis file: $GENESIS_FILE${NC}"
echo ""

# Function to configure a single Besu node
configure_besu_node() {
    local region=$1
    local ip=$2
    local vm_name=$3
    
    echo -e "${BLUE}========================================${NC}"
    echo -e "${BLUE}Configuring Besu Node: $region${NC}"
    echo -e "${BLUE}IP: $ip${NC}"
    echo -e "${BLUE}VM: $vm_name${NC}"
    echo -e "${BLUE}========================================${NC}"
    echo ""
    
    # Copy setup script
    echo -e "${YELLOW}Copying setup script to VM...${NC}"
    scp "$SCRIPT_DIR/setup-besu-node.sh" besuadmin@$ip:/tmp/ 2>/dev/null || {
        echo -e "${RED}Error: Could not copy setup script to $ip${NC}"
        echo -e "${YELLOW}Please ensure VPN/Bastion access is configured${NC}"
        return 1
    }
    
    # Copy genesis file
    echo -e "${YELLOW}Copying genesis file to VM...${NC}"
    scp "$GENESIS_FILE" besuadmin@$ip:/tmp/genesis.json 2>/dev/null || {
        echo -e "${RED}Error: Could not copy genesis file to $ip${NC}"
        return 1
    }
    
    # Run setup script
    echo -e "${YELLOW}Running Besu setup script...${NC}"
    ssh besuadmin@$ip "chmod +x /tmp/setup-besu-node.sh && sudo /tmp/setup-besu-node.sh besu-node 0 $region" || {
        echo -e "${RED}Error: Setup script failed on $ip${NC}"
        return 1
    }
    
    # Copy genesis file to correct location
    echo -e "${YELLOW}Installing genesis file...${NC}"
    ssh besuadmin@$ip "sudo cp /tmp/genesis.json /opt/besu/config/genesis.json && sudo chown besuadmin:besuadmin /opt/besu/config/genesis.json" || {
        echo -e "${RED}Error: Failed to install genesis file${NC}"
        return 1
    }
    
    # Start Besu service
    echo -e "${YELLOW}Starting Besu service...${NC}"
    ssh besuadmin@$ip "sudo systemctl start besu.service && sleep 5 && sudo systemctl status besu.service --no-pager | head -10" || {
        echo -e "${YELLOW}Warning: Service start may have failed. Check logs.${NC}"
    }
    
    # Verify Besu is running
    echo -e "${YELLOW}Verifying Besu is running...${NC}"
    sleep 10
    if ssh besuadmin@$ip "docker ps | grep -q besu"; then
        echo -e "${GREEN}✓ Besu container is running${NC}"
    else
        echo -e "${YELLOW}⚠ Besu container not running. Check logs:${NC}"
        echo -e "${YELLOW}  ssh besuadmin@$ip 'docker logs besu-besu-node-0'${NC}"
    fi
    
    # Test RPC endpoint
    echo -e "${YELLOW}Testing RPC endpoint...${NC}"
    sleep 5
    if ssh besuadmin@$ip "curl -s http://localhost:8545 -X POST -H 'Content-Type: application/json' -d '{\"jsonrpc\":\"2.0\",\"method\":\"eth_blockNumber\",\"params\":[],\"id\":1}' | grep -q jsonrpc"; then
        echo -e "${GREEN}✓ RPC endpoint is responding${NC}"
    else
        echo -e "${YELLOW}⚠ RPC endpoint not responding yet (may need more time to start)${NC}"
    fi
    
    echo -e "${GREEN}✓ Configuration complete for $region${NC}"
    echo ""
}

# Main execution
echo -e "${BLUE}Starting configuration of ${#BACKEND_VMS[@]} Besu nodes...${NC}"
echo ""

SUCCESS_COUNT=0
FAILED_REGIONS=()

for region in "${!BACKEND_VMS[@]}"; do
    IFS=':' read -r ip vm_name <<< "${BACKEND_VMS[$region]}"
    
    if configure_besu_node "$region" "$ip" "$vm_name"; then
        ((SUCCESS_COUNT++))
    else
        FAILED_REGIONS+=("$region")
    fi
    
    echo ""
done

# Summary
echo "=========================================="
echo "Configuration Summary"
echo "=========================================="
echo -e "${GREEN}Successfully configured: $SUCCESS_COUNT/${#BACKEND_VMS[@]} nodes${NC}"

if [ ${#FAILED_REGIONS[@]} -gt 0 ]; then
    echo -e "${RED}Failed regions:${NC}"
    for region in "${FAILED_REGIONS[@]}"; do
        echo -e "${RED}  - $region${NC}"
    done
    echo ""
    echo -e "${YELLOW}To retry failed regions:${NC}"
    echo -e "${YELLOW}  ssh besuadmin@<ip>${NC}"
    echo -e "${YELLOW}  sudo /tmp/setup-besu-node.sh besu-node 0 <region>${NC}"
fi

echo ""
echo -e "${BLUE}Next steps:${NC}"
echo "1. Verify all nodes are running: ssh besuadmin@<ip> 'docker ps'"
echo "2. Test RPC endpoints: curl http://<ip>:8545"
echo "3. Check logs: ssh besuadmin@<ip> 'docker logs besu-besu-node-0'"
echo "4. Configure cross-region connectivity"
echo ""

if [ $SUCCESS_COUNT -eq ${#BACKEND_VMS[@]} ]; then
    echo -e "${GREEN}✓ All Besu nodes configured successfully!${NC}"
    exit 0
else
    echo -e "${YELLOW}⚠ Some nodes failed to configure. Review errors above.${NC}"
    exit 1
fi

