.PHONY: build clean test install run fmt lint help build-all example gen-gateway deps

# Variables
BINARY_NAME=go-doc
BUILD_DIR=bin
MAIN_PATH=./cmd/go-doc
VERSION?=1.0.0
COMMIT=$(shell git rev-parse --short HEAD 2>/dev/null || echo "dev")
BUILD_DATE=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
LDFLAGS=-ldflags "-X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(BUILD_DATE)"

help: ## Display this help screen
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

build: ## Build the binary for the current platform
	@echo "Building $(BINARY_NAME)..."
	@mkdir -p $(BUILD_DIR)
	@go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_PATH)
	@echo "Build complete: $(BUILD_DIR)/$(BINARY_NAME)"

clean: ## Remove build artifacts
	@echo "Cleaning..."
	@rm -rf $(BUILD_DIR)
	@rm -rf example/test_output
	@echo "Clean complete"

test: ## Run tests
	@echo "Running tests..."
	@go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
	@echo "Tests complete"

install: ## Install the binary to GOPATH/bin (use directly as go-doc command)
	@echo "Installing $(BINARY_NAME) to GOPATH/bin..."
	@go install $(LDFLAGS) $(MAIN_PATH)
	@echo "Install complete. Run: go-doc --help"

run: build ## Build and run with example API file
	@echo "Running $(BINARY_NAME)..."
	@$(BUILD_DIR)/$(BINARY_NAME) -a example/example_cn.api -d example/test_output -f example -s openapi3.0
	@echo "Generated: example/test_output/example.json"

fmt: ## Format code
	@echo "Formatting code..."
	@gofmt -s -w .
	@goimports -w .
	@echo "Format complete"

lint: ## Run linters
	@echo "Running linters..."
	@golangci-lint run || (echo "golangci-lint not found, skipping..." && exit 0)
	@echo "Lint complete"

deps: ## Download dependencies
	@echo "Downloading dependencies..."
	@go mod download
	@go mod tidy
	@echo "Dependencies updated"

# Build for multiple platforms
# Output layout:
#   bin/darwin/amd64/go-doc
#   bin/darwin/arm64/go-doc
#   bin/linux/amd64/go-doc
#   bin/linux/arm64/go-doc
#   bin/windows/amd64/go-doc.exe
# Copy the binary for your OS/arch to a directory in your PATH and use it as: go-doc
build-all: ## Build for macOS, Linux, and Windows (amd64 + arm64)
	@echo "Building for all platforms..."
	@mkdir -p $(BUILD_DIR)/darwin/amd64  $(BUILD_DIR)/darwin/arm64
	@mkdir -p $(BUILD_DIR)/linux/amd64   $(BUILD_DIR)/linux/arm64
	@mkdir -p $(BUILD_DIR)/windows/amd64
	@echo "  darwin/amd64..."
	@GOOS=darwin  GOARCH=amd64 go build $(LDFLAGS) -o $(BUILD_DIR)/darwin/amd64/$(BINARY_NAME)      $(MAIN_PATH)
	@echo "  darwin/arm64 (Apple Silicon)..."
	@GOOS=darwin  GOARCH=arm64 go build $(LDFLAGS) -o $(BUILD_DIR)/darwin/arm64/$(BINARY_NAME)      $(MAIN_PATH)
	@echo "  linux/amd64..."
	@GOOS=linux   GOARCH=amd64 go build $(LDFLAGS) -o $(BUILD_DIR)/linux/amd64/$(BINARY_NAME)       $(MAIN_PATH)
	@echo "  linux/arm64..."
	@GOOS=linux   GOARCH=arm64 go build $(LDFLAGS) -o $(BUILD_DIR)/linux/arm64/$(BINARY_NAME)       $(MAIN_PATH)
	@echo "  windows/amd64..."
	@GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -o $(BUILD_DIR)/windows/amd64/$(BINARY_NAME).exe $(MAIN_PATH)
	@echo ""
	@echo "Build complete. Binaries:"
	@echo "  macOS Intel  : $(BUILD_DIR)/darwin/amd64/$(BINARY_NAME)"
	@echo "  macOS Apple  : $(BUILD_DIR)/darwin/arm64/$(BINARY_NAME)"
	@echo "  Linux amd64  : $(BUILD_DIR)/linux/amd64/$(BINARY_NAME)"
	@echo "  Linux arm64  : $(BUILD_DIR)/linux/arm64/$(BINARY_NAME)"
	@echo "  Windows amd64: $(BUILD_DIR)/windows/amd64/$(BINARY_NAME).exe"
	@echo ""
	@echo "Copy the binary for your platform to a directory in PATH to use: go-doc --help"

example: build ## Generate example swagger and openapi files
	@echo "Generating examples..."
	@mkdir -p example/test_output
	@echo "  - Swagger 2.0 (JSON)..."
	@$(BUILD_DIR)/$(BINARY_NAME) -a example/example.api -d example/test_output -f example_swagger2
	@echo "  - Swagger 2.0 (YAML)..."
	@$(BUILD_DIR)/$(BINARY_NAME) -a example/example.api -d example/test_output -f example_swagger2 -y
	@echo "  - OpenAPI 3.0 (JSON)..."
	@$(BUILD_DIR)/$(BINARY_NAME) -a example/example.api -d example/test_output -f example_openapi3 -s openapi3.0
	@echo "  - OpenAPI 3.0 (YAML)..."
	@$(BUILD_DIR)/$(BINARY_NAME) -a example/example.api -d example/test_output -f example_openapi3 -s openapi3.0 -y
	@echo "  - Chinese Swagger 2.0..."
	@$(BUILD_DIR)/$(BINARY_NAME) -a example/example_cn.api -d example/test_output -f example_cn_swagger2
	@echo "  - Chinese OpenAPI 3.0..."
	@$(BUILD_DIR)/$(BINARY_NAME) -a example/example_cn.api -d example/test_output -f example_cn_openapi3 -s openapi3.0
	@echo "Examples generated in example/test_output/"

gen-gateway: build ## Generate Gateway API documentation (OpenAPI 3.0)
	@echo "Generating Gateway API documentation..."
	@mkdir -p example/test_output
	@$(BUILD_DIR)/$(BINARY_NAME) -a example/api/gateway.api -d example/test_output -f gateway -s openapi3.0
	@echo "Generated: example/test_output/gateway.json"

