#!/usr/bin/env bash
# One-time host build for DermaClin (run over SSH inside the app folder).
#   bash deploy/setup.sh
set -euo pipefail
cd "$(dirname "$0")/.."

echo "==> node: $(node -v)"
NODE_MAJOR=$(node -p 'process.versions.node.split(".")[0]')
if [ "$NODE_MAJOR" -lt 24 ]; then
  echo "!! node:sqlite needs Node 24+. Pick Node.js 24 in cPanel 'Setup Node.js App', then re-run."
  exit 1
fi

echo "==> installing server dependencies"
(cd server && npm install --omit=dev --no-audit --no-fund)

echo "==> building the UI (needs devDependencies)"
(cd client && npm install --no-audit --no-fund && npm run build)

if [ ! -d client/dist ]; then
  echo "!! client/dist is missing - the UI build failed"; exit 1
fi

mkdir -p data/uploads backups
[ -f data/secret.key ] && chmod 600 data/secret.key

echo
echo "==> done. Next:"
echo "   1. bash deploy/test-on-host.sh     # full feature test on a throwaway database"
echo "   2. node deploy/host-check.mjs      # read-only audit of the real instance"
echo "   3. change the admin password before opening the domain to the public"
