.PHONY: build test lint clean fmt vet version cross-compile release install uninstall

# Version handling - override with VERSION=0.2.0 make build
VERSION := $(shell cat VERSION 2>/dev/null || echo "0.1.0")
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
BUILD_TIME := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")

BINARY  := obm
LDFLAGS := -ldflags "-s -w -X main.version=$(VERSION) -X main.gitCommit=$(GIT_COMMIT) -X main.buildTime=$(BUILD_TIME)"

# Go parameters
GOCMD := go
GOBUILD := $(GOCMD) build
GOCLEAN := $(GOCMD) clean
GOTEST := $(GOCMD) test
GOGET := $(GOCMD) get
GOMOD := $(GOCMD) mod
GOVET := $(GOCMD) vet

# Cross-compilation targets
TARGETS := linux-amd64 linux-arm64 darwin-arm64 windows-amd64 windows-arm64

# Platform detection
GOOS := $(shell go env GOOS)
GOARCH := $(shell go env GOARCH)

all: lint test build

build:
	@echo "Building $(BINARY) v$(VERSION) for $(GOOS)/$(GOARCH)..."
	$(GOBUILD) $(LDFLAGS) -o $(BINARY) ./cmd/obm

test:
	@echo "Running tests..."
	$(GOTEST) -v -race -coverprofile=coverage.out ./...

lint: vet fmt
	@echo "✓ Lint pass"

vet:
	@echo "Running go vet..."
	$(GOVET) ./...

fmt:
	@echo "Formatting code..."
	gofmt -l -s -w .

clean:
	@echo "Cleaning..."
	rm -f $(BINARY)
	rm -f coverage.out
	rm -rf bin/
	$(GOCLEAN)

run: build
	./$(BINARY)

install: build
	@echo "Installing $(BINARY)..."
	cp $(BINARY) $(GOPATH)/bin/

uninstall: clean
	@echo "Uninstalling $(BINARY)..."
	rm -f $(GOPATH)/bin/$(BINARY)

version:
	@echo "$(VERSION)"

# Cross-compilation
cross-compile:
	@echo "Cross-compiling for all platforms..."
	@mkdir -p bin
	@for target in $(TARGETS); do \
		os=$${target%-*}; \
		arch=$${target#*-}; \
		echo "Building for $$os/$$arch..."; \
		binary="$(BINARY)"; \
		ext=""; \
		if [ "$$os" = "windows" ]; then \
			ext=".exe"; \
		fi; \
		GOOS=$$os GOARCH=$$arch CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o "bin/$$target/$$binary$$ext" ./cmd/obm; \
	done
	@echo "✓ Cross-compilation complete"

# Build specific platform
build-linux-amd64:
	@mkdir -p bin
	GOOS=linux GOARCH=amd64 CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o bin/linux-amd64/$(BINARY) ./cmd/obm

build-linux-arm64:
	@mkdir -p bin
	GOOS=linux GOARCH=arm64 CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o bin/linux-arm64/$(BINARY) ./cmd/obm

build-darwin-arm64:
	@mkdir -p bin
	GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o bin/darwin-arm64/$(BINARY) ./cmd/obm

build-windows-amd64:
	@mkdir -p bin
	GOOS=windows GOARCH=amd64 CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o bin/windows-amd64/$(BINARY).exe ./cmd/obm

build-windows-arm64:
	@mkdir -p bin
	GOOS=windows GOARCH=arm64 CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o bin/windows-arm64/$(BINARY).exe ./cmd/obm

# Create release archives
release-archives: cross-compile
	@echo "Creating release archives..."
	@cd bin && \
	for dir in */; do \
		platform=$${dir%/}; \
		echo "Archiving $$platform..."; \
		if [[ "$$platform" == windows-* ]]; then \
			zip -j $$platform.zip $$platform/; \
		else \
			tar -czf $$platform.tar.gz -C $$platform .; \
		fi; \
	done
	@echo "✓ Release archives created in bin/"

# Checksums
checksums:
	@echo "Generating checksums..."
	@cd bin && sha256sum *.tar.gz *.zip > checksums.txt 2>/dev/null || true
	@echo "✓ Checksums in bin/checksums.txt"

# Full release preparation (local)
prepare-release: clean test cross-compile release-archives checksums
	@echo ""
	@echo "Release v$(VERSION) prepared in bin/"
	@ls -la bin/

# Docker build (for containerized usage)
docker-build:
	docker build -t obm:$(VERSION) -t obm:latest .

# Development
dev: build test
	@echo "✓ Development build complete"

# Quick test during development
quick-test:
	$(GOTEST) -race ./...

# Coverage report
coverage: test
	$(GOCMD) tool cover -html=coverage.out -o coverage.html
	@echo "Coverage report: coverage.html"