#!/usr/bin/env bash
# run-all.sh - run the full local flow: build -> generate configs -> Prometheus test -> OTel test
set -euo pipefail

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

echo "================================================================"
echo " TiDB X Local Comparison Test"
echo "================================================================"
echo ""

# Check whether AUTH0_CLIENT_SECRET is set in test.env
if [[ -z "${AUTH0_CLIENT_SECRET:-}" ]]; then
  echo "❌ AUTH0_CLIENT_SECRET is not set. Please edit ${TEST_ENV_FILE}"
  exit 1
fi

echo "Test round: ${TEST_ROUND_NAME}"
echo "Results directory: ${RESULTS_DIR}"
echo ""

echo "Cluster availability check:"
for cluster_entry in \
  "${CLUSTER_ID_DEDICATED:-}:Dedicated" \
  "${CLUSTER_ID_PREMIUM:-}:Premium" \
  "${CLUSTER_ID_ESSENTIAL:-}:Essential" \
  "${CLUSTER_ID_STARTER:-}:Starter"; do
  IFS=: read -r cluster_id service_plan <<< "${cluster_entry}"
  if [[ -z "${cluster_id}" ]]; then
    echo "  - ${service_plan}: cluster_id is empty, skipping in this round"
  else
    echo "  - ${service_plan}: ${cluster_id}"
  fi
done

# Step 1: ensure the baseline worktree exists
echo "[1/4] Checking the baseline worktree..."
echo "  Baseline ref: ${BASELINE_REF}"
echo "  Baseline label: ${BASELINE_LABEL}"
if [[ ! -d "${BASELINE_WORKTREE}" ]]; then
  echo "  Directory does not exist, adding a worktree at ${BASELINE_WORKTREE}..."
  git -C "${REPO_ROOT}" worktree add "${BASELINE_WORKTREE}" "${BASELINE_REF}"
else
  echo "  Directory already exists: ${BASELINE_WORKTREE}"
fi

# Step 2: build pipeline binaries
echo ""
echo "[2/4] Building pipeline binaries..."
FEATURE_PIPELINE="${REPO_ROOT}/pipeline"
BASELINE_PIPELINE="${BASELINE_WORKTREE}/pipeline"
BIN_DIR="${GENERATED_BIN_DIR}"
mkdir -p "${BIN_DIR}"

echo "  [feature] metrics-collector..."
(cd "${FEATURE_PIPELINE}" && CGO_ENABLED=0 go build -o "${BIN_DIR}/metrics-collector-feature" cmd/metrics-collector/main.go)
echo "  [feature] prometheus..."
(cd "${FEATURE_PIPELINE}" && CGO_ENABLED=0 go build -o "${BIN_DIR}/prometheus-feature" cmd/prometheus/main.go)
echo "  [${BASELINE_LABEL}] metrics-collector..."
(cd "${BASELINE_PIPELINE}" && CGO_ENABLED=0 go build -o "${BIN_DIR}/metrics-collector-${BASELINE_LABEL}" cmd/metrics-collector/main.go)
echo "  [${BASELINE_LABEL}] prometheus..."
(cd "${BASELINE_PIPELINE}" && CGO_ENABLED=0 go build -o "${BIN_DIR}/prometheus-${BASELINE_LABEL}" cmd/prometheus/main.go)

echo "  Build complete. Binaries:"
ls -lh "${BIN_DIR}"/metrics-collector-* "${BIN_DIR}"/prometheus-* 2>/dev/null

# Step 3: generate test configs
echo ""
echo "[3/4] Generating test configs..."
bash "${SCRIPT_DIR}/gen-configs.sh"

# Step 4: run tests
echo ""
echo "[4/4] Running Prometheus and OTel tests in parallel..."
bash "${SCRIPT_DIR}/run-prometheus-test.sh" &
PID_PROM=$!
bash "${SCRIPT_DIR}/run-otel-test.sh" &
PID_OTEL=$!

ALL_OK=true
wait "${PID_PROM}" || ALL_OK=false
wait "${PID_OTEL}" || ALL_OK=false
${ALL_OK} || { echo "⚠️  Some tests failed"; exit 1; }

# Summary
echo ""
echo "================================================================"
echo " All tests completed. Result file tree:"
echo "================================================================"
find "${RESULTS_DIR}" -type f | sort
