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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

export IRIS_HTTPS_PORT="${IRIS_HTTPS_PORT:-8443}"
export SHUFFLE_OPENSEARCH_PORT="${SHUFFLE_OPENSEARCH_PORT:-9201}"
export PAGERDUTY_STUB_PORT="${PAGERDUTY_STUB_PORT:-18080}"
export SOC_INTEGRATOR_PORT="${SOC_INTEGRATOR_PORT:-8088}"

check_http() {
  local name="$1"
  local url="$2"
  local insecure="${3:-false}"
  local code

  if [[ "${insecure}" == "true" ]]; then
    code="$(curl -k -sS -o /dev/null -w "%{http_code}" --max-time 5 "${url}" || true)"
  else
    code="$(curl -sS -o /dev/null -w "%{http_code}" --max-time 5 "${url}" || true)"
  fi

  if [[ -n "${code}" && "${code}" != "000" ]]; then
    printf "%-20s %-7s %s\n" "${name}" "OK" "${url} (HTTP ${code})"
  else
    printf "%-20s %-7s %s\n" "${name}" "FAIL" "${url}"
  fi
}

echo "SOC stack container status"
docker ps -a \
  --filter "name=wazuh-single-" \
  --filter "name=iriswebapp_" \
  --filter "name=shuffle-" \
  --filter "name=pagerduty-stub" \
  --filter "name=soc-integrator" \
  --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

echo
echo "Endpoint checks"
check_http "Wazuh Dashboard" "https://localhost/" "true"
check_http "Wazuh API" "https://localhost:55000/" "true"
check_http "IRIS Web" "https://localhost:${IRIS_HTTPS_PORT}/" "true"
check_http "Shuffle Frontend" "http://localhost:3001/"
check_http "Shuffle Backend" "http://localhost:5001/"
check_http "Shuffle OpenSearch" "https://localhost:${SHUFFLE_OPENSEARCH_PORT}/" "true"
check_http "PagerDuty Stub" "http://localhost:${PAGERDUTY_STUB_PORT}/"
check_http "SOC Integrator" "http://localhost:${SOC_INTEGRATOR_PORT}/health"

echo
echo "Key URLs"
echo "Wazuh Dashboard : https://localhost"
echo "IRIS Web        : https://localhost:${IRIS_HTTPS_PORT}"
echo "Shuffle UI      : http://localhost:3001"
echo "PagerDuty Stub  : http://localhost:${PAGERDUTY_STUB_PORT}"
echo "SOC Integrator  : http://localhost:${SOC_INTEGRATOR_PORT}/docs"
