#!/bin/bash
set -e

echo "→ Running PM2 processes:"
pm2 list

echo ""
read -p "Enter app name or id to update: " TARGET

# ─── Sitemap ──────────────────────────────────────────────────────────────────
echo "→ Generating sitemap..."
node scripts/sitemap-generator.js

# ─── Build ────────────────────────────────────────────────────────────────────
echo "→ Building..."
NODE_OPTIONS="--max-old-space-size=1536" GENERATE_SOURCEMAP=false npm run build

# ─── Copy static assets to standalone ────────────────────────────────────────
echo "→ Copying static assets to standalone..."
cp -r .next/static .next/standalone/.next/static
cp -r public .next/standalone/public

# ─── Restart ──────────────────────────────────────────────────────────────────
echo "→ Restarting $TARGET..."
pm2 restart "$TARGET"

echo ""
echo "✓ Update complete → $TARGET"
