#!make
SHELL := /bin/bash
ENV := ./scripts/apply_env.sh

.PHONY: help

export VERSION=$(if $(v),$(v),latest)

help: ## Help command
	@echo
	@cat Makefile | grep -E '^[a-zA-Z\/_-]+:.*?## .*$$' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
	@echo

backend-up: ## Start the backend containers
	@$(ENV) docker compose --profile backend up -d

up-backend: backend-up

backend-down: ## Stop the backend containers
	@$(ENV) docker compose --profile backend down

down-backend: backend-down

run: ## Run the project
	@echo "Running project..."
	@$(ENV) cargo run
	@echo "Run complete"

format: ## Format code
	@echo "Formatting code..."
	@$(ENV) cargo fmt
	@echo "Format complete"

clean: ## Clean the project
	@echo "Cleaning project..."
	@$(ENV) cargo clean
	@echo "Clean complete"

docker-up: ## Start the app
	@$(ENV) docker compose --profile backend --profile bot up -d

docker-down: ## Stop the app
	@$(ENV) docker compose --profile backend --profile bot down

docker-build: ## Build the docker image
	@$(ENV) docker build -f Dockerfile -t siren:${VERSION} .

docker-clean: ## Stop the docker containers and remove volumes
	@echo "Stopping docker container and removing volumes..."
	@$(ENV) docker compose --profile backend --profile bot down -v
	@echo "Docker container stopped and volumes removed"

docker-refresh: docker-clean backend-up ## Refresh the docker containers

psql: ## Connect to the database
	@$(ENV) docker exec -it siren-postgres psql -U ${DATABASE_USER} -P pager=off

insert-api:
	@$(ENV) ./scripts/insert_api_key.sh