.PHONY: help build dev test clean install lint fmt vet

BINARY_NAME=wild-central
BUILD_DIR=build

GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOMOD=$(GOCMD) mod
GOFMT=$(GOCMD) fmt
GOVET=$(GOCMD) vet

LDFLAGS=-ldflags "-s -w"

help: ## Show this help message
	@echo 'Usage: make [target]'
	@echo ''
	@echo 'Available targets:'
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "  %-15s %s\n", $$1, $$2}' $(MAKEFILE_LIST)

build: ## Build the daemon binary
	@echo "Building $(BINARY_NAME)..."
	@mkdir -p $(BUILD_DIR)
	$(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) .

dev: ## Run in development mode with live reloading
	@if command -v air >/dev/null 2>&1; then \
		echo "Starting $(BINARY_NAME) in development mode with live reloading (air)..."; \
		air; \
	else \
		echo "air not found. Install it for live reloading: go install github.com/air-verse/air@latest"; \
		echo "Starting $(BINARY_NAME) in development mode without live reloading..."; \
		$(GOCMD) run .; \
	fi

test: ## Run tests
	@echo "Running tests..."
	$(GOTEST) -v ./...

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

clean: ## Clean build artifacts
	@echo "Cleaning..."
	$(GOCLEAN)
	@rm -rf $(BUILD_DIR)
	@rm -f coverage.out coverage.html

install: build ## Install the binary
	@echo "Installing $(BINARY_NAME)..."
	@mkdir -p $$($(GOCMD) env GOPATH)/bin
	@cp $(BUILD_DIR)/$(BINARY_NAME) $$($(GOCMD) env GOPATH)/bin/

deps: ## Download dependencies
	@echo "Downloading dependencies..."
	$(GOMOD) download
	$(GOMOD) tidy

fmt: ## Format Go code
	@echo "Formatting code..."
	$(GOFMT) ./...

vet: ## Run go vet
	@echo "Running go vet..."
	$(GOVET) ./...

lint: fmt vet ## Run formatters and linters

check: lint test ## Run all checks (lint + test)

all: clean lint test build ## Clean, lint, test, and build
