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

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

ensure_env_file
ensure_paths_defined

service_id() {
  compose ps -q "$1"
}

wait_http() {
  local url="$1"; local attempts="${2:-120}"; local i=1
  while (( i <= attempts )); do
    if curl -fsS "$url" >/dev/null 2>&1; then
      return 0
    fi
    echo "[..] waiting $url ($i/$attempts)"
    sleep 5
    ((i++))
  done
  echo "[ERR] Timeout waiting for $url" >&2
  return 1
}

log "Waiting for ES/Kibana/Master/Fluent Bit/Bind"

attempt=1; max=120
while (( attempt <= max )); do
  if curl -fsS "http://localhost:9200/_cluster/health?wait_for_status=yellow&timeout=1s" >/dev/null 2>&1; then
    break
  fi
  echo "[..] waiting ES ($attempt/$max)"
  sleep 5
  ((attempt++))
done
if (( attempt > max )); then
  echo "[ERR] ES not ready" >&2
  exit 1
fi

log "Applying relaxed ES disk watermarks for debug"
curl -fsS -XPUT "http://localhost:9200/_cluster/settings" \
  -H 'Content-Type: application/json' \
  -d '{
    "transient": {
      "cluster.routing.allocation.disk.watermark.low": "99%",
      "cluster.routing.allocation.disk.watermark.high": "99%",
      "cluster.routing.allocation.disk.watermark.flood_stage": "99%"
    }
  }' >/dev/null || echo "[WARN] Failed to adjust ES watermarks"

log "Waiting for Kibana to be available (HTTP 200)"
kb_attempt=1; kb_max=180
while (( kb_attempt <= kb_max )); do
  body=$(curl -sS "http://localhost:5601/api/status" 2>/dev/null || true)
  code=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:5601/api/status" || echo 000)
  if [[ "$code" == "200" ]] && echo "$body" | grep -q '"level":"available"'; then
    log "Kibana available"
    break
  fi
  echo "[..] waiting kibana 200 ($kb_attempt/$kb_max), last_code=$code"
  sleep 5
  ((kb_attempt++))
done
if (( kb_attempt > kb_max )); then
  echo "[ERR] Kibana did not reach HTTP 200" >&2
  exit 1
fi

wait_http "http://localhost:32300/readyz" 120
wait_http "http://localhost:2020/api/v2/metrics" 120
wait_http "http://localhost:2021/api/v2/metrics" 120

BIND_ID="$(service_id bind)"
if [[ -n "$BIND_ID" ]]; then
  docker exec "$BIND_ID" named-checkconf >/dev/null
else
  echo "[WARN] bind container id not found" >&2
fi

log "All services are ready"
