#!/bin/bash
# Genex Chain — 本地单节点测试链初始化脚本
#
# 基于 cosmos/evm v0.5.1, Cosmos SDK v0.53.5
#
# 用法: bash scripts/init-local.sh
# 或:   make init-local
#
# 此脚本将:
# 1. 编译 genexd 二进制
# 2. 初始化本地节点
# 3. 创建验证者账户
# 4. 配置创世状态（GNX 代币分配）
# 5. 启动本地链

set -euo pipefail

BINARY="./build/genexd"
CHAIN_ID="genex-localnet"
MONIKER="local-validator"
HOME_DIR="$HOME/.genexd"
DENOM="agnx"
KEYRING="test"
EVM_CHAIN_ID=8888

echo "============================================"
echo "  Genex Chain — Local Node Initialization"
echo "  cosmos/evm v0.5.1 | EVM Chain ID: $EVM_CHAIN_ID"
echo "============================================"

# Step 1: Build
echo ""
echo "[1/6] Building genexd..."
make build

# Step 2: Clean old data
echo "[2/6] Cleaning previous data..."
rm -rf "$HOME_DIR"

# Step 3: Initialize node
echo "[3/6] Initializing node..."
$BINARY init "$MONIKER" --chain-id "$CHAIN_ID"

# Step 4: Create accounts
echo "[4/6] Creating accounts..."

# Validator account
echo "  Creating validator account..."
$BINARY keys add validator --keyring-backend "$KEYRING" 2>&1 | tee /tmp/genex-validator-key.txt
VALIDATOR_ADDR=$($BINARY keys show validator -a --keyring-backend "$KEYRING")
echo "  Validator address: $VALIDATOR_ADDR"

# Platform operations account (Gas subsidy pool)
echo "  Creating platform account..."
$BINARY keys add platform --keyring-backend "$KEYRING" 2>&1 | tee /tmp/genex-platform-key.txt
PLATFORM_ADDR=$($BINARY keys show platform -a --keyring-backend "$KEYRING")
echo "  Platform address: $PLATFORM_ADDR"

# Test user accounts
echo "  Creating test user accounts..."
$BINARY keys add user1 --keyring-backend "$KEYRING" > /dev/null 2>&1
$BINARY keys add user2 --keyring-backend "$KEYRING" > /dev/null 2>&1
$BINARY keys add issuer --keyring-backend "$KEYRING" > /dev/null 2>&1

# Step 5: Configure genesis
echo "[5/6] Configuring genesis..."

# Validator: 100M GNX (100M * 10^18 agnx)
$BINARY genesis add-genesis-account validator "100000000000000000000000000${DENOM}" --keyring-backend "$KEYRING"
# Platform: 400M GNX (Gas subsidy pool)
$BINARY genesis add-genesis-account platform "400000000000000000000000000${DENOM}" --keyring-backend "$KEYRING"
# Test users: 1M GNX each
$BINARY genesis add-genesis-account user1 "1000000000000000000000000${DENOM}" --keyring-backend "$KEYRING"
$BINARY genesis add-genesis-account user2 "1000000000000000000000000${DENOM}" --keyring-backend "$KEYRING"
$BINARY genesis add-genesis-account issuer "10000000000000000000000000${DENOM}" --keyring-backend "$KEYRING"

# Create genesis validator tx
$BINARY genesis gentx validator "50000000000000000000000000${DENOM}" \
    --chain-id "$CHAIN_ID" \
    --moniker "$MONIKER" \
    --commission-rate "0.10" \
    --commission-max-rate "0.20" \
    --commission-max-change-rate "0.01" \
    --min-self-delegation "1" \
    --keyring-backend "$KEYRING"

# Collect genesis txs
$BINARY genesis collect-gentxs

# Validate genesis
$BINARY genesis validate-genesis

# Step 6: Configuration tuning
echo "[6/6] Applying configurations..."

# Set minimum-gas-prices = 0 (platform subsidy)
sed -i 's/minimum-gas-prices = ""/minimum-gas-prices = "0agnx"/' "$HOME_DIR/config/app.toml"

# Enable JSON-RPC (EVM)
sed -i 's/enable = false/enable = true/' "$HOME_DIR/config/app.toml"

# Set EVM chain ID
sed -i "s/evm-chain-id = .*/evm-chain-id = $EVM_CHAIN_ID/" "$HOME_DIR/config/app.toml"

echo ""
echo "============================================"
echo "  Initialization Complete!"
echo "============================================"
echo ""
echo "Chain ID:    $CHAIN_ID"
echo "Moniker:     $MONIKER"
echo "Home:        $HOME_DIR"
echo "EVM Chain:   $EVM_CHAIN_ID"
echo "Denom:       $DENOM (GNX)"
echo "Bech32:      genex"
echo ""
echo "Endpoints:"
echo "  EVM RPC:     http://localhost:8545"
echo "  EVM WS:      ws://localhost:8546"
echo "  Cosmos RPC:  http://localhost:26657"
echo "  REST API:    http://localhost:1317"
echo "  gRPC:        localhost:9090"
echo ""
echo "Validator:   $VALIDATOR_ADDR"
echo "Platform:    $PLATFORM_ADDR"
echo ""
echo "Start: make start"
echo "  or:  $BINARY start --evm.chain-id $EVM_CHAIN_ID --minimum-gas-prices 0agnx"
echo ""
echo "MetaMask Config:"
echo "  Network:   Genex Chain (Local)"
echo "  RPC URL:   http://localhost:8545"
echo "  Chain ID:  $EVM_CHAIN_ID"
echo "  Currency:  GNX"
echo ""
echo "Keys saved to /tmp/genex-*-key.txt"
echo "============================================"
