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

PROJECT_NAME="${PROJECT_NAME:-davos-assessment}"
ALERT_WEBHOOK_URL="${ALERT_WEBHOOK_URL:-}"

notify_failure() {
  if [ -z "$ALERT_WEBHOOK_URL" ]; then
    return
  fi
  local msg="${1:-容器状态异常}"
  curl -sS -X POST "$ALERT_WEBHOOK_URL" \
    -H "Content-Type: application/json" \
    -d "{\"title\":\"CONTAINER_ALERT\",\"message\":\"${msg}\"}" >/dev/null || true
}

if ! docker compose -p "$PROJECT_NAME" ps --status running | grep -q "backend"; then
  notify_failure "backend 容器未运行"
  exit 1
fi

if ! docker compose -p "$PROJECT_NAME" ps --status running | grep -q "db"; then
  notify_failure "db 容器未运行"
  exit 1
fi

if ! docker compose -p "$PROJECT_NAME" ps --status running | grep -q "frontend"; then
  notify_failure "frontend 容器未运行"
  exit 1
fi

echo "Stack health looks good."
