#!/bin/sh
# NABDH container entrypoint.
#
# Prepares the database before handing control to the server, so a fresh `docker compose
# up` produces a working demonstration rather than an empty console.
#
# Both steps are opt-out: seeding resets the demonstration data, which is exactly right
# for a demo container and exactly wrong for anything holding real activity. Set
# SKIP_SEED=true (or SKIP_DB_SETUP=true) for those deployments.
set -e

echo "[nabdh] validating environment"
node scripts/check-env.mjs

if [ "${SKIP_DB_SETUP}" != "true" ]; then
  echo "[nabdh] syncing database schema"
  node scripts/db-prepare.mjs
  npx prisma db push --skip-generate --accept-data-loss

  if [ "${SKIP_SEED}" != "true" ]; then
    echo "[nabdh] seeding demonstration data"
    npx tsx prisma/seed.ts
  else
    echo "[nabdh] seeding skipped (SKIP_SEED=true)"
  fi
fi

echo "[nabdh] starting server on ${HOSTNAME:-0.0.0.0}:${PORT:-3000}"
exec "$@"
