.PHONY: build run test test-short test-coverage clean fmt vet lint install tidy deps verify release help

# Binary name
BINARY_NAME=hostkeeper
BUILD_DIR=bin

# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOFMT=$(GOCMD) fmt
GOVET=$(GOCMD) vet

# Version info
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
LDFLAGS := -X main.version=$(VERSION) -X main.buildTime=$(BUILD_TIME)

# Default target
all: build

## build: Compile the binary
build:
	$(GOBUILD) -ldflags "$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/hostkeeper

## run: Build and run
run: build
	./$(BUILD_DIR)/$(BINARY_NAME) $(ARGS)

## test: Run all tests
test:
	$(GOTEST) ./... -v

## test-short: Run only short tests
test-short:
	$(GOTEST) ./... -v -short

## test-coverage: Run tests with coverage report
test-coverage:
	$(GOTEST) ./... -v -coverprofile=coverage.out -covermode=atomic
	$(GOCMD) tool cover -html=coverage.out -o coverage.html
	@echo "Coverage report generated: coverage.html"

## clean: Remove build artifacts
clean:
	$(GOCLEAN)
	rm -rf $(BUILD_DIR)
	rm -f coverage.out coverage.html

## fmt: Format all Go files
fmt:
	$(GOFMT) ./...

## vet: Run go vet
vet:
	$(GOVET) ./...

## lint: Run linter (requires golangci-lint)
lint:
	golangci-lint run ./...

## install: Install the binary to GOPATH/bin
install: build
	$(GOCMD) install ./cmd/hostkeeper

## tidy: Run go mod tidy
tidy:
	$(GOCMD) mod tidy

## deps: Download dependencies
deps:
	$(GOCMD) mod download

## verify: Verify module dependencies
verify:
	$(GOCMD) mod verify

## release: Build for multiple platforms
release: clean
	mkdir -p $(BUILD_DIR)
	GOOS=darwin GOARCH=amd64 $(GOBUILD) -ldflags "$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-amd64 ./cmd/hostkeeper
	GOOS=darwin GOARCH=arm64 $(GOBUILD) -ldflags "$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-arm64 ./cmd/hostkeeper
	GOOS=linux GOARCH=amd64 $(GOBUILD) -ldflags "$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_NAME)-linux-amd64 ./cmd/hostkeeper
	GOOS=linux GOARCH=arm64 $(GOBUILD) -ldflags "$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_NAME)-linux-arm64 ./cmd/hostkeeper
	GOOS=windows GOARCH=amd64 $(GOBUILD) -ldflags "$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_NAME)-windows-amd64.exe ./cmd/hostkeeper

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