.PHONY: build build-release clean test run version install-hooks

# Get version from git
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S')
LDFLAGS := -s -w -X main.Version=$(VERSION)

# Binary name
BINARY := qfs

# Build for development (with debug info)
build:
	go build -o bin/$(BINARY) ./cmd/qfs

# Build for release (optimized, with version)
build-release:
	@echo "Building $(BINARY) version $(VERSION)..."
	CGO_ENABLED=0 go build -ldflags="$(LDFLAGS)" -o bin/$(BINARY) ./cmd/qfs
	@echo "✓ Built: bin/$(BINARY)"
	@./bin/$(BINARY) -version

# Build release for Linux (cross-compile)
build-linux:
	@echo "Building $(BINARY) for Linux..."
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="$(LDFLAGS)" -o bin/$(BINARY)-linux-amd64 ./cmd/qfs
	@echo "✓ Built: bin/$(BINARY)-linux-amd64"

# Build release for macOS (cross-compile)
build-macos:
	@echo "Building $(BINARY) for macOS..."
	CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="$(LDFLAGS)" -o bin/$(BINARY)-darwin-amd64 ./cmd/qfs
	CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="$(LDFLAGS)" -o bin/$(BINARY)-darwin-arm64 ./cmd/qfs
	@echo "✓ Built: bin/$(BINARY)-darwin-amd64"
	@echo "✓ Built: bin/$(BINARY)-darwin-arm64"

# Build release for Windows (cross-compile)
build-windows:
	@echo "Building $(BINARY) for Windows..."
	CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="$(LDFLAGS)" -o bin/$(BINARY)-windows-amd64.exe ./cmd/qfs
	@echo "✓ Built: bin/$(BINARY)-windows-amd64.exe"

# Build all platforms
build-all: build-release build-linux build-macos build-windows

# Create release packages for all platforms
release:
	@./scripts/release.sh

# Show version
version:
	@echo "Version: $(VERSION)"

# Clean build artifacts
clean:
	rm -rf bin/
	rm -f $(BINARY)

# Run tests
test:
	go test -v ./...

# Run development server
run:
	go run ./cmd/qfs

# Run with auto-restart (requires entr: brew install entr)
watch:
	find . -name '*.go' | entr -r go run ./cmd/qfs

# Install dependencies
deps:
	go mod download
	go mod tidy

# Install local git hooks
install-hooks:
	git config core.hooksPath .githooks
	chmod +x .githooks/pre-commit scripts/check-secrets.sh
	@echo "Installed git hooks from .githooks/"

# Help
help:
	@echo "QuoteForge Server (qfs) - Build Commands"
	@echo ""
	@echo "Usage: make [target]"
	@echo ""
	@echo "Targets:"
	@echo "  build          Build for development (with debug info)"
	@echo "  build-release  Build optimized release (default)"
	@echo "  build-linux    Cross-compile for Linux"
	@echo "  build-macos    Cross-compile for macOS (Intel + Apple Silicon)"
	@echo "  build-windows  Cross-compile for Windows"
	@echo "  build-all      Build for all platforms"
	@echo "  release        Create release packages for all platforms"
	@echo "  version        Show current version"
	@echo "  clean          Remove build artifacts"
	@echo "  test           Run tests"
	@echo "  run            Run development server"
	@echo "  watch          Run with auto-restart (requires entr)"
	@echo "  deps           Install/update dependencies"
	@echo "  install-hooks  Install local git hooks (secret scan on commit)"
	@echo "  help           Show this help"
	@echo ""
	@echo "Current version: $(VERSION)"
