.PHONY: start stop restart deploy logs build ps port health clean-orphans app-shell db-shell db-init db-migrate db-seed prisma-generate prisma-migrate help

MIGRATION_NAME ?= init

start:
	docker compose up -d --build

stop:
	docker compose down

restart: stop start

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

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

build:
	docker compose build

ps:
	docker compose ps

port:
	@echo "Site:  http://localhost:3014"
	@echo "Admin: http://rootmohfarawati.localhost:3014"

clean-orphans:
	docker compose up -d --remove-orphans

app-shell:
	docker compose exec app sh

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

db-init:
	docker compose exec app sh -lc "npx prisma generate && npx prisma migrate deploy && npx prisma db seed"

db-migrate:
	docker compose exec app npx prisma migrate deploy

db-seed:
	docker compose exec app npx prisma db seed

prisma-generate:
	docker compose exec app npx prisma generate

prisma-migrate:
	docker compose exec app npx prisma migrate dev --name $(MIGRATION_NAME)

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

help:
	@echo "Available targets:"
	@echo "  make start            Start all containers"
	@echo "  make stop             Stop and remove containers"
	@echo "  make restart          Restart all containers"
	@echo "  make deploy           Pull latest code and deploy updated stack"
	@echo "  make logs             Follow container logs"
	@echo "  make build            Build images"
	@echo "  make ps               Show container status"
	@echo "  make port             Show app public URL"
	@echo "  make clean-orphans    Remove orphaned old containers"
	@echo "  make app-shell        Open shell in app container"
	@echo "  make db-shell         Open PostgreSQL shell"
	@echo "  make db-init          Generate client, apply migrations, run seed"
	@echo "  make db-migrate       Apply prisma migrations"
	@echo "  make db-seed          Seed database data"
	@echo "  make prisma-generate  Run prisma generate"
	@echo "  make prisma-migrate   Create/apply dev migration"
	@echo "  make health           Check app health endpoint via public domain"
