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

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEST_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
VERIFY_SCRIPT="$(cd "$TEST_ROOT/.." && pwd)/scripts/agent_deployment_verify.sh"

if ! docker ps --format '{{.Names}}' | grep -q '^argus-agent-e2e$'; then
  echo "[WARN] agent container not running; skip verification"
  exit 0
fi

if docker exec -i argus-agent-e2e bash -lc 'command -v curl >/dev/null && command -v jq >/dev/null'; then
  echo "[INFO] curl/jq already installed in agent container"
else
  echo "[INFO] Installing curl/jq in agent container"
  docker exec -i argus-agent-e2e bash -lc 'apt-get update >/dev/null 2>&1 && apt-get install -y curl jq >/dev/null 2>&1' || true
fi

if docker exec -i argus-agent-e2e bash -lc 'command -v /usr/local/bin/agent_deployment_verify.sh >/dev/null'; then
  docker exec -i argus-agent-e2e /usr/local/bin/agent_deployment_verify.sh
elif [[ -x "$VERIFY_SCRIPT" ]]; then
  docker exec -i argus-agent-e2e "$VERIFY_SCRIPT"
else
  echo "[WARN] agent_deployment_verify.sh not found"
fi
