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

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

echo "================================================================"
echo " prom-e2e up"
echo "================================================================"
echo "env: ${PROM_E2E_ENV_FILE}"
echo "result dir: ${PROM_E2E_RESULT_DIR}"
echo ""

build_prometheus_binary
generate_local_e2e_configs
start_mirrord_tunnels
render_runtime_configs
render_prometheus_config

echo "[prom-e2e] starting docker-compose services..."
compose up -d

if ! wait_http "http://127.0.0.1:${PROM_E2E_PROMETHEUS_PORT}/-/healthy" 60; then
  echo "❌ Prometheus container did not become healthy"
  exit 1
fi

if ! wait_http "http://127.0.0.1:${PROM_E2E_GRAFANA_PORT}/api/health" 60; then
  echo "❌ Grafana container did not become healthy"
  exit 1
fi

echo "[prom-e2e] waiting 25s for Prometheus to scrape exporters..."
sleep 25

echo ""
echo "✅ prom-e2e stack is up"
echo "Prometheus: http://127.0.0.1:${PROM_E2E_PROMETHEUS_PORT}"
echo "Grafana:    http://127.0.0.1:${PROM_E2E_GRAFANA_PORT}  (admin/admin)"
echo ""
echo "Next:"
echo "  bash pipeline/tools/prom-e2e/scripts/verify.sh"
