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

SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
PLATFORM_ROOT=$(CDPATH= cd -- "$SCRIPT_DIR/../.." && pwd)
AI_ASSISTANT_DIR="$PLATFORM_ROOT/services/ai-workspace-assistant"

OPS_GATEWAY_REPO="${OPS_GATEWAY_REPO:-$PLATFORM_ROOT/../../data/NODEDC_TASKMANAGER_CODEXAPI}"
ENGINE_REPO="${ENGINE_REPO:-$PLATFORM_ROOT/../NODEDC_ENGINE_INFRA}"

RUN_GATEWAY_SMOKE="${RUN_GATEWAY_SMOKE:-1}"
GATEWAY_DATABASE_URL="${GATEWAY_DATABASE_URL:-postgres://nodedc_agent_gateway:replace-with-local-postgres-password@localhost:54100/nodedc_agent_gateway}"
GATEWAY_INTERNAL_TOKEN="${GATEWAY_INTERNAL_TOKEN:-replace-with-gateway-internal-token}"
GATEWAY_RUN_TOKEN_TTL_SECONDS="${GATEWAY_RUN_TOKEN_TTL_SECONDS:-43200}"

passed=0
failed=0
skipped=0

section() {
  printf '\n== %s ==\n' "$1"
}

pass() {
  passed=$((passed + 1))
  printf 'PASS %s\n' "$1"
}

fail() {
  failed=$((failed + 1))
  printf 'FAIL %s\n' "$1" >&2
}

skip() {
  skipped=$((skipped + 1))
  printf 'SKIP %s\n' "$1"
}

run_in() {
  dir="$1"
  label="$2"
  command="$3"

  if [ ! -d "$dir" ]; then
    fail "$label (missing directory: $dir)"
    return 0
  fi

  printf '\n$ %s\n' "$label"
  set +e
  (cd "$dir" && sh -lc "$command")
  status=$?
  set -e

  if [ "$status" -eq 0 ]; then
    pass "$label"
  else
    fail "$label (exit $status)"
  fi
}

section "AI Workspace topology classification"
run_in "$PLATFORM_ROOT" \
  "Classify current topology" \
  "infra/scripts/check-ai-workspace-topology.sh"

section "Platform static and contract gates"
run_in "$PLATFORM_ROOT" \
  "Local test system contract gate" \
  "sh infra/scripts/check-local-test-system.sh"
run_in "$PLATFORM_ROOT" \
  "Platform shell syntax" \
  "sh -n infra/scripts/init-dev-env.sh infra/scripts/check-local-test-system.sh infra/scripts/check-ai-workspace-topology.sh infra/scripts/check-ai-workspace-config-contract.sh infra/scripts/check-ai-workspace-release-gates.sh infra/synology/check-ai-workspace-apply-plan.sh infra/synology/prepare-ai-workspace-env.sh infra/synology/deploy-current.sh infra/synology/verify-current-runtime.sh infra/synology/apply-current-runtime.sh infra/synology/backup-current.sh"
run_in "$PLATFORM_ROOT" \
  "AI Workspace config contract" \
  "infra/scripts/check-ai-workspace-config-contract.sh"
run_in "$AI_ASSISTANT_DIR" \
  "AI Workspace Assistant server syntax" \
  "node --check src/server.mjs"
run_in "$AI_ASSISTANT_DIR" \
  "AI Workspace run profile smoke" \
  "npm run smoke:run-profile"
run_in "$PLATFORM_ROOT" \
  "Platform diff hygiene" \
  "git diff --check"

section "Ops Gateway static and vertical gates"
if [ -d "$OPS_GATEWAY_REPO" ]; then
  run_in "$OPS_GATEWAY_REPO" \
    "Ops Gateway TypeScript check" \
    "npm run check"
  run_in "$OPS_GATEWAY_REPO" \
    "Ops Gateway build" \
    "npm run build"
  if [ "$RUN_GATEWAY_SMOKE" = "1" ]; then
    run_in "$OPS_GATEWAY_REPO" \
      "Ops Gateway smoke" \
      "DATABASE_URL='$GATEWAY_DATABASE_URL' NODEDC_AGENT_GATEWAY_INTERNAL_TOKEN='$GATEWAY_INTERNAL_TOKEN' NODEDC_AI_WORKSPACE_RUN_TOKEN_TTL_SECONDS='$GATEWAY_RUN_TOKEN_TTL_SECONDS' npm run smoke:gateway"
  else
    skip "Ops Gateway smoke (RUN_GATEWAY_SMOKE=$RUN_GATEWAY_SMOKE)"
  fi
  run_in "$OPS_GATEWAY_REPO" \
    "Ops Gateway diff hygiene" \
    "git diff --check"
else
  fail "Ops Gateway repo not found: $OPS_GATEWAY_REPO"
fi

section "Engine static gates"
if [ -d "$ENGINE_REPO" ]; then
  run_in "$ENGINE_REPO" \
    "Engine AI Workspace client syntax" \
    "node --check nodedc-source/server/aiWorkspace/assistantRegistryClient.js"
  run_in "$ENGINE_REPO" \
    "Engine diff hygiene" \
    "git diff --check"
else
  fail "Engine repo not found: $ENGINE_REPO"
fi

section "Summary"
printf 'passed=%s failed=%s skipped=%s\n' "$passed" "$failed" "$skipped"

if [ "$failed" -ne 0 ]; then
  exit 1
fi
