#!/usr/bin/env sh
set -eu

SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
INFRA_DIR=$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)
PLATFORM_ENV="${PLATFORM_ENV:-$INFRA_DIR/.env}"

ASSISTANT_URL="${ASSISTANT_URL:-http://127.0.0.1:18082}"
HUB_URL="${HUB_URL:-http://127.0.0.1:18081}"
GATEWAY_URL="${GATEWAY_URL:-http://127.0.0.1:4100}"
TASK_URL="${TASK_URL:-http://task.local.nodedc/}"
ENGINE_URL="${ENGINE_URL:-http://127.0.0.1:5300/}"

read_env() {
  file="$1"
  key="$2"
  if [ ! -f "$file" ]; then
    return 0
  fi
  awk -F= -v key="$key" '
    $0 ~ "^[[:space:]]*#" { next }
    $1 == key {
      value = substr($0, index($0, "=") + 1)
      gsub(/^[[:space:]]+|[[:space:]]+$/, "", value)
      gsub(/^"|"$/, "", value)
      print value
      exit
    }
  ' "$file"
}

http_status() {
  url="$1"
  curl -k -sS -o /dev/null -w "%{http_code}" --max-time 3 "$url" 2>/dev/null || true
}

http_body() {
  url="$1"
  curl -k -sS --max-time 3 "$url" 2>/dev/null || true
}

ok_status() {
  code="$1"
  case "$code" in
    2*|3*) printf true ;;
    *) printf false ;;
  esac
}

json_number() {
  key="$1"
  sed -n "s/.*\"$key\"[[:space:]]*:[[:space:]]*\\([0-9][0-9]*\\).*/\\1/p" | head -n 1
}

url_kind() {
  value="$1"
  case "$value" in
    "")
      printf missing
      ;;
    *127.0.0.1*|*localhost*|*host.docker.internal*|*".local.nodedc"*|*ai-workspace-hub*|*agent-gateway*)
      printf local
      ;;
    *100.[6-9][0-9].*|*100.1[01][0-9].*|*100.12[0-7].*|*.ts.net*)
      printf tailscale
      ;;
    *172.22.0.222*|*".nas.nodedc"*)
      printf synology-lan
      ;;
    *ai-hub.nodedc.ru*|*ops-agents.nodedc.ru*|*hub.nodedc.ru*|*ops.nodedc.ru*)
      printf prod-public
      ;;
    http://172.*|http://192.168.*|http://10.*)
      printf lan
      ;;
    *)
      printf custom
      ;;
  esac
}

bool_and() {
  left="$1"
  right="$2"
  if [ "$left" = true ] && [ "$right" = true ]; then
    printf true
  else
    printf false
  fi
}

assistant_code=$(http_status "$ASSISTANT_URL/healthz")
gateway_code=$(http_status "$GATEWAY_URL/readyz")
task_code=$(http_status "$TASK_URL")
engine_code=$(http_status "$ENGINE_URL")

assistant_ok=$(ok_status "$assistant_code")
gateway_ok=$(ok_status "$gateway_code")
task_ok=$(ok_status "$task_code")
engine_ok=$(ok_status "$engine_code")

profile=$(read_env "$PLATFORM_ENV" NODEDC_ENV || true)
case "$profile" in
  "") profile=local ;;
esac
hub_public=$(read_env "$PLATFORM_ENV" AI_WORKSPACE_HUB_PUBLIC_URL || true)
hub_internal=$(read_env "$PLATFORM_ENV" AI_WORKSPACE_HUB_INTERNAL_URL || true)
ops_entitlement=$(read_env "$PLATFORM_ENV" AI_WORKSPACE_OPS_ENTITLEMENT_URL || true)
action_relay_enabled=$(read_env "$PLATFORM_ENV" AI_WORKSPACE_ASSISTANT_ACTION_RELAY_ENABLED || true)
action_relay_id=$(read_env "$PLATFORM_ENV" AI_WORKSPACE_ASSISTANT_ACTION_RELAY_ID || true)
case "$action_relay_enabled" in
  1|true|TRUE|yes|YES|on|ON) action_relay_enabled=true ;;
  *) action_relay_enabled=false ;;
esac

hub_public_kind=$(url_kind "$hub_public")
hub_internal_kind=$(url_kind "$hub_internal")
ops_entitlement_kind=$(url_kind "$ops_entitlement")

local_hub_body=$(http_body "$HUB_URL/healthz")
local_hub_code=000
if [ -n "$local_hub_body" ]; then
  local_hub_code=$(http_status "$HUB_URL/healthz")
fi
local_hub_ok=$(ok_status "$local_hub_code")

relay_health_code=000
relay_health_ok=false
if [ -n "$hub_internal" ]; then
  relay_health_code=$(http_status "$(printf "%s" "$hub_internal" | sed 's#/*$##')/healthz")
  relay_health_ok=$(ok_status "$relay_health_code")
