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

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
EXIT_CODE=0

run_step() {
  local label="$1"
  shift
  echo "==> $label"
  set +e
  "$@"
  local code=$?
  set -e
  if (( code > EXIT_CODE )); then
    EXIT_CODE=$code
  fi
}

run_step "Commerce schema" php "$ROOT/scripts/verify-commerce-schema.php"
run_step "Go-live env" php "$ROOT/scripts/verify-go-live-env.php"
run_step "Production surface" node "$ROOT/scripts/verify-production-surface.mjs"

if command -v node >/dev/null 2>&1 && [[ -f "$ROOT/scripts/release-gate.mjs" ]]; then
  run_step "Release gate (JSON)" node "$ROOT/scripts/release-gate.mjs"
fi

exit "$EXIT_CODE"
