{{- if and .Values.ingress .Values.ingress.marketplace .Values.ingress.marketplace.enabled -}}
{{- /*
Event-bus default — issue #942 (2026-05-05).

On Catalyst-Zero (contabo, global.sovereignFQDN empty) the SME services
historically point at a cross-namespace Redpanda in `talentmesh` (migration
tracked in #68). On franchised Sovereigns the talentmesh namespace does
not exist and the OpenOva architecture uses NATS JetStream as the only
local bus per ADR-0001 — slot 09 ships bp-nats-jetstream into namespace
`nats-jetstream`. Pre-#942 the value was hardcoded to the talentmesh
Service, which made every SME service crashloop on a fresh Sovereign at
startup with `lookup redpanda.talentmesh.svc.cluster.local: no such host`,
blocking alice signup gate 3 (tenant ready).

Per Inviolable Principle #4 (never hardcode), the broker target is
data-driven via .Values.smeServices.eventBus.brokers. Defaults differ per
topology so the contabo + Sovereign render paths both work out of the box:

  - Sovereign install (global.sovereignFQDN non-empty) defaults to the
    in-cluster NATS JetStream Service.
  - Catalyst-Zero install (global.sovereignFQDN empty) defaults to the
    legacy Redpanda Service in talentmesh so contabo keeps working.

Per-Sovereign overlays MAY override either default to wire SME services
at any NATS-protocol or Kafka-protocol broker without forking the chart.

The ConfigMap key name stays REDPANDA_BROKERS for backwards-compatibility
with existing SME service Go env wiring (the env name is historical; the
value is whatever broker is actually wired). A future ticket may rename
the env to EVENT_BUS_BROKERS once every SME service binary reads the new
name; that rename can ship without re-touching this template because the
key name is the only thing this template promises to its consumers.
*/ -}}
{{- $defaultBrokers := "redpanda.talentmesh.svc.cluster.local:9092" -}}
{{- if .Values.global.sovereignFQDN -}}
  {{- $defaultBrokers = "nats-jetstream.nats-jetstream.svc.cluster.local:4222" -}}
{{- end -}}
{{- $brokers := default $defaultBrokers (index (default (dict) .Values.smeServices.eventBus) "brokers") -}}
apiVersion: v1
kind: ConfigMap
metadata:
  name: sme-services-config
  namespace: sme
  annotations:
    description: |
      Central source of truth for SME service URLs, DB URIs, and external
      dependencies. All SME Deployments reference keys here via
      envFrom/valueFrom.configMapKeyRef — see issue #87.

      Forking this manifest to a new environment (staging, other clusters)
      should require editing only THIS file, not every service YAML.
data:
  # ---- cluster-internal service endpoints -----------------------------------
  # The SME microservice mesh. All services run in the `sme` namespace and
  # expose their own ports. Gateway fan-out uses these URLs; services call
  # each other directly using these too.
  AUTH_URL: "http://auth.sme.svc.cluster.local:8081"
  CATALOG_URL: "http://catalog.sme.svc.cluster.local:8082"
  TENANT_URL: "http://tenant.sme.svc.cluster.local:8083"
  PROVISIONING_URL: "http://provisioning.sme.svc.cluster.local:8084"
  BILLING_URL: "http://billing.sme.svc.cluster.local:8085"
  DOMAIN_URL: "http://domain.sme.svc.cluster.local:8086"
  NOTIFICATION_URL: "http://notification.sme.svc.cluster.local:8087"

  # ---- shared backing-store endpoints ---------------------------------------
  # FerretDB (MongoDB-wire) — Catalyst-curated Deployment in `sme` ns
  # (see ferretdb.yaml, issue #861). Operator-overridable via
  # .Values.smeServices.ferretdb.{host,port} so a per-Sovereign overlay
  # MAY swap to an external MongoDB endpoint without forking the chart.
  MONGODB_URI: "mongodb://{{ .Values.smeServices.ferretdb.host | default "ferretdb.sme.svc.cluster.local" }}:{{ .Values.smeServices.ferretdb.port | default 27017 }}"
  # Valkey — bp-valkey 1.0.0 deploys to `valkey` namespace (slot 17,
  # see 17-valkey.yaml). Bitnami valkey 5.5.1 with architecture:replication
  # renders Services `valkey-primary` (read/write), `valkey-replicas`,
  # `valkey-headless` — NO plain `valkey` Service exists. Default points
  # at the primary; per-Sovereign overlays MAY override via
  # .Values.smeServices.valkey.host (e.g. for HA replicas read-only or
  # to swap to an external Redis-compatible cache). Cross-ns reachability
  # is granted by templates/sme-services/valkey-cross-ns-policy.yaml.
  VALKEY_ADDR: "{{ .Values.smeServices.valkey.host | default "valkey-primary.valkey.svc.cluster.local" }}:{{ .Values.smeServices.valkey.port | default 6379 }}"
  POSTGRES_HOST: "{{ .Values.smePostgres.cluster.name | default "sme-pg" }}-rw.{{ .Values.smePostgres.cluster.namespace | default "sme" }}.svc.cluster.local"
  POSTGRES_PORT: "{{ .Values.smeServices.ferretdb.postgresPort | default 5432 }}"

  # ---- event bus ------------------------------------------------------------
  # See header comment for the topology-aware default selection (issue #942).
  REDPANDA_BROKERS: {{ $brokers | quote }}
  # Protocol hint for SME services that want to switch wire formats per
  # broker without re-reading the URL — `kafka` for Redpanda (legacy
  # contabo), `nats` for NATS JetStream (Sovereign default). Operator-
  # overridable via .Values.smeServices.eventBus.protocol.
  EVENT_BUS_PROTOCOL: {{ default (ternary "nats" "kafka" (ne (toString .Values.global.sovereignFQDN) "")) (index (default (dict) .Values.smeServices.eventBus) "protocol") | quote }}

  # ---- public / CORS endpoints ----------------------------------------------
  CORS_ORIGIN_PUBLIC: "https://sme.openova.io"
  CORS_ORIGIN_ADMIN: "https://sme.openova.io,https://admin.sme.openova.io"
  CORS_ORIGIN_GATEWAY: "https://sme.openova.io,https://admin.sme.openova.io,https://console.sme.openova.io,https://portal.sme.openova.io"
  PUBLIC_BASE_URL: "https://sme.openova.io"
  PUBLIC_API_BASE_URL: "https://sme.openova.io/api"
  CNAME_TARGET: "sme.openova.io"

  # ---- checkout redirect targets --------------------------------------------
  CHECKOUT_SUCCESS_URL: "https://sme.openova.io/checkout/success"
  CHECKOUT_CANCEL_URL: "https://sme.openova.io/checkout/cancel"
{{- end }}