fi

agents_online=$(printf "%s" "$local_hub_body" | json_number agentsOnline)
case "$agents_online" in
  ""|*[!0-9]*) agents_online=0 ;;
esac

local_services_ok=$(bool_and "$assistant_ok" "$(bool_and "$gateway_ok" "$task_ok")")
worker_on_local_hub=false
if [ "$agents_online" -gt 0 ]; then
  worker_on_local_hub=true
fi

action_relay_configured=false
if [ "$action_relay_enabled" = true ] && [ -n "$action_relay_id" ]; then
  action_relay_configured=true
fi

hub_runtime_kind=custom
case "$hub_public_kind:$hub_internal_kind" in
  local:local|local:missing|missing:local) hub_runtime_kind=local ;;
  tailscale:local|tailscale:tailscale|tailscale:missing) hub_runtime_kind=tailscale ;;
  prod-public:prod-public|prod-public:missing|missing:prod-public) hub_runtime_kind=prod-public ;;
  *) hub_runtime_kind=mixed ;;
esac

classification=contract-only
if [ "$local_services_ok" = true ]; then
  if [ "$engine_ok" = true ] \
    && [ "$action_relay_configured" = true ] \
    && [ "$hub_runtime_kind" = prod-public ]; then
    classification=local-remote-worker-relay
  elif [ "$engine_ok" = true ] \
    && [ "$worker_on_local_hub" = true ] \
    && [ "$ops_entitlement_kind" = local ] \
    && [ "$hub_runtime_kind" = local ]; then
    classification=true-local-e2e
  elif [ "$engine_ok" = true ] \
    && [ "$worker_on_local_hub" = true ] \
    && { [ "$ops_entitlement_kind" = tailscale ] || [ "$hub_runtime_kind" = tailscale ]; }; then
    classification=tunnel-local-e2e
  elif [ "$hub_runtime_kind" = prod-public ] || [ "$worker_on_local_hub" = false ]; then
    classification=hybrid-prod-bridge
  else
    classification=local-ops-vertical
  fi
elif [ "$gateway_ok" = true ] && [ "$task_ok" = true ]; then
  classification=local-ops-vertical
elif [ "$task_ok" = true ] || [ "$engine_ok" = true ]; then
  classification=local-ui-only
fi

cat <<EOF
classification=$classification
profile=$profile
platform_env=$PLATFORM_ENV
assistant_health=$assistant_ok status=$assistant_code url=$ASSISTANT_URL/healthz
local_hub_health=$local_hub_ok status=$local_hub_code url=$HUB_URL/healthz agents_online=$agents_online
relay_health=$relay_health_ok status=$relay_health_code url=${hub_internal%/}/healthz
gateway_health=$gateway_ok status=$gateway_code url=$GATEWAY_URL/readyz
task_health=$task_ok status=$task_code url=$TASK_URL
engine_health=$engine_ok status=$engine_code url=$ENGINE_URL
hub_public_kind=$hub_public_kind
hub_internal_kind=$hub_internal_kind
hub_runtime_kind=$hub_runtime_kind
ops_entitlement_kind=$ops_entitlement_kind
assistant_action_relay_enabled=${action_relay_enabled:-missing}
assistant_action_relay_id=${action_relay_id:-missing}
assistant_action_relay_configured=$action_relay_configured
EOF

case "$classification" in
  true-local-e2e|tunnel-local-e2e)
    echo "meaning=local write-path e2e can be interpreted if the worker can reach returned MCP URLs"
    ;;
  local-remote-worker-relay)
    echo "meaning=local apps are the downstream target; deployed AI Hub is used only as remote worker relay and assistant actions route back to local Assistant"
    ;;
  hybrid-prod-bridge)
    echo "meaning=do not treat UI dialog as local write-path proof; topology is mixed or assistant action relay is not configured"
    ;;
  local-ops-vertical)
    echo "meaning=Gateway and Tasker local vertical checks are valid, but no live Codex worker e2e is proven"
    ;;
  local-ui-only)
    echo "meaning=local UI/proxy is reachable, but AI Workspace write path is not proven"
    ;;
  *)
    echo "meaning=contract tests only; no runtime topology is proven"
    ;;
esac

if [ "${REQUIRE_TRUE_E2E:-0}" = "1" ]; then
  case "$classification" in
    true-local-e2e|tunnel-local-e2e) exit 0 ;;
    *) exit 2 ;;
  esac
fi

if [ "${REQUIRE_LOCAL_REMOTE_WORKER_RELAY:-0}" = "1" ]; then
  case "$classification" in
    local-remote-worker-relay) exit 0 ;;
    *) exit 2 ;;
  esac
fi
