#!/usr/bin/env bash
# Beta Testnet Validator 8 Startup Script

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PEZKUWI_DIR="$(dirname "$SCRIPT_DIR")"
BINARY="$PEZKUWI_DIR/target/release/pezkuwi"
BASE_PATH="/tmp/beta-validator-8"
LOG_FILE="/tmp/beta-validator-8.log"

GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'

echo -e "${BLUE}🚀 Starting Beta Testnet Validator 8${NC}"

if [ ! -f "$BINARY" ]; then
    echo "Error: Pezkuwi binary not found at $BINARY"
    exit 1
fi

mkdir -p "$BASE_PATH"

echo -e "${GREEN}📡 Starting validator node...${NC}"
echo "Base path: $BASE_PATH"
echo "Log file: $LOG_FILE"
echo "P2P port: 30336"
echo "RPC port: 9947"
echo ""

nohup "$BINARY" \
    --chain pezkuwichain-beta-testnet \
    --base-path "$BASE_PATH" \
    --validator \
    --name "Validator-beta-8" \
    --port 30340 \
    --rpc-port 9951 \
    --rpc-cors all \
    --rpc-external \
    --rpc-methods=Unsafe \
    --unsafe-rpc-external \
    --unsafe-force-node-key-generation \
    --prometheus-external \
    --prometheus-port 9622 \
    --bootnodes "/ip4/172.31.134.70/tcp/30333/p2p/12D3KooWLdhYkAXWWR7fFSWhMmBFgHVTf78ZWaWf2BpJxzJbTqCU" \
    > "$LOG_FILE" 2>&1 &

NODE_PID=$!
echo "$NODE_PID" > /tmp/beta-validator-8.pid

echo -e "${GREEN}✅ Validator started with PID: $NODE_PID${NC}"
sleep 10

if ps -p $NODE_PID > /dev/null 2>&1; then
    echo -e "${GREEN}✅ Node is running!${NC}"
    tail -20 "$LOG_FILE"
    echo ""
    echo "RPC: http://127.0.0.1:9947"
else
    echo "❌ Node failed to start"
    tail -50 "$LOG_FILE"
    exit 1
fi
