#!/usr/bin/env bash
# Shared entrypoint for the ADR-0030 `apps` Compose profile services.
#
# Behaviour is driven by env + the passed command:
#   - The `apps-deps` one-shot service runs `pnpm install` as its
#     command; this entrypoint just execs it (APF_ROLE unset → no
#     prisma). It populates the shared node_modules volume once, so
#     the app services don't race three concurrent installs.
#   - The BFF service sets APF_ROLE=bff → this entrypoint runs
#     `prisma generate` + `prisma migrate deploy` (client + committed
#     migrations) before serving. `migrate deploy`, never `migrate
#     dev` — a container never authors migrations.
#   - The SPA services (portal-shell / portal-admin) leave APF_ROLE
#     unset → straight to `exec "$@"` (nx serve).
set -euo pipefail
cd /workspace

if [[ "${APF_ROLE:-}" == "bff" ]]; then
  echo "[dev-entrypoint] BFF: prisma generate"
  pnpm exec prisma generate
  echo "[dev-entrypoint] BFF: prisma migrate deploy"
  pnpm exec prisma migrate deploy
fi

echo "[dev-entrypoint] exec: $*"
exec "$@"
