.PHONY: build build-all test test-unit test-coverage lint fmt vet clean run dev help

# Variables
BINARY_NAME=devpack
MAIN_PATH=./cmd/devpack
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
DATE ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
LDFLAGS=-ldflags "-s -w -X github.com/user/devpack/pkg/version.Version=$(VERSION) -X github.com/user/devpack/pkg/version.Commit=$(COMMIT) -X github.com/user/devpack/pkg/version.Date=$(DATE)"

# Default target
all: lint test build

## help: Show this help message
help:
	@echo "DevPack Makefile"
	@echo ""
	@echo "Usage:"
	@sed -n 's/^##//p' $(MAKEFILE_LIST) | column -t -s ':' | sed -e 's/^/ /'

## build: Build for current platform
build:
	go build $(LDFLAGS) -o $(BINARY_NAME) $(MAIN_PATH)

## build-windows: Build for Windows (amd64)
build-windows:
	GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -o $(BINARY_NAME).exe $(MAIN_PATH)

## build-darwin: Build for macOS (amd64 + arm64)
build-darwin:
	GOOS=darwin GOARCH=amd64 go build $(LDFLAGS) -o $(BINARY_NAME)-darwin-amd64 $(MAIN_PATH)
	GOOS=darwin GOARCH=arm64 go build $(LDFLAGS) -o $(BINARY_NAME)-darwin-arm64 $(MAIN_PATH)

## build-linux: Build for Linux (amd64 + arm64)
build-linux:
	GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o $(BINARY_NAME)-linux-amd64 $(MAIN_PATH)
	GOOS=linux GOARCH=arm64 go build $(LDFLAGS) -o $(BINARY_NAME)-linux-arm64 $(MAIN_PATH)

## build-all: Build for all platforms
build-all: build-windows build-darwin build-linux

## test: Run all tests
test:
	go test -race -count=1 ./...

## test-unit: Run unit tests only
test-unit:
	go test -race -count=1 -short ./...

## test-coverage: Run tests with coverage report
test-coverage:
	go test -race -coverprofile=coverage.out -covermode=atomic ./...
	go tool cover -html=coverage.out -o coverage.html
	@echo "Coverage report: coverage.html"

## lint: Run linter
lint:
	golangci-lint run ./...

## fmt: Format code
fmt:
	go fmt ./...
	goimports -w .

## vet: Run go vet
vet:
	go vet ./...

## run: Build and run
run: build
	./$(BINARY_NAME)

## dev: Run in development mode
dev:
	go run $(MAIN_PATH)

## clean: Clean build artifacts
clean:
	rm -f $(BINARY_NAME) $(BINARY_NAME).exe
	rm -f $(BINARY_NAME)-*
	rm -f coverage.out coverage.html
	rm -rf dist/

## deps: Download dependencies
deps:
	go mod download
	go mod tidy

## snapshot: Create GoReleaser snapshot
snapshot:
	goreleaser release --snapshot --clean

## release: Create release with GoReleaser
release:
	goreleaser release --clean
