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

endpoints=(
  "Backend API|http://127.0.0.1:8000/health"
  "Platform Service|http://127.0.0.1:4003/health"
  "Extraction Service|http://127.0.0.1:4005/health"
  "Extraction Sidecar|http://127.0.0.1:4006/health"
  "Admin Dashboard|http://127.0.0.1:3001/api/health"
  "User Dashboard|http://127.0.0.1:3002/api/health"
  "Tracker Dashboard|http://127.0.0.1:3003/api/health"
  "Traefik (dashboard)|http://127.0.0.1:8080/api/overview"
  "Loki|http://127.0.0.1:3100/ready"
  "Grafana|http://127.0.0.1:3000/api/health"
)

failures=0

echo "Health check (local)"
echo

for item in "${endpoints[@]}"; do
  name="${item%%|*}"
  url="${item#*|}"

  if curl -fsS --max-time 5 "$url" >/dev/null; then
    printf "  OK   %s\n" "$name"
  else
    printf "  FAIL %s (%s)\n" "$name" "$url" >&2
    failures=$((failures + 1))
  fi
done

echo

if [[ "$failures" -gt 0 ]]; then
  echo "Result: FAIL ($failures)" >&2
  exit 1
fi

echo "Result: OK"
