# Makefile for the Hermes Crawler Application

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


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

# --- Crawler Configuration ---
BINARY_NAME=crawler
IMAGE_NAME=hermes-crawler
IMAGE_URL=$(GCR_HOSTNAME)/$(GCP_PROJECT_ID)/$(IMAGE_NAME):$(IMAGE_TAG)

# --- Bot Configuration ---
BOT_BINARY_NAME=bot
BOT_IMAGE_NAME=hermes-bot
BOT_IMAGE_URL=$(GCR_HOSTNAME)/$(GCP_PROJECT_ID)/$(BOT_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"

# =============================
# BOT SPECIFIC CONFIG
# =============================
.PHONY: bot-build bot-run bot-clean bot-docker-build bot-docker-push bot-deploy

bot-build: ## 🛠️ Build the Go binary for the webhook bot
	@echo "--> Building Go bot binary..."
	@go build -o $(BOT_BINARY_NAME) ./cmd/bot

bot-run: bot-build ## 🚀 Run the bot locally
	@echo "--> Running bot locally..."
	@./$(BOT_BINARY_NAME)

bot-clean: ## 🗑️ Clean bot binary
	@echo "--> Cleaning bot binary..."
	@rm -f $(BOT_BINARY_NAME)

bot-docker-build: ## 🐳 Build the bot Docker image
	@echo "--> Building Docker image for bot: $(BOT_IMAGE_URL)"
	@docker build -f Dockerfile.bot -t $(BOT_IMAGE_URL) .

bot-docker-push: bot-docker-build ## ⬆️ Push the bot image
	@echo "--> Pushing bot image: $(BOT_IMAGE_URL)"
	@docker push $(BOT_IMAGE_URL)

bot-deploy: bot-docker-push ## ☁️ Deploy the bot to Cloud Run
	@echo "--> Deploying bot to Cloud Run..."
	@gcloud run deploy $(BOT_IMAGE_NAME) \
		--image=$(BOT_IMAGE_URL) \
		--region=us-central1 \
		--platform=managed \
		--allow-unauthenticated \
		--cpu=1 \
		--memory=512Mi \
		--port=8080