#!/bin/bash
# Genex Chain — 生产构建脚本
#
# 基于 cosmos/evm v0.5.1, Cosmos SDK v0.53.5, CometBFT v0.38.19
#
# 方式1: 直接构建（需要 Linux/macOS + Go 1.23.8+ + CGO）
#   bash scripts/build-production.sh
#
# 方式2: Docker 构建（推荐，任何平台）
#   docker build -t genex-chain:latest .

set -euo pipefail

echo "============================================"
echo "  Genex Chain — Production Build"
echo "  cosmos/evm v0.5.1 | Cosmos SDK v0.53.5"
echo "============================================"

BINARY_NAME="genexd"
VERSION="1.0.0"
CHAIN_ID="genex-1"
EVM_CHAIN_ID=8888
COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "dev")
BUILD_DIR="./build"

# ============================
# Step 1: Check prerequisites
# ============================
echo ""
echo "[1/4] Checking prerequisites..."

# Check Go version
if ! command -v go &> /dev/null; then
    echo "ERROR: Go is not installed. Minimum required: Go 1.23.8"
    echo "Install from: https://go.dev/dl/"
    exit 1
fi

GO_VERSION=$(go version | awk '{print $3}' | sed 's/go//')
echo "  Go version: $GO_VERSION"

# Check CGO
if [ "$(go env CGO_ENABLED)" != "1" ]; then
    echo "WARNING: CGO is disabled. Enabling..."
    export CGO_ENABLED=1
fi

# Check C compiler
if ! command -v gcc &> /dev/null && ! command -v cc &> /dev/null; then
    echo "ERROR: C compiler not found. Install gcc or clang."
    echo "  Ubuntu/Debian: sudo apt install build-essential"
    echo "  macOS: xcode-select --install"
    echo "  Alpine: apk add gcc musl-dev"
    exit 1
fi
echo "  CGO: enabled"
echo "  C compiler: $(gcc --version 2>/dev/null | head -1 || cc --version 2>/dev/null | head -1)"

# ============================
# Step 2: Resolve dependencies
# ============================
echo ""
echo "[2/4] Resolving dependencies..."
go mod tidy
go mod download
echo "  Dependencies resolved."

# ============================
# Step 3: Build binary
# ============================
echo ""
echo "[3/4] Building binary..."

LDFLAGS="-X github.com/cosmos/cosmos-sdk/version.Name=GenexChain \
         -X github.com/cosmos/cosmos-sdk/version.AppName=$BINARY_NAME \
         -X github.com/cosmos/cosmos-sdk/version.Version=$VERSION \
         -X github.com/cosmos/cosmos-sdk/version.Commit=$COMMIT \
         -X github.com/cosmos/cosmos-sdk/version.BuildTags=netgo,ledger \
         -w -s"

mkdir -p "$BUILD_DIR"

CGO_ENABLED=1 go build \
    -ldflags "$LDFLAGS" \
    -tags "netgo,ledger" \
    -trimpath \
    -o "$BUILD_DIR/$BINARY_NAME" \
    ./cmd/genexd/

echo "  Binary: $BUILD_DIR/$BINARY_NAME"
echo "  Size: $(du -h "$BUILD_DIR/$BINARY_NAME" | awk '{print $1}')"

# ============================
# Step 4: Verify build
# ============================
echo ""
echo "[4/4] Verifying build..."
$BUILD_DIR/$BINARY_NAME version
echo "  Build verified."

# Run module tests
echo ""
echo "Running module tests..."
go test -v ./x/evm/ante/ ./x/evm/keeper/

echo ""
echo "============================================"
echo "  Production Build Complete!"
echo "============================================"
echo ""
echo "  Binary:       $BUILD_DIR/$BINARY_NAME"
echo "  Version:      $VERSION ($COMMIT)"
echo "  Chain ID:     $CHAIN_ID"
echo "  EVM Chain ID: $EVM_CHAIN_ID"
echo "  Bech32:       genex"
echo "  Denom:        agnx (GNX)"
echo ""
echo "  Quick Start:"
echo "    1. Init:  $BUILD_DIR/$BINARY_NAME init my-node --chain-id $CHAIN_ID"
echo "    2. Start: $BUILD_DIR/$BINARY_NAME start --minimum-gas-prices 0agnx --evm.chain-id $EVM_CHAIN_ID"
echo ""
echo "  Docker Deployment:"
echo "    docker build -t genex-chain:$VERSION ."
echo "    docker compose -f blockchain/docker-compose.yml up -d"
echo ""
echo "============================================"
