BINARY_NAME    = webmirror
WINDOWS_BINARY = $(BINARY_NAME)-windows-amd64.exe
LINUX_BINARY   = $(BINARY_NAME)-linux-amd64
VERSION       := $(shell cat VERSION)
LDFLAGS        = -s -w -X webmirror/internal/version.ver=$(VERSION)
GOFLAGS        = -trimpath
TARBALL_ROOT   = $(BINARY_NAME)-$(VERSION)
TARBALL_NAME   = $(TARBALL_ROOT).tar.gz
TARBALL_CONTENTS = \
	Makefile \
	VERSION \
	LICENSE \
	README.md \
	demo.svg \
	go.mod \
	go.sum \
	cmd \
	internal \
	examples

.PHONY: all build build-linux build-windows release install test test-race bench vet fmt lint check clean tar help

## Default: build for local architecture
all: build

## Build for local architecture
build:
	@echo "Building $(BINARY_NAME) v$(VERSION)..."
	CGO_ENABLED=0 go build $(GOFLAGS) -ldflags '$(LDFLAGS)' -o $(BINARY_NAME) ./cmd/webmirror

## Build for Linux amd64
build-linux:
	@echo "Building $(LINUX_BINARY) v$(VERSION)..."
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build $(GOFLAGS) -ldflags '$(LDFLAGS)' -o $(LINUX_BINARY) ./cmd/webmirror

## Build for Windows amd64
build-windows:
	@echo "Building $(WINDOWS_BINARY) v$(VERSION)..."
	CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build $(GOFLAGS) -ldflags '$(LDFLAGS)' -o $(WINDOWS_BINARY) ./cmd/webmirror

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

## Install to GOPATH/bin
install:
	@echo "Installing $(BINARY_NAME) v$(VERSION)..."
	CGO_ENABLED=0 go install $(GOFLAGS) -ldflags '$(LDFLAGS)' ./cmd/webmirror

## Run tests
test:
	go test ./... -timeout 120s

## Run tests with race detector
test-race:
	go test -race ./... -timeout 120s

## Run benchmarks
bench:
	go test -bench=. -benchmem ./...

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

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

## Run staticcheck (install: go install honnef.co/go/tools/cmd/staticcheck@latest)
lint:
	@command -v staticcheck >/dev/null 2>&1 || { echo "staticcheck not installed; run: go install honnef.co/go/tools/cmd/staticcheck@latest"; exit 1; }
	staticcheck ./...

## Run all checks (fmt, vet, lint, test)
check: fmt vet lint test

## Create release tarball
tar:
	@echo "Creating $(TARBALL_NAME)..."
	tar -czf $(TARBALL_NAME) \
		--transform 's,^,$(TARBALL_ROOT)/,' \
		$(TARBALL_CONTENTS)

## Remove build artifacts
clean:
	@echo "Cleaning..."
	go clean
	rm -f $(BINARY_NAME) $(LINUX_BINARY) $(WINDOWS_BINARY) $(TARBALL_NAME)

## Show available targets
help:
	@echo "$(BINARY_NAME) v$(VERSION) - Makefile targets:"
	@echo ""
	@echo "  all            Build for local architecture (default)"
	@echo "  build          Build local binary"
	@echo "  build-linux    Cross-compile for Linux amd64"
	@echo "  build-windows  Cross-compile for Windows amd64"
	@echo "  release        Build all platforms"
	@echo "  install        Install to GOPATH/bin"
	@echo "  test           Run tests"
	@echo "  test-race      Run tests with race detector"
	@echo "  bench          Run benchmarks"
	@echo "  vet            Run go vet"
	@echo "  fmt            Format code with gofmt"
	@echo "  lint           Run staticcheck"
	@echo "  check          Run fmt + vet + lint + test"
	@echo "  tar            Create release tarball"
	@echo "  clean          Remove build artifacts"
	@echo "  help           Show this help"
