#!/bin/bash
# ============================================================================
# PharmaSFA Pro — Build & Start script for production
# Run this on your server AFTER uploading files and setting .env
# ============================================================================
set -e

echo "=== PharmaSFA Pro — Production Setup ==="

# 1. Install dependencies
echo "[1/5] Installing dependencies..."
npm install --omit=dev || bun install

# 2. Generate Prisma client
echo "[2/5] Generating Prisma client..."
npx prisma generate

# 3. Build Next.js for production
echo "[3/5] Building Next.js (this takes 1-2 minutes)..."
npx next build --webpack

# 4. Verify database connection
echo "[4/5] Testing database connection..."
npx prisma db pull --print > /dev/null 2>&1 && echo "  ✓ Database connected" || echo "  ✗ Database connection failed — check DATABASE_URL in .env"

# 5. Start the server
echo "[5/5] Starting production server on port 3000..."
echo ""
echo "=== App running at http://localhost:3000 ==="
echo "=== Press Ctrl+C to stop ==="
echo ""
exec npx next start -p 3000
