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

echo "[INFO] Sending logs via node-a/node-b and asserting ES counts..."

get_count() {
  local idx="$1"
  curl -s "http://localhost:9200/${idx}/_count?ignore_unavailable=true&allow_no_indices=true" | sed -E 's/.*"count":([0-9]+).*/\1/' | awk 'NF{print $0;exit} END{if(NR==0)print 0}'
}

train0=$(get_count "train-*")
infer0=$(get_count "infer-*")
base=$((train0 + infer0))
echo "[INFO] initial counts: train=${train0} infer=${infer0} total=${base}"

send_logs() {
  local cname="$1"; local hosttag="$2"
  docker exec "$cname" sh -lc 'mkdir -p /logs/train /logs/infer'
  docker exec "$cname" sh -lc "ts=\
\$(date '+%F %T'); echo \"\$ts INFO [$hosttag] training step=1 loss=1.23 model=bert\" >> /logs/train/train-demo.log"
  docker exec "$cname" sh -lc "ts=\
\$(date '+%F %T'); echo \"\$ts INFO [$hosttag] training step=2 loss=1.10 model=bert\" >> /logs/train/train-demo.log"
  docker exec "$cname" sh -lc "ts=\
\$(date '+%F %T'); echo \"\$ts WARN [$hosttag] inference slow on batch=2 latency=1.9s\" >> /logs/infer/infer-demo.log"
}

# Determine container names
node_a=$(docker ps --format '{{.Names}}' | grep -E '^argus-node-a$|argus-sys-node-a-1' | head -n1)
node_b=$(docker ps --format '{{.Names}}' | grep -E '^argus-node-b$|argus-sys-node-b-1' | head -n1)

send_logs "$node_a" "host01"
send_logs "$node_b" "host02"

echo "[INFO] Waiting for ES to ingest..."
sleep 10

train1=$(get_count "train-*")
infer1=$(get_count "infer-*")
final=$((train1 + infer1))
echo "[INFO] final counts: train=${train1} infer=${infer1} total=${final}"

if (( final <= base )); then
  echo "[ERR] ES total did not increase (${base} -> ${final})" >&2
  exit 1
fi

if (( final < 4 )); then
  echo "[ERR] ES total below expected threshold: ${final} < 4" >&2
  exit 1
fi

# Health endpoints
es_health=$(curl -s "http://localhost:9200/_cluster/health" | grep -o '"status":"[^"]*"' | cut -d'"' -f4)
if [[ "$es_health" != "green" && "$es_health" != "yellow" ]]; then
  echo "[ERR] ES health not green/yellow: $es_health" >&2
  exit 1
fi

if ! curl -fs "http://localhost:5601/api/status" >/dev/null 2>&1; then
  echo "[WARN] Kibana status endpoint not available"
fi

echo "[OK] ES counts increased and services healthy"
