#!/usr/bin/env bash
set -euo pipefail

# 只重建/重啟 API、Web、Workers；不碰 mongo/redis（資料留在 volume）。

# shellcheck source=scripts/prod-common.sh
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/prod-common.sh"

prod_load_env
prod_require_docker

cd "$BACKEND_DIR"
prod_build_web_if_static

cd "$DEPLOY_DIR"

if ! prod_deps_healthy; then
  echo "[prod] mongo/redis 未在運行，先啟動依賴（不會清 volume）..."
  prod_ensure_deps
else
  echo "[prod] mongo + redis 維持運行 — 只更新應用層"
fi

echo "[prod] building app images (api, web, workers)..."
prod_compose build api web go-worker node-worker-style-8d

prod_start_app_services
prod_wait_api_health
prod_print_stack_summary