# ── repertory-api Makefile ────────────────────────────────────────────────────
#
# make local   — first-time (or repeated) local dev setup:
#                  copies env.example → .env if missing,
#                  starts Postgres via Docker Compose,
#                  waits for it to be ready,
#                  runs Alembic migrations,
#                  starts the API container.
#
# make stop    — stop all Docker Compose services (data is preserved).
# make reset   — stop services AND wipe the Postgres volume (fresh slate).
# make migrate — run pending Alembic migrations against the local DB.
# make test    — run the pytest suite (SQLite in-memory, no Docker needed).
# make logs    — tail Docker Compose logs.
# make shell   — open a psql shell in the running Postgres container.
# ─────────────────────────────────────────────────────────────────────────────

COMPOSE  = docker compose -f docker/docker-compose.yml --env-file .env
ALEMBIC  = PYTHONPATH=. uv run alembic
PYTEST   = PYTHONPATH=. uv run pytest
PY       = uv run python

DB_HOST  = localhost
DB_USER  = repertory
DB_NAME  = tunes

.PHONY: help local stop reset migrate test logs shell sync

# Default target
help:
	@echo ""
	@echo "  make local    — set up and start local dev environment"
	@echo "  make stop     — stop Docker services"
	@echo "  make reset    — stop services and wipe database volume"
	@echo "  make migrate  — run pending Alembic migrations"
	@echo "  make test     — run test suite (no Docker required)"
	@echo "  make logs     — tail Docker Compose logs"
	@echo "  make shell    — open a psql shell in the Postgres container"
	@echo ""

# ── Sync dependencies ─────────────────────────────────────────────────────────

sync:
	uv sync

# ── Local dev ─────────────────────────────────────────────────────────────────

local: sync _copy_env _db_up _wait_for_db migrate _api_up
	@echo ""
	@echo "  ✅  Local environment is ready."
	@echo ""
	@set -a; . ./.env; set +a; \
	echo "  API:      http://localhost:$${API_PORT:-5000}"
	@echo "  API key:  $$(grep '^API_KEY=' .env | cut -d= -f2)"
	@echo ""
	@echo "  Run 'make logs' to tail the logs."
	@echo "  Run 'make stop' to stop services."
	@echo ""

_copy_env:
	@if [ ! -f .env ]; then \
		cp env.example .env; \
		echo "  📋  Copied env.example → .env"; \
		echo "      API key is set to: $$(grep '^API_KEY=' .env | cut -d= -f2)"; \
		echo "      Edit .env to change it before deploying to production."; \
	else \
		echo "  ✔   .env already exists — skipping copy."; \
	fi

_db_up:
	@echo "  🐳  Starting Postgres…"
	$(COMPOSE) up -d db

_wait_for_db:
	@echo "  ⏳  Waiting for Postgres to be ready…"
	@set -a; . ./.env; set +a; \
	for i in $$(seq 1 30); do \
		if pg_isready -U $(DB_USER) -d $(DB_NAME) -h $(DB_HOST) -p $${DB_PORT:-5432} \
			> /dev/null 2>&1; then \
			echo "  ✔   Postgres is ready."; \
			break; \
		fi; \
		if [ $$i -eq 30 ]; then \
			echo "  ❌  Timed out waiting for Postgres."; exit 1; \
		fi; \
		sleep 1; \
	done

_api_up:
	@echo "  🐳  Starting API container…"
	$(COMPOSE) up -d --build api

# ── Migrations ────────────────────────────────────────────────────────────────

migrate: sync
	@echo "  🗄   Running Alembic migrations…"
	@set -a; . ./.env; set +a; $(ALEMBIC) upgrade head
	@echo "  ✔   Migrations complete."

# ── Tests ─────────────────────────────────────────────────────────────────────
# Uses SQLite in-memory — no Docker or network required.

test: sync
	@echo "  🧪  Running tests…"
	$(PYTEST) tests/ -v

# ── Utilities ─────────────────────────────────────────────────────────────────

stop:
	$(COMPOSE) stop

reset:
	@echo "  ⚠️   Stopping services and removing database volume…"
	$(COMPOSE) down -v
	@echo "  ✔   Done. Run 'make local' to start fresh."

logs:
	$(COMPOSE) logs -f

shell:
	$(COMPOSE) exec db psql -U $(DB_USER) -d $(DB_NAME)
