#!/bin/bash
# deploy.sh — Build & deploy clickerz to VPS
# Usage: ssh vps 'cd /opt/clickerz && bash deploy/deploy.sh'

set -euo pipefail

echo "=== Clickerz deploy ==="

# 1. Pull latest
git pull --ff-only

# 2. Build frontend
echo "--- Building frontend..."
cd Frontend
npm ci --production=false
npm run build
echo "--- Copying dist to /var/www/clickerz..."
sudo rm -rf /var/www/clickerz
sudo cp -r dist /var/www/clickerz
cd ..

# 3. Backend deps
echo "--- Installing backend deps..."
cd Backend
npm ci --production
cd ..

# 4. Run migrations
echo "--- Running DB migrations..."
cd Backend
npm run db:migrate
cd ..

# 5. Restart pm2
echo "--- Restarting pm2..."
pm2 startOrRestart ecosystem.config.cjs --env production
pm2 save

echo "=== Deploy complete ==="
