#!/bin/bash
# ==========================================================
# POWERBALL CLAIMS CENTER - LINUX VPS DEPLOYMENT SCRIPT
# ==========================================================

set -e

echo "=== [1/5] Checking Node.js Environment ==="
node -v || { echo "Node.js is not installed. Please install Node.js 20 or higher."; exit 1; }
npm -v

echo "=== [2/5] Installing Dependencies ==="
npm ci --production=false

echo "=== [3/5] Building Next.js Production Application ==="
npm run build

echo "=== [4/5] Setting up PM2 Process Manager ==="
if ! command -v pm2 &> /dev/null
then
    echo "Installing PM2 globally..."
    npm install -g pm2
fi

echo "=== [5/5] Launching Application with PM2 ==="
pm2 start ecosystem.config.js || pm2 restart powerball-claims-portal
pm2 save

echo ""
echo "=========================================================="
echo "✓ DEPLOYMENT COMPLETE! Application is running on port 3000."
echo "  Live Portal:   http://your-server-ip:3000/"
echo "  Admin Center:  http://your-server-ip:3000/admin"
echo "  Default Login: admin / admin"
echo "=========================================================="
