.PHONY: build build-release clean test run version install-hooks migrate migrate-status migrate-baseline security-check

# 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: security-check
	@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: security-check
	@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: security-check
	@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: security-check
	@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)

# Server (MariaDB) migrations — migrations/*.sql tracked in qt_schema_migrations.
# Always built from the current source (go run), never a possibly stale bin/ binary.
# Uses the connection saved in the local app settings.

# List migrations with applied/pending state (read-only)
migrate-status:
	go run ./cmd/qfs -migrate-status

# Apply pending migrations
migrate:
	go run ./cmd/qfs -migrate

# Record migrations up to UPTO as applied WITHOUT running them (one-time, for a server
# whose schema is already current): make migrate-baseline UPTO=033_add_price_quality_to_pricelist_items.sql
migrate-baseline:
	@test -n "$(UPTO)" || (echo "usage: make migrate-baseline UPTO=<file.sql>"; exit 1)
	go run ./cmd/qfs -migrate-baseline "$(UPTO)"

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

# Pin the scanner so releases do not depend on a locally installed executable.
security-check:
	go run golang.org/x/vuln/cmd/govulncheck@v1.9.0 ./...

# 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)"
