#!/usr/bin/env bash
# LunarFront — Redeploy script (run after pushing changes to main)
# Usage: sudo bash deploy/deploy.sh
set -euo pipefail

APP_DIR="/opt/lunarfront"
APP_USER="ubuntu"
BUN_BIN="/home/${APP_USER}/.bun/bin/bun"

cd "$APP_DIR"

echo "==> Installing dependencies..."
sudo -u "$APP_USER" "$BUN_BIN" install --frozen-lockfile

echo "==> Building admin frontend..."
sudo -u "$APP_USER" bash -c "cd ${APP_DIR}/packages/admin && ${BUN_BIN} run build"

echo "==> Running migrations..."
sudo -u "$APP_USER" bash -c \
  "cd ${APP_DIR}/packages/backend && ${BUN_BIN} x drizzle-kit migrate"

echo "==> Restarting backend..."
sudo systemctl restart lunarfront

echo "==> Done! Checking status..."
sleep 2
sudo systemctl status lunarfront --no-pager
