#!/usr/bin/env bash
#
# Restart services in the Supabase or LiveKit compose stack.
#
# Usage:
#   ./scripts/prod/restart.sh <stack> [<service>]
#     stack   = supabase | livekit
#     service = (optional) restart just this one, default = whole stack
#
# Examples:
#   ./scripts/prod/restart.sh supabase          # down + up -d
#   ./scripts/prod/restart.sh supabase auth     # only auth
#   ./scripts/prod/restart.sh livekit           # both caddy + livekit
#   ./scripts/prod/restart.sh livekit livekit   # only livekit container

set -euo pipefail

here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${here}/config.sh"

stack="${1:-}"
service="${2:-}"

case "${stack}" in
  supabase) dir="${PROD_SUPABASE_DIR}" ;;
  livekit)  dir="${PROD_LIVEKIT_DIR}" ;;
  *)
    echo "usage: $0 <supabase|livekit> [service]" >&2
    exit 1
    ;;
esac

if [[ -n "${service}" ]]; then
  echo "restarting ${stack}/${service}"
  remote "cd ${dir} && docker compose restart ${service}"
else
  echo "restarting full ${stack} stack (down + up -d)"
  remote "cd ${dir} && docker compose down && docker compose up -d"
fi

echo
remote "cd ${dir} && docker compose ps"
