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

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

PRIVATE_CORE="$TEST_ROOT/private"
PRIVATE_NODEA="$TEST_ROOT/private-nodea"
PRIVATE_NODEB="$TEST_ROOT/private-nodeb"
TMP_DIR="$TEST_ROOT/tmp"

source "$REPO_ROOT/scripts/common/build_user.sh"
load_build_user

ensure_image() {
  local image="$1"
  if ! docker image inspect "$image" >/dev/null 2>&1; then
    echo "[ERROR] Missing image: $image. Please run ./build/build_images.sh" >&2
    exit 1
  fi
}

echo "[INFO] Preparing directories..."
ensure_writable_dir() {
  local path="$1"
  local parent
  parent="$(dirname "$path")"
  mkdir -p "$parent" 2>/dev/null || true
  mkdir -p "$path" 2>/dev/null || true
  if [[ ! -w "$path" ]]; then
    docker run --rm -v "$parent:/target" ubuntu:24.04 bash -lc "chown -R $(id -u):$(id -g) /target" >/dev/null 2>&1 || true
  fi
  mkdir -p "$path"
}

# preflight: make base dirs writable if inherited from root-owned mounts
ensure_writable_dir "$PRIVATE_CORE/argus"
ensure_writable_dir "$PRIVATE_CORE/argus/metric"
ensure_writable_dir "$PRIVATE_CORE/argus/metric/grafana"
ensure_writable_dir "$PRIVATE_CORE/argus/metric/prometheus"

mkdir -p \
  "$PRIVATE_CORE/argus/etc" \
  "$PRIVATE_CORE/argus/bind" \
  "$PRIVATE_CORE/argus/master" \
  "$PRIVATE_CORE/argus/metric/prometheus" \
  "$PRIVATE_CORE/argus/metric/ftp/share" \
  "$PRIVATE_CORE/argus/metric/grafana/data" \
  "$PRIVATE_CORE/argus/metric/grafana/logs" \
  "$PRIVATE_CORE/argus/metric/grafana/plugins" \
  "$PRIVATE_CORE/argus/metric/grafana/provisioning/datasources" \
  "$PRIVATE_CORE/argus/metric/grafana/provisioning/dashboards" \
  "$PRIVATE_CORE/argus/metric/grafana/data/sessions" \
  "$PRIVATE_CORE/argus/metric/grafana/data/dashboards" \
  "$PRIVATE_CORE/argus/metric/grafana/config" \
  "$PRIVATE_CORE/argus/metric/prometheus/data" \
  "$PRIVATE_CORE/argus/metric/prometheus/rules" \
  "$PRIVATE_CORE/argus/metric/prometheus/targets" \
  "$PRIVATE_CORE/argus/agent" \
  "$PRIVATE_CORE/argus/log/elasticsearch" \
  "$PRIVATE_CORE/argus/log/kibana" \
  "$PRIVATE_NODEA/argus/agent/dev-yyrshare-nbnyx10-cp2f-pod-0/health" \
  "$PRIVATE_NODEB/argus/agent/dev-yyrshare-uuuu10-ep2f-pod-0/health" \
  "$TMP_DIR"

# Align ownership for supervisor-managed services (ES/Kibana/Grafana expect UID/GID inside container)
echo "[INFO] Fixing ownership for core private directories..."
chown -R "${ARGUS_BUILD_UID}:${ARGUS_BUILD_GID}" \
  "$PRIVATE_CORE/argus/log/elasticsearch" \
  "$PRIVATE_CORE/argus/log/kibana" \
  "$PRIVATE_CORE/argus/metric/grafana" \
  "$PRIVATE_CORE/argus/metric/prometheus" \
  "$PRIVATE_CORE/argus/metric/ftp" \
  "$PRIVATE_CORE/argus/agent" \
  "$PRIVATE_CORE/argus/etc" 2>/dev/null || true

echo "[INFO] Using compose-managed network (auto-created by docker compose)"

echo "[INFO] Distributing update-dns.sh for core services (bind/master/es/kibana)"
BIND_UPDATE_SRC="$REPO_ROOT/src/bind/build/update-dns.sh"
BIND_UPDATE_DEST="$PRIVATE_CORE/argus/etc/update-dns.sh"
if [[ -f "$BIND_UPDATE_SRC" ]]; then
  cp "$BIND_UPDATE_SRC" "$BIND_UPDATE_DEST"
  chmod +x "$BIND_UPDATE_DEST"
else
  echo "[WARN] bind update-dns.sh not found at $BIND_UPDATE_SRC"
fi

echo "[INFO] Ensuring images present..."
ensure_image "argus-elasticsearch:latest"
ensure_image "argus-kibana:latest"
ensure_image "argus-bind9:latest"
ensure_image "argus-master:latest"
ensure_image "argus-metric-ftp:latest"
ensure_image "argus-metric-prometheus:latest"
ensure_image "argus-metric-grafana:latest"

echo "[INFO] Building agent binary..."
pushd "$REPO_ROOT/src/agent" >/dev/null
./scripts/build_binary.sh
popd >/dev/null

AGENT_BIN="$REPO_ROOT/src/agent/dist/argus-agent"
if [[ ! -x "$AGENT_BIN" ]]; then
  echo "[ERROR] Agent binary not found at $AGENT_BIN" >&2
  exit 1
fi
echo "$AGENT_BIN" > "$TMP_DIR/agent_binary_path"

# 检测GPU环境
REQUEST_GPU=${ARGUS_SYS_ENABLE_GPU:-false}
GPU_CHECK_SCRIPT="$REPO_ROOT/src/metric/tests/scripts/common/check-gpu.sh"
if [[ "$REQUEST_GPU" == "true" ]]; then
  echo "[INFO] --enable-gpu 已启用，开始检测GPU环境..."
  if [[ -f "$GPU_CHECK_SCRIPT" ]]; then
    if bash "$GPU_CHECK_SCRIPT" >/dev/null 2>&1; then
      echo "[INFO] GPU环境可用，将在 compose 中启用 test-gpu-node"
      GPU_AVAILABLE=true
    else
      echo "[ERROR] 未检测到可用 GPU，但指定了 --enable-gpu" >&2
      exit 1
    fi
  else
    echo "[ERROR] 未找到 GPU 检测脚本: $GPU_CHECK_SCRIPT" >&2
    exit 1
  fi
else
  GPU_AVAILABLE=false
  echo "[INFO] GPU 支持未启用，跳过 GPU 检测"
fi

echo "[INFO] Writing .env with UID/GID and metric configuration"
cat > "$TEST_ROOT/.env" <<EOF
ARGUS_BUILD_UID=$ARGUS_BUILD_UID
ARGUS_BUILD_GID=$ARGUS_BUILD_GID

# GPU 配置
ENABLE_GPU=$GPU_AVAILABLE

# FTP 配置
FTP_PORT=21
FTP_DATA_PORT=20
FTP_PASSWORD=ZGClab1234!
FTP_DOMAIN=ftp.metric.argus.com

# Prometheus 配置
PROMETHEUS_PORT=9090

# Grafana 配置
GRAFANA_PORT=3000

# 网络配置
USE_INTRANET=false
EOF

echo "[OK] Bootstrap completed"
