# mohfarawati.de — task runner.
# Local dev  = DB in Docker, app on host (`npm run dev`).
# Production = docker-compose.yml runs the full stack on the server.

.DEFAULT_GOAL := help
.PHONY: help install start stop restart db-up db-down \
        migrate generate db-generate db-push studio psql \
        build deploy deploy-logs deploy-down \
        test test-watch \
        health help

## --- Help ------------------------------------------------------------------

help:
	@echo "Local development (DB in Docker, app on host):"
	@echo "  make start            Start DB + dev server (http://localhost:3014)"
	@echo "  make stop             Stop the database container"
	@echo "  make restart          Restart DB + dev server"
	@echo "  make install          Install host dependencies"
	@echo ""
	@echo "Database:"
	@echo "  make db-up            Start only the database container"
	@echo "  make db-down          Stop the database container"
	@echo "  make migrate          Apply Drizzle migrations"
	@echo "  make db-generate      Generate a migration from schema changes"
	@echo "  make db-push          Push schema to DB (dev shortcut, no migration)"
	@echo "  make studio           Open Drizzle Studio to browse the database"
	@echo "  make psql             Open a psql shell on the database"
	@echo ""
	@echo "Quality:"
	@echo "  make build            Production build"
	@echo "  make test             Run the whole test suite + copy-paste summary"
	@echo "  make test-watch       Run the test suite in watch mode"
	@echo ""
	@echo "Deploy (run on the SERVER, with .env filled in):"
	@echo "  make deploy           Pull latest, rebuild, restart"
	@echo "  make deploy-logs      Follow the app logs"
	@echo "  make deploy-down      Stop the production stack"
	@echo "  make health           Check app health endpoint via public domain"

## --- Local development (DB in Docker, app on host) -------------------------

install:
	npm install

start:
	-@docker compose stop app 2>/dev/null || true
	docker compose up -d db
	@for _ in $$(seq 1 20); do docker compose exec -T db pg_isready -U postgres >/dev/null 2>&1 && break; sleep 1; done
	npm run dev

stop:
	docker compose down

restart: stop start

## --- Database --------------------------------------------------------------

db-up:
	docker compose up -d db

db-down:
	docker compose down

migrate:
	npm run db:migrate

generate: db-generate ## Alias for db-generate

db-generate:
	npm run db:generate

db-push:
	npm run db:push

studio:
	npm run db:studio

psql:
	docker compose exec db psql -U postgres -d moh_sass

## --- Quality ---------------------------------------------------------------

build:
	npm run build

test:
	@node scripts/test-summary.mjs

test-watch:
	npm run test:watch

## --- Deploy (server) -------------------------------------------------------
# Run these ON THE SERVER, inside the repo. docker-compose.yml is the full stack
# (app + db); the server does `git pull` + `make deploy`.

deploy:
	git pull
	docker compose up -d --build
	@git log -1 --oneline

deploy-logs:
	docker compose logs -f --tail=200

deploy-down:
	docker compose down

health:
	curl -sS https://mohfarawati.de/api/health
