# Flaxon++ Go sidecar Makefile
#
# Usage:
#   make build         - Build for current platform
#   make build-all     - Build for all platforms
#   make test          - Run tests
#   make clean         - Clean build artifacts
#   make install       - Install to $GOPATH/bin
#   make fmt           - Format code
#   make lint          - Run linter

.PHONY: build build-all test clean install fmt lint

# Variables
BINARY_NAME := flaxonpp
BUILD_DIR := bin
GO := go
GOFLAGS := -trimpath -ldflags="-s -w"

# Version information
VERSION := 0.2.0
COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S')
LDFLAGS := -ldflags="-X main.version=${VERSION} -X main.commit=${COMMIT} -X main.buildTime=${BUILD_TIME}"

# Platform-specific builds
PLATFORMS := linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64

# Default target
all: build

# Build for current platform
build:
	@echo "Building ${BINARY_NAME} for current platform..."
	@mkdir -p ${BUILD_DIR}
	${GO} build ${LDFLAGS} ${GOFLAGS} -o ${BUILD_DIR}/${BINARY_NAME} ./cmd/flaxonpp
	@echo "Build complete: ${BUILD_DIR}/${BINARY_NAME}"

# Build for all platforms
build-all: $(addprefix build-,$(PLATFORMS))

build-linux/amd64:
	@echo "Building for Linux/amd64..."
	@mkdir -p ${BUILD_DIR}
	GOOS=linux GOARCH=amd64 ${GO} build ${LDFLAGS} ${GOFLAGS} -o ${BUILD_DIR}/${BINARY_NAME}-linux-amd64 ./cmd/flaxonpp

build-linux/arm64:
	@echo "Building for Linux/arm64..."
	@mkdir -p ${BUILD_DIR}
	GOOS=linux GOARCH=arm64 ${GO} build ${LDFLAGS} ${GOFLAGS} -o ${BUILD_DIR}/${BINARY_NAME}-linux-arm64 ./cmd/flaxonpp

build-darwin/amd64:
	@echo "Building for Darwin/amd64..."
	@mkdir -p ${BUILD_DIR}
	GOOS=darwin GOARCH=amd64 ${GO} build ${LDFLAGS} ${GOFLAGS} -o ${BUILD_DIR}/${BINARY_NAME}-darwin-amd64 ./cmd/flaxonpp

build-darwin/arm64:
	@echo "Building for Darwin/arm64..."
	@mkdir -p ${BUILD_DIR}
	GOOS=darwin GOARCH=arm64 ${GO} build ${LDFLAGS} ${GOFLAGS} -o ${BUILD_DIR}/${BINARY_NAME}-darwin-arm64 ./cmd/flaxonpp

build-windows/amd64:
	@echo "Building for Windows/amd64..."
	@mkdir -p ${BUILD_DIR}
	GOOS=windows GOARCH=amd64 ${GO} build ${LDFLAGS} ${GOFLAGS} -o ${BUILD_DIR}/${BINARY_NAME}-windows-amd64.exe ./cmd/flaxonpp

# Run tests
test:
	@echo "Running tests..."
	${GO} test -v -race -cover ./...

# Run tests with coverage
test-coverage:
	@echo "Running tests with coverage..."
	${GO} test -v -race -coverprofile=coverage.out ./...
	${GO} tool cover -html=coverage.out -o coverage.html
	@echo "Coverage report: coverage.html"

# Clean build artifacts
clean:
	@echo "Cleaning build artifacts..."
	rm -rf ${BUILD_DIR}/
	rm -f coverage.out coverage.html
	@echo "Clean complete"

# Install to $GOPATH/bin
install: build
	@echo "Installing ${BINARY_NAME} to $${GOPATH}/bin..."
	@cp ${BUILD_DIR}/${BINARY_NAME} $(shell ${GO} env GOPATH)/bin/
	@echo "Install complete"

# Format code
fmt:
	@echo "Formatting code..."
	${GO} fmt ./...

# Run linter
lint:
	@echo "Running linter..."
	@if command -v golangci-lint >/dev/null 2>&1; then \
		golangci-lint run ./...; \
	else \
		echo "golangci-lint not installed, skipping"; \
	fi

# Run static analysis
vet:
	@echo "Running static analysis..."
	${GO} vet ./...

# Download dependencies
deps:
	@echo "Downloading dependencies..."
	${GO} mod download
	${GO} mod verify

# Tidy dependencies
tidy:
	@echo "Tidying dependencies..."
	${GO} mod tidy

# Generate protocol test fixtures
test-fixtures:
	@echo "Generating protocol test fixtures..."
	${GO} test -run TestGenerateFixtures ./internal/ipc/...

# Development build with debug symbols
build-dev:
	@echo "Building ${BINARY_NAME} for development..."
	@mkdir -p ${BUILD_DIR}
	${GO} build -gcflags="all=-N -l" -o ${BUILD_DIR}/${BINARY_NAME}-dev ./cmd/flaxonpp
	@echo "Dev build complete: ${BUILD_DIR}/${BINARY_NAME}-dev"

# Run in development mode
dev: build-dev
	@echo "Running in development mode..."
	${BUILD_DIR}/${BINARY_NAME}-dev -log-level debug

# Help
help:
	@echo "Available targets:"
	@echo "  build          - Build for current platform"
	@echo "  build-all      - Build for all platforms"
	@echo "  build-dev      - Build for development (with debug symbols)"
	@echo "  test           - Run tests"
	@echo "  test-coverage  - Run tests with coverage report"
	@echo "  clean          - Clean build artifacts"
	@echo "  install        - Install to $$GOPATH/bin"
	@echo "  fmt            - Format code"
	@echo "  lint           - Run linter"
	@echo "  vet            - Run static analysis"
	@echo "  deps           - Download dependencies"
	@echo "  tidy           - Tidy dependencies"
	@echo "  dev            - Run in development mode"
	@echo "  help           - Show this help"
