.PHONY: build run test lint clean migrate dev prod deps fmt deb release

# Binary name
BINARY=llamalink
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 "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME)"

# Directories
BUILD_DIR=./bin
DIST_DIR=./dist
FRONTEND_DIR=./web/frontend
DEB_ARCH=$(shell dpkg --print-architecture 2>/dev/null || echo "amd64")

# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build $(LDFLAGS)
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod
GOFMT=gofmt
GOLINT=golangci-lint

# Default target
all: deps build

## build: Build the binary
build:
	@echo "Building $(BINARY)..."
	@mkdir -p $(BUILD_DIR)
	$(GOBUILD) -o $(BUILD_DIR)/$(BINARY) ./cmd/llamalink

## run: Build and run
run: build
	@echo "Running..."
	$(BUILD_DIR)/$(BINARY)

## dev: Run in development mode
dev:
	LLAMALINK_ENV=development $(GOCMD) run ./cmd/llamalink

## test: Run tests
test:
	$(GOTEST) -v -race -coverprofile=coverage.out ./cmd/... ./internal/...

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

## lint: Run linters
lint:
	$(GOLINT) run ./...

## fmt: Format code
fmt:
	$(GOFMT) -s -w .

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

## clean: Remove build artifacts
clean:
	rm -rf $(BUILD_DIR) $(DIST_DIR)
	rm -f coverage.out
	rm -f *.db

## migrate/up: Run database migrations up
migrate/up:
	migrate -path internal/db/migrations -database "$(DATABASE_URL)" up

## migrate/down: Run database migrations down
migrate/down:
	migrate -path internal/db/migrations -database "$(DATABASE_URL)" down

## migrate/create: Create a new migration (Usage: make migrate/create NAME=add_users_table)
migrate/create:
	@if [ -z "$(NAME)" ]; then echo "Usage: make migrate/create NAME=add_users_table"; exit 1; fi
	migrate create -path internal/db/migrations -ext .sql -dir internal/db/migrations $(NAME)

## frontend/install: Install frontend dependencies
frontend/install:
	cd $(FRONTEND_DIR) && npm install

## frontend/dev: Run frontend dev server
frontend/dev:
	cd $(FRONTEND_DIR) && npm run dev

## frontend/build: Build frontend for production
frontend/build:
	cd $(FRONTEND_DIR) && npm run build

## docker/build: Build Docker image
docker/build:
	docker build -t llamalink:latest -f deploy/Dockerfile .

## docker/run: Run Docker container
docker/run:
	docker-compose -f deploy/docker-compose.yml up

## docker/build/run: Build and run with docker-compose
docker/build/run: docker/build docker/run

## deb: Build .deb package (requires dpkg-deb, Linux only)
deb: build
	@echo "Building .deb package..."
	@./scripts/build-deb.sh $(VERSION) $(DEB_ARCH)

## release: Build all release artifacts locally
release:
	@echo "Building release artifacts for $(VERSION)..."
	@mkdir -p $(DIST_DIR)
	$(MAKE) build
	$(MAKE) deb
	@echo "Release artifacts in $(DIST_DIR)/"

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