# Pipeline targets for Crussell backend
# Use: make <target>        (runs with default tags: test,dev)
#      make TAGS="test,!dev" <target>

TAGS ?= test,dev
RACE_FLAGS ?= -race -timeout 480s

.PHONY: test test-all test-race vet lint ci help

# --- Test matrix ---

test:  ## Run tests with default build tags (test,dev)
	go test -tags "$(TAGS)" -count=1 -parallel 8 ./...

test-prod:  ## Run tests with production build tags (test,!dev)
	go test -tags "test,!dev" -count=1 -parallel 8 ./...

test-dev:  ## Run tests with dev build tags (test,dev)
	go test -tags "test,dev" -count=1 -parallel 8 ./...

test-all: test-dev test-prod  ## Run tests under both dev and prod build tags

test-race:  ## Run tests with race detector enabled
	go test -tags "$(TAGS)" $(RACE_FLAGS) -count=1 ./...

test-count:  ## Run each test 10 times to detect flakiness
	go test -tags "$(TAGS)" -count=10 -parallel 8 ./...

# --- Static analysis ---

vet:  ## go vet under current build tags
	go vet -tags "$(TAGS)" ./...

vet-all:  ## go vet under both dev and prod build tags
	@echo "=== vet: dev ==="
	go vet -tags "test,dev" ./...
	@echo "=== vet: prod ==="
	go vet -tags "test,!dev" ./...

lint:  ## staticcheck under current build tags
	staticcheck -tags "$(TAGS)" ./...

lint-all:  ## staticcheck under both build tags
	@echo "=== staticcheck: dev ==="
	staticcheck -tags "test,dev" ./...
	@echo "=== staticcheck: prod ==="
	staticcheck -tags "test,!dev" ./...

# --- Combined ---

ci: test-all vet-all lint-all  ## Full CI pipeline: test + vet + lint under both tag sets

ci-race: test-race vet lint  ## CI with race detection

# --- Utilities ---

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