#!/usr/bin/env bash
# Membresia 360 — Corrige erro 502 Bad Gateway
# Uso: sudo bash fix-502.sh
set -e
APP_DIR="${APP_DIR:-/var/www/membresia360}"
APP_NAME="${APP_NAME:-membresia360}"
cd "$APP_DIR"
echo "==> Removendo processo antigo do PM2..."
pm2 delete "$APP_NAME" || true
echo "==> Rebuild..."
NITRO_PRESET=node-server npm run build
echo "==> Iniciando novamente..."
pm2 start ".output/server/index.mjs" --name "$APP_NAME"
pm2 save
echo "==> Recarregando Nginx..."
systemctl reload nginx
echo "==> OK. Testando com curl local..."
sleep 2
curl -sSI http://127.0.0.1:3000/ | head -n 1 || true
