# mycoolmusicapp — dev workflow shortcuts.
# `make` or `make help` lists targets.

COMPOSE := docker compose

# Force BuildKit so `RUN --mount=type=cache` works everywhere (older/Linux
# Docker engines otherwise fall back to the legacy builder and fail the build).
export DOCKER_BUILDKIT := 1
export COMPOSE_DOCKER_CLI_BUILD := 1

.DEFAULT_GOAL := help
.PHONY: help env up build rebuild down stop restart ps logs logs-api logs-webui \
        sh-api sh-webui db-shell redis-cli migrate makemigration downgrade \
        test test-api test-webui lint fmt clean prune \
        prod-build prod-build-api prod-build-webui

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

env: ## Create .env from .env.example (no overwrite)
	@test -f .env || cp .env.example .env && echo ".env ready"

## ---- lifecycle -------------------------------------------------------
up: env ## Build (if needed) + start the full dev stack
	$(COMPOSE) up --build

build: ## Build all images
	$(COMPOSE) build

rebuild: ## Rebuild images with no cache
	$(COMPOSE) build --no-cache

down: ## Stop and remove containers
	$(COMPOSE) down

stop: ## Stop containers (keep them)
	$(COMPOSE) stop

restart: ## Restart all services
	$(COMPOSE) restart

ps: ## Show container status
	$(COMPOSE) ps

## ---- logs ------------------------------------------------------------
logs: ## Tail logs (all services)
	$(COMPOSE) logs -f --tail=100

logs-api: ## Tail api logs
	$(COMPOSE) logs -f --tail=100 api

logs-webui: ## Tail webui logs
	$(COMPOSE) logs -f --tail=100 webui

## ---- shells ----------------------------------------------------------
sh-api: ## Shell into the api container
	$(COMPOSE) exec api bash

sh-webui: ## Shell into the webui container
	$(COMPOSE) exec webui sh

db-shell: ## psql into the database
	$(COMPOSE) exec db psql -U $${POSTGRES_USER:-mcma} -d $${POSTGRES_DB:-mcma}

redis-cli: ## redis-cli into redis
	$(COMPOSE) exec redis redis-cli

## ---- database migrations (alembic) -----------------------------------
migrate: ## Apply latest migrations
	$(COMPOSE) exec api alembic upgrade head

makemigration: ## Autogenerate a migration: make makemigration m="msg"
	$(COMPOSE) exec api alembic revision --autogenerate -m "$(m)"

downgrade: ## Roll back one migration
	$(COMPOSE) exec api alembic downgrade -1

## ---- quality ---------------------------------------------------------
test: test-api test-webui ## Run all tests

test-api: ## Run backend tests
	$(COMPOSE) exec api pytest

test-webui: ## Run frontend tests
	$(COMPOSE) exec webui npm test

lint: ## Lint backend + frontend
	$(COMPOSE) exec api ruff check .
	$(COMPOSE) exec webui npm run lint

fmt: ## Format backend + frontend
	$(COMPOSE) exec api ruff format .
	$(COMPOSE) exec webui npm run format

## ---- cleanup ---------------------------------------------------------
clean: ## Stop + remove containers AND volumes (DESTROYS db data)
	$(COMPOSE) down -v

prune: ## Docker system prune (dangling images/build cache)
	docker system prune -f

## ---- production image builds (no compose yet) ------------------------
prod-build: prod-build-api prod-build-webui ## Build both prod images

prod-build-api: ## Build the backend prod image
	docker build -f mcma-backend/dockerfiles/Dockerfile.prod -t mcma-backend:prod ./mcma-backend

prod-build-webui: ## Build the webui prod image
	docker build -f mcma-webui/dockerfiles/Dockerfile.prod -t mcma-webui:prod ./mcma-webui
