#!/usr/bin/env bash
# NABDH — cPanel post-deploy sequence.
#
# Run this from inside the cPanel Node.js App's virtual environment shell
# (Setup Node.js App → your app → "Enter to the virtual environment" gives you
# a terminal with the correct Node version and the app's environment variables
# already loaded) with the current directory set to the Application Root
# (the "app/" folder from this package, after upload and extraction).
#
# Safe to re-run. Does not reset, drop, or delete any data:
#   - `prisma generate` only (re)builds the query engine/types.
#   - `prisma migrate deploy` only applies migrations not yet recorded as
#     applied; it never runs `migrate reset` and never drops tables.
set -euo pipefail

echo "[1/3] Installing dependencies (npm ci)…"
if [ -f package-lock.json ]; then
  npm ci
else
  npm install
fi

echo "[2/3] Generating the Prisma client…"
npx prisma generate

echo "[3/3] Applying database migrations (safe — no reset, no data loss)…"
npx prisma migrate deploy

echo
echo "Done. Restart the application from cPanel's Setup Node.js App screen"
echo "(or run: touch tmp/restart.txt if your account uses that convention)."
