.PHONY: build test lint clean install run docker

# Binary name
BINARY=devour
MAIN_PATH=./cmd/devour

# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod

# Build flags
VERSION=$(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME=$(shell date -u '+%Y-%m-%d_%H:%M:%S')
LDFLAGS=-ldflags "-s -w -X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME)"

# Default target
all: clean build

## build: Build the binary
build:
	$(GOBUILD) $(LDFLAGS) -o $(BINARY) $(MAIN_PATH)

## build-prod: Build optimized production binary
build-prod:
	CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o $(BINARY) $(MAIN_PATH)

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

## test-coverage: Run tests with coverage
test-coverage:
	$(GOTEST) -coverprofile=coverage.out ./...
	$(GOCMD) tool cover -html=coverage.out -o coverage.html

## lint: Run linter
lint:
	@which golangci-lint > /dev/null || go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
	golangci-lint run ./...

## fmt: Format code
fmt:
	$(GOCMD) fmt ./...

## clean: Clean build artifacts
clean:
	$(GOCLEAN)
	rm -f $(BINARY)
	rm -f coverage.out coverage.html

## install: Install binary to GOPATH/bin
install:
	$(GOCMD) install $(MAIN_PATH)

## run: Run the application
run:
	$(GOCMD) run $(MAIN_PATH)

## deps: Download dependencies
deps:
	$(GOMOD) download
	$(GOMOD) tidy

## docker: Build Docker image
docker:
	docker build -t devour:latest .

## docker-run: Run Docker container
docker-run:
	docker run -it --rm \
		-e OPENAI_API_KEY=$(OPENAI_API_KEY) \
		-v $(PWD)/devour_data:/app/devour_data \
		devour:latest

## help: Show this help
help:
	@echo 'Usage: make [target]'
	@echo ''
	@echo 'Targets:'
	@sed -n 's/^##//p' $(MAKEFILE_LIST) | column -t -s ':'
