#!/bin/bash
# ============================================================
# aqar210 — deploy script for cPanel (Namecheap) shared hosting
# Run from SSH:  bash deploy.sh [/home/USER/public_html/1]
# ============================================================
set -e

APP_DIR="${1:-$HOME/public_html/1}"
ENV_FILE="$APP_DIR/.env"

echo "=== aqar210 deploy ==="
echo "App dir : $APP_DIR"
node -v
npm -v

if [ ! -f "$ENV_FILE" ]; then
  echo "ERROR: $ENV_FILE not found. Upload .env first."
  exit 1
fi

cd "$APP_DIR"
export NODE_ENV=production

echo "==> 1/3 npm install (runs postinstall: prisma generate + db push + seed)"
npm ci --no-audit --no-fund || npm install --no-audit --no-fund

echo "==> 2/3 next build"
npm run build

echo "==> 3/3 done. Restart the Node app from cPanel."
echo "If the build runs out of memory, retry with:"
echo "  NODE_OPTIONS=--max-old-space-size=1024 npm run build"
