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

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

echo "[SYS-METRIC] Verify: master"
"$SCRIPT_DIR/13_metric_verify_master.sh"
echo

echo "[SYS-METRIC] Verify: prometheus"
PROM_RETRIES=${PROM_VERIFY_RETRIES:-2}
PROM_BACKOFF=${PROM_VERIFY_BACKOFF_SECONDS:-30}
attempt=0
while true; do
  if "$SCRIPT_DIR/13_metric_verify_prometheus.sh"; then
    break
  fi
  attempt=$((attempt+1))
  if (( attempt > PROM_RETRIES )); then
    echo "[ERR] prometheus verify failed after $PROM_RETRIES retries" >&2
    exit 1
  fi
  echo "[WARN] prometheus verify failed; retry $attempt/$PROM_RETRIES after ${PROM_BACKOFF}s"
  sleep "$PROM_BACKOFF"
done
echo

echo "[SYS-METRIC] Verify: dataplane"
"$SCRIPT_DIR/13_metric_verify_dataplane.sh"
echo

echo "[SYS-METRIC] Verify: grafana"
"$SCRIPT_DIR/13_metric_verify_grafana.sh"
echo

echo "[SYS-METRIC] Verify: grafana panels"
"$SCRIPT_DIR/13_metric_verify_grafana_panels.sh"
echo

echo "[SYS-METRIC] Metric verification completed"
