#!/bin/bash
# Curl /api/healthz and report status + response time.
URL="${1:-http://127.0.0.1:8080/api/healthz}"
START=$(date +%s%N)
CODE=$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "$URL" 2>/dev/null) || CODE="000"
END=$(date +%s%N)
MS=$(( (END - START) / 1000000 ))
if [ "$CODE" = "200" ]; then
  echo "OK: API healthz returned 200 in ${MS}ms | response_time=${MS}ms;500;1000;0;"
  exit 0
elif [ "$CODE" = "000" ]; then
  echo "CRITICAL: API not reachable at $URL | response_time=${MS}ms"
  exit 2
else
  echo "CRITICAL: API healthz returned HTTP $CODE | response_time=${MS}ms"
  exit 2
fi
