.PHONY: all build test test-race test-integration test-e2e test-all test-cover lint security fmt clean docker docker-run setup dev changelog release rd-log sbom help

# Variables
BINARY    := bridge
CMD_DIR   := ./cmd/bridge
VERSION   ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
COMMIT    ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "none")
BUILD     ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
LDFLAGS   := -s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.buildTime=$(BUILD)
COVERAGE  := 60

# Developer Experience
setup:                        ## Install dev tools and git hooks
	go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
	go install github.com/securego/gosec/v2/cmd/gosec@latest
	go install golang.org/x/tools/cmd/goimports@latest
	brew install lefthook gitleaks git-cliff 2>/dev/null || true
	lefthook install

dev: build                    ## Build and run locally
	./$(BINARY)

# Core
build:                        ## Build binary with version info
	CGO_ENABLED=0 go build -ldflags="$(LDFLAGS)" -o $(BINARY) $(CMD_DIR)

clean:                        ## Remove build artifacts
	rm -f $(BINARY) coverage.out coverage.html cover.out

# Testing
test:                         ## Run unit tests
	CGO_ENABLED=0 go test -short -count=1 ./...

test-race:                    ## Run tests with race detector
	CGO_ENABLED=0 go test -race -short -count=10 ./...

test-integration:             ## Run integration tests
	CGO_ENABLED=0 go test -tags=integration -count=1 -timeout=60s ./...

test-e2e:                     ## Run end-to-end tests
	CGO_ENABLED=0 go test -tags=e2e -count=1 -timeout=120s ./...

test-all:                     ## Run all tests with race detector
	CGO_ENABLED=0 go test -race -tags="integration e2e" -count=1 -timeout=120s ./...

test-cover:                   ## Run tests with coverage report and threshold check
	CGO_ENABLED=0 go test -coverprofile=coverage.out ./...
	@TOTAL=$$(go tool cover -func=coverage.out | grep total | awk '{print $$3}' | tr -d '%'); \
	echo "Total coverage: $$TOTAL%"; \
	if [ $$(echo "$$TOTAL < $(COVERAGE)" | bc) -eq 1 ]; then \
		echo "FAIL: Coverage $$TOTAL% < $(COVERAGE)% threshold"; exit 1; \
	fi
	go tool cover -html=coverage.out -o coverage.html
	@echo "Coverage report: coverage.html"

test-leak:                    ## Run goroutine leak tests
	CGO_ENABLED=0 go test -race -run TestNoGoroutineLeak ./...

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

security:                     ## Run security scans
	gosec ./...
	gitleaks detect --no-banner

fmt:                          ## Format code
	gofmt -w .
	goimports -w .

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

# Docker
docker:                       ## Build Docker image
	docker build -t p2ptb-bridge:$(VERSION) \
		--build-arg VERSION=$(VERSION) \
		--build-arg COMMIT=$(COMMIT) .

docker-run: docker            ## Build and run in Docker
	docker run -p 8766:8766 \
		-e P2PTB_ADMIN_TOKEN=dev-token \
		p2ptb-bridge:$(VERSION)

# Release
changelog:                    ## Generate changelog
	git-cliff -o CHANGELOG.md

release: test-all lint security  ## Full release validation
	@echo "All checks passed. Tag with:"
	@echo "  git tag -s -a v0.x.y -m 'Release v0.x.y: summary'"
	@echo "  git push origin main --follow-tags"

sbom:                         ## Generate Software Bill of Materials
	go version -m $(BINARY) > SBOM.txt
	@echo "SBOM written to SBOM.txt"

# R&D
rd-log:                       ## Generate weekly R&D activity log
	@echo "\n## R&D Activity — $$(date -u +%Y-%m-%d)" >> docs/RD_LOG.md
	@git log --since="1 week ago" --pretty=format:"- %ad %s (%h)" --date=short >> docs/RD_LOG.md
	@echo "" >> docs/RD_LOG.md
	@echo "R&D log updated: docs/RD_LOG.md"

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

.DEFAULT_GOAL := help
