# Copyright The Linux Foundation and each contributor to LFX.
# SPDX-License-Identifier: MIT

.PHONY: all build clean check fmt vet lint test test-coverage run help deps install-tools docker-build ko-build megalinter

# Build variables
BINARY_NAME=lfx-mcp-server
CMD_DIR=./cmd/lfx-mcp-server
BUILD_DIR=./bin
GO_FILES=$(shell find . -name "*.go" -type f)

# Version string: clean tag on a tagged commit, tag+offset+hash between tags,
# with a -dirty suffix if there are uncommitted changes.
VERSION := $(shell git describe --tags --dirty --always 2>/dev/null || echo "dev")

# Build flags
LDFLAGS=-ldflags="-s -w -X main.Version=$(VERSION)"

# Docker/ko variables
DOCKER_IMAGE=linuxfoundation/lfx-mcp/lfx-mcp-server
DOCKER_TAG=local

# Default target
all: clean check build

# Build the binary
build: $(BUILD_DIR)/$(BINARY_NAME)

$(BUILD_DIR)/$(BINARY_NAME): $(GO_FILES)
	@echo "Building $(BINARY_NAME)..."
	@mkdir -p $(BUILD_DIR)
	go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) $(CMD_DIR)

# Clean build artifacts
clean:
	@echo "Cleaning build artifacts..."
	@rm -rf $(BUILD_DIR)

# Run all checks
check: fmt vet lint revive

# Format Go code
fmt:
	@echo "Formatting Go code..."
	go fmt ./...

# Run go vet
vet:
	@echo "Running go vet..."
	go vet ./...

# Run golangci-lint (if available)
lint:
	@echo "Running linters..."
	@if command -v golangci-lint >/dev/null 2>&1; then \
		golangci-lint run; \
	else \
		echo "golangci-lint not installed, skipping..."; \
	fi

# Run revive (if available)
revive:
	@echo "Running revive..."
	@if command -v revive >/dev/null 2>&1; then \
		revive ./...; \
	else \
		echo "revive not installed, skipping..."; \
	fi

# Run tests
test:
	@echo "Running tests..."
	go test -v ./...

# Run tests with coverage
test-coverage:
	@echo "Running tests with coverage..."
	go test -v -coverprofile=coverage.out ./...
	go tool cover -html=coverage.out -o coverage.html

# Run the server in stdio mode
run: build
	@echo "Starting LFX MCP Server..."
	$(BUILD_DIR)/$(BINARY_NAME) stdio

# Download dependencies
deps:
	@echo "Downloading dependencies..."
	go mod download
	go mod tidy

# Install development tools
install-tools:
	@echo "Installing development tools..."
	go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

# Build Docker image
docker-build:
	@echo "Building Docker image..."
	docker build --build-arg VERSION=$(VERSION) -t $(DOCKER_IMAGE):$(DOCKER_TAG) -f Dockerfile .
	@echo "Docker image built: $(DOCKER_IMAGE):$(DOCKER_TAG)"

# Build ko image locally: loads into local Docker daemon with a :local tag, matching docker-build.
# KO_DOCKER_REPO is the parent path; ko appends the binary name to produce the full image name.
# VERSION is exported so the .ko.yaml {{.Env.VERSION}} template resolves correctly.
ko-build:
	@echo "Building ko image..."
	KO_DOCKER_REPO=$(DOCKER_IMAGE) VERSION=$(VERSION) ko build -L --bare --tags local ./cmd/lfx-mcp-server

# Run MegaLinter locally via Docker (matches CI Go flavor at v9).
megalinter:
	docker pull oxsecurity/megalinter-go:v9
	docker run --rm --platform linux/amd64 -v '$(CURDIR):/tmp/lint:rw' oxsecurity/megalinter-go:v9

# Show help
help:
	@echo "Available targets:"
	@echo "  all            - Clean, check, and build (default)"
	@echo "  build          - Build the binary"
	@echo "  clean          - Clean build artifacts"
	@echo "  check          - Run all code quality checks"
	@echo "  fmt            - Format Go code"
	@echo "  vet            - Run go vet"
	@echo "  lint           - Run golangci-lint"
	@echo "  test           - Run tests"
	@echo "  test-coverage  - Run tests with coverage report"
	@echo "  run            - Build and run the server"
	@echo "  deps           - Download and tidy dependencies"
	@echo "  install-tools  - Install development tools"
	@echo "  docker-build   - Build Docker image"
	@echo "  ko-build       - Build ko image locally with :local tag"
	@echo "  megalinter     - Run MegaLinter locally via Docker"
	@echo "  help           - Show this help message"
