# Makefile for the Hermes Crawler Application

# ====================================================================================
# VARIABLES
# ====================================================================================

# --- Application Configuration ---
BINARY_NAME=crawler

# --- Docker & GCP Configuration ---
# IMPORTANT: Replace with your actual GCP Project ID.
GCP_PROJECT_ID=
GCR_HOSTNAME=gcr.io
IMAGE_NAME=hermes-crawler
IMAGE_TAG=latest

# This variable constructs the full image URL.
IMAGE_URL=$(GCR_HOSTNAME)/$(GCP_PROJECT_ID)/$(IMAGE_NAME):$(IMAGE_TAG)

# This allows passing arguments to the `run` command, e.g., `make run ARGS="-debug"`
ARGS=""


# ====================================================================================
# COMMANDS
# ====================================================================================

# Set the default goal to 'help' so that running 'make' by itself shows the help menu.
.DEFAULT_GOAL := help

.PHONY: help build run test tidy lint clean docker-build docker-push deploy

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

build: ## 🛠️  Build the Go binary
	@echo "--> Building Go binary..."
	@go build -o $(BINARY_NAME) ./cmd/crawler

run: build ## 🚀 Run the application locally (use ARGS="-debug" for debug mode)
	@echo "--> Running application locally..."
	@./$(BINARY_NAME) $(ARGS)

test: ## 🧪 Run all tests in verbose mode
	@echo "--> Running tests..."
	@go test -v ./...

tidy: ## 🧹 Tidy Go module dependencies
	@echo "--> Tidying go.mod..."
	@go mod tidy

lint: ## 🔍 Run the linter (requires golangci-lint)
	@echo "--> Running linter..."
	@golangci-lint run

clean: ## 🗑️  Remove the compiled binary
	@echo "--> Cleaning up..."
	@rm -f $(BINARY_NAME)

docker-build: ## 🐳 Build the Docker image
	@echo "--> Building Docker image: $(IMAGE_URL)"
	@docker build -t $(IMAGE_URL) .

docker-push: docker-build ## ⬆️  Push the Docker image to Google Container Registry
	@echo "--> Pushing Docker image: $(IMAGE_URL)"
	@docker push $(IMAGE_URL)

deploy: docker-push ## ☁️  Deploy the application as a Cloud Run Job
	@echo "--> Deploying to Cloud Run Jobs in project $(GCP_PROJECT_ID)..."
	@gcloud beta run jobs deploy ${IMAGE_NAME} \
		--image=${IMAGE_URL} \
		--region="us-central1" \
		--cpu="1" \
		--memory="512Mi" \
		--task-timeout="5m"