#!/usr/bin/env bash
# One-time / update helpers for cPanel Node.js hosting.
# Run from the application root after uploading the deploy package.
set -euo pipefail

echo "==> Node version (expect 20.x)"
node -v

if [[ ! -f package.json ]]; then
  echo "Run this script from the Node.js application root."
  exit 1
fi

if [[ ! -f .next/BUILD_ID ]]; then
  echo "Missing .next/BUILD_ID. Upload a production build before setup."
  exit 1
fi

echo "==> Installing production dependencies (Linux native builds)"
if [[ -f package-lock.json ]]; then
  npm ci --omit=dev
else
  npm install --omit=dev
fi

mkdir -p data/uploads

if [[ ! -f .env ]]; then
  echo "WARNING: .env not found. Copy .env.example to .env and set secrets,"
  echo "         or configure the same keys in cPanel Setup Node.js App."
fi

echo "==> Applying database migrations"
npm run db:migrate

if [[ "${1:-}" == "--seed" ]]; then
  echo "==> Seeding default content"
  npm run db:seed
fi

echo "==> Done. Restart the Node.js application in cPanel."
echo "    Then open https://YOUR-DOMAIN/admin/login"
