# ─────────────────────────────────────────────────────────────────────────────
#  PMDI — Pay My Damn Invoice  |  Developer Makefile
# ─────────────────────────────────────────────────────────────────────────────
# Usage:
#   make up         — start the full Docker stack
#   make down       — stop and remove containers
#   make logs       — tail all container logs
#   make cron       — trigger the billing scan manually
#   make migrate    — run pending Alembic migrations inside the backend container
#   make autogen    — generate a new migration from model changes
#   make shell-be   — open a bash shell inside the backend container
#   make shell-db   — open psql inside the db container
#
# Requires:  docker compose, curl, jq
# ─────────────────────────────────────────────────────────────────────────────

.PHONY: up down restart logs cron migrate autogen shell-be shell-db help

COMPOSE  := docker compose
BACKEND  := pmdi_backend
DB       := pmdi_db
API_BASE := http://localhost:8000/api

# Load CRON_SECRET_TOKEN from .env for the manual cron trigger
-include .env
export

# ── Stack management ───────────────────────────────────────────────────────────

up:
	$(COMPOSE) up -d --build

down:
	$(COMPOSE) down

restart:
	$(COMPOSE) restart

logs:
	$(COMPOSE) logs -f --tail=100

# ── Cron manual trigger ────────────────────────────────────────────────────────
#  Sends a POST to /api/cron/run with the CRON_SECRET_TOKEN from .env
#  Pipe through jq if available, fall back to plain output.
cron:
	@echo "▶  Triggering billing scan…"
	@curl -s -X POST $(API_BASE)/cron/run \
	  -H "Authorization: Bearer $(CRON_SECRET_TOKEN)" \
	  -H "Content-Type: application/json" \
	| (command -v jq > /dev/null 2>&1 && jq . || cat)
	@echo ""

# ── Database migrations ────────────────────────────────────────────────────────
migrate:
	@echo "▶  Running Alembic migrations…"
	$(COMPOSE) exec $(BACKEND) alembic upgrade head

autogen:
	@echo "▶  Auto-generating migration from model changes…"
	$(COMPOSE) exec $(BACKEND) alembic revision --autogenerate -m "$(MSG)"

# ── Shells ─────────────────────────────────────────────────────────────────────
shell-be:
	$(COMPOSE) exec $(BACKEND) bash

shell-db:
	$(COMPOSE) exec $(DB) psql -U $${POSTGRES_USER:-pmdi} -d $${POSTGRES_DB:-pmdi}

# ── Help ───────────────────────────────────────────────────────────────────────
help:
	@echo ""
	@echo "  PMDI Developer Makefile"
	@echo "  ──────────────────────────────────────────────────"
	@echo "  make up          Start stack (build if needed)"
	@echo "  make down        Stop and remove containers"
	@echo "  make restart     Restart all services"
	@echo "  make logs        Tail logs from all containers"
	@echo "  make cron        Manually trigger billing scan"
	@echo "  make migrate     Apply pending DB migrations"
	@echo "  make autogen MSG='describe change'  Generate migration"
	@echo "  make shell-be    Shell into backend container"
	@echo "  make shell-db    psql into PostgreSQL container"
	@echo ""
