# ============================================================
# Genex Chain — Makefile
# ============================================================
# 基于 Cosmos SDK + cosmos/evm + CometBFT
#
# 需要: Go 1.23.8+, CGO, Linux/macOS (或 Docker)
# ============================================================

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

# Build flags
LDFLAGS := -X github.com/cosmos/cosmos-sdk/version.Name=$(APP_NAME) \
           -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

BUILD_FLAGS := -ldflags '$(LDFLAGS)' -tags "netgo,ledger" -trimpath

.PHONY: all build install clean test lint docker docker-run docker-stop \
        init-local start deps format help version

## help: Display this help message
help:
	@echo "Genex Chain — Build System"
	@echo ""
	@echo "Usage: make <target>"
	@echo ""
	@echo "Targets:"
	@grep -E '^## ' Makefile | sed 's/## /  /'

## all: Build the binary
all: build

## deps: Download and tidy dependencies
deps:
	@echo "==> Downloading dependencies..."
	go mod tidy
	go mod download

## build: Build genexd binary
build: deps
	@echo "==> Building $(BINARY_NAME)..."
	@mkdir -p $(BUILD_DIR)
	CGO_ENABLED=1 go build $(BUILD_FLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/genexd/
	@echo "Binary: $(BUILD_DIR)/$(BINARY_NAME)"

## install: Install genexd to GOPATH/bin
install: deps
	@echo "==> Installing $(BINARY_NAME)..."
	CGO_ENABLED=1 go install $(BUILD_FLAGS) ./cmd/genexd/

## test: Run all tests
test:
	@echo "==> Running tests..."
	go test -v ./x/evm/ante/ ./x/evm/keeper/
	@echo "All tests passed!"

## test-contracts: Run Solidity contract tests (requires Foundry)
test-contracts:
	@echo "==> Running contract tests..."
	cd ../genex-contracts && forge test -vvv

## lint: Run linters
lint:
	@echo "==> Running linters..."
	@which golangci-lint > /dev/null 2>&1 || (echo "golangci-lint not found" && exit 1)
	golangci-lint run ./...

## format: Format Go code
format:
	@echo "==> Formatting code..."
	gofmt -s -w .

## clean: Remove build artifacts
clean:
	@echo "==> Cleaning..."
	rm -rf $(BUILD_DIR)

## init-local: Initialize a local single-node testnet
init-local: build
	@echo "==> Initializing local testnet..."
	$(BUILD_DIR)/$(BINARY_NAME) init genex-local --chain-id $(CHAIN_ID)
	@echo "Local testnet initialized at ~/.genexd"
	@echo "Start with: make start"

## start: Start the local node
start:
	@echo "==> Starting Genex Chain..."
	$(BUILD_DIR)/$(BINARY_NAME) start \
		--minimum-gas-prices 0agnx \
		--evm.chain-id $(EVM_CHAIN_ID)

## docker: Build Docker image
docker:
	@echo "==> Building Docker image..."
	docker build -t $(DOCKER_IMAGE):$(VERSION) -t $(DOCKER_IMAGE):latest .
	@echo "Image: $(DOCKER_IMAGE):$(VERSION)"

## docker-run: Run single node in Docker
docker-run:
	@echo "==> Running Genex Chain in Docker..."
	docker run -d --name genex-node \
		-p 26656:26656 -p 26657:26657 \
		-p 8545:8545 -p 8546:8546 \
		-p 1317:1317 -p 9090:9090 \
		$(DOCKER_IMAGE):latest

## docker-stop: Stop Docker node
docker-stop:
	docker stop genex-node && docker rm genex-node

## version: Show version
version:
	@echo "$(APP_NAME) $(VERSION) ($(COMMIT))"
	@echo "Chain ID:     $(CHAIN_ID)"
	@echo "EVM Chain ID: $(EVM_CHAIN_ID)"
	@echo "Denom:        agnx (GNX)"
	@echo "Bech32:       genex"
