{{- 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.
*/ -}}
{{- /*
Convergence fix 2026-05-18 — Sovereign event bus wiring:

(1) Sovereign default of REDPANDA_BROKERS is now EMPTY (not the NATS URL
    poked into a Kafka env). The SME services' Kafka client cannot speak
    the NATS wire protocol; the previous default value of
    `nats-jetstream.nats-jetstream.svc.cluster.local:4222` made every
    franz-go dial fail at startup, which on the publish side silently
    dropped tenant.created / order.placed and blocked convergence end
    to end.
(2) NATS_URL is now exported as a separate ConfigMap key and wired into
    the tenant + billing Deployments (see tenant.yaml / billing.yaml).
    Services use the JetStream-native MultiPublisher (see
    core/services/shared/events/bridge.go) to write to
    `catalyst.<domain>.<event>` per ADR-0001 §6.
(3) Note also the NATS namespace: `bp-nats-jetstream` reconciles into
    namespace `nats-system` (see clusters/_template/bootstrap-kit/
    07-nats-jetstream.yaml), NOT `nats-jetstream`. Earlier defaults that
    pointed at `.nats-jetstream.svc` resolved to NXDOMAIN.

Catalyst-Zero (contabo, global.sovereignFQDN empty) keeps the legacy
Redpanda default so contabo's existing SME consumers keep functioning.
*/ -}}
{{- $defaultBrokers := "redpanda.talentmesh.svc.cluster.local:9092" -}}
{{- $defaultNATS := "" -}}
{{- if .Values.global.sovereignFQDN -}}
  {{- /* Sovereign default: no Redpanda exists in cluster — leave empty
       so the SME services skip the legacy Kafka transport entirely. */ -}}
  {{- $defaultBrokers = "" -}}
  {{- /* Sovereign default NATS URL. Correct namespace is `nats-system`
       (see header comment §3). */ -}}
  {{- $defaultNATS = "nats://nats-jetstream.nats-system.svc.cluster.local:4222" -}}
{{- end -}}
{{- $brokers := default $defaultBrokers (index (default (dict) .Values.smeServices.eventBus) "brokers") -}}
{{- $natsURL := default $defaultNATS (index (default (dict) .Values.smeServices.eventBus) "natsURL") -}}
{{- /*
TBD-A68 (issue #1994, 2026-05-19) — derive the marketplace host from the
per-Sovereign FQDN instead of hardcoding `sme.openova.io`. Pre-fix every
CORS_ORIGIN_* / PUBLIC_BASE_URL / CHECKOUT_*_URL key emitted
`https://sme.openova.io`, which on a franchised Sovereign (e.g.
omani.homes) made:
  - storefront fetches mis-match CORS preflight (mothership host
    advertised, real host `marketplace.omani.homes` rejected),
  - PUBLIC_BASE_URL render absolute links pointing at the mothership
    instead of the local sovereign,
  - Stripe / payment-success redirects bounce tenants OFF their
    Sovereign back to Catalyst-Zero.

Resolution order, longest-match wins:
  1. .Values.ingress.hosts.marketplace.host — explicit overlay value
     (e.g. `marketplace.omani.homes`). Already canonical, kept as-is.
  2. else fall back to `marketplace.<sovereignFQDN>` when sovereignFQDN
     is set (canonical franchised-Sovereign convention).
  3. else fall back to the legacy `sme.openova.io` so contabo's
     Catalyst-Zero render (sovereignFQDN unset, host unset) keeps
     working.

Admin storefront prefix `admin.` and console prefix `console.` mirror
the platform subdomain plan (catalyst/chart/values.yaml: ingress.hosts
keys). No other CORS rule lives off these prefixes today.
*/ -}}
{{- /* Marketplace + sibling-platform hosts.
       - $marketplaceHost — the buyer-facing storefront
       - $platformZone — the parent zone admin/console/portal live in
       Selection rules (longest-match wins):
         1. Operator pinned `.Values.ingress.hosts.marketplace.host` →
            keep that exact host; platform-zone = parent of the host
            (drop one label). Vanity / A-B-test setups can colocate
            siblings cleanly.
         2. global.sovereignFQDN set → marketplaceHost =
            `marketplace.<sov>`, platformZone = `<sov>`. Canonical
            franchised-Sovereign layout (admin.<sov> / console.<sov> /
            portal.<sov>).
         3. Neither set → preserve historical Catalyst-Zero layout
            (sme.openova.io for marketplace, admin.sme.openova.io for
            admin, etc.). Pre-A68 default — kept byte-identical so
            contabo's existing CORS / public URLs don't drift. */ -}}
{{- $marketplaceHost := default "" (((.Values.ingress).hosts).marketplace).host -}}
{{- $platformZone := "" -}}
{{- if $marketplaceHost -}}
  {{- if contains "." $marketplaceHost -}}
    {{- $platformZone = (index (splitn "." 2 $marketplaceHost) "_1") -}}
  {{- else -}}
    {{- $platformZone = $marketplaceHost -}}
  {{- end -}}
{{- else if .Values.global.sovereignFQDN -}}
  {{- $marketplaceHost = printf "marketplace.%s" .Values.global.sovereignFQDN -}}
  {{- $platformZone = .Values.global.sovereignFQDN -}}
{{- else -}}
  {{- /* Legacy Catalyst-Zero default — siblings share the
         `sme.openova.io` parent so admin.sme.openova.io etc. stay
         resolvable, matching pre-A68 emit. */ -}}
  {{- $marketplaceHost = "sme.openova.io" -}}
  {{- $platformZone = "sme.openova.io" -}}
{{- end -}}
{{- $marketplaceOrigin := printf "https://%s" $marketplaceHost -}}
{{- $adminOrigin := printf "https://admin.%s" $platformZone -}}
{{- $consoleOrigin := printf "https://console.%s" $platformZone -}}
{{- $portalOrigin := printf "https://portal.%s" $platformZone -}}
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
  # + 2026-05-18 convergence fix). On Sovereigns REDPANDA_BROKERS is
  # intentionally empty — the SME services treat empty as "skip the
  # legacy Kafka transport" (NATS-only mode).
  REDPANDA_BROKERS: {{ $brokers | quote }}
  # NATS JetStream URL — canonical event bus per ADR-0001 §6. Wired
  # into tenant + billing Deployments (and any future SME service
  # publishing `catalyst.<domain>.<event>` subjects via
  # events.MultiPublisher). Empty on Catalyst-Zero (no NATS yet);
  # populated on Sovereigns by the default above.
  NATS_URL: {{ $natsURL | 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: {{ $marketplaceOrigin | quote }}
  CORS_ORIGIN_ADMIN: {{ printf "%s,%s" $marketplaceOrigin $adminOrigin | quote }}
  CORS_ORIGIN_GATEWAY: {{ printf "%s,%s,%s,%s" $marketplaceOrigin $adminOrigin $consoleOrigin $portalOrigin | quote }}
  PUBLIC_BASE_URL: {{ $marketplaceOrigin | quote }}
  PUBLIC_API_BASE_URL: {{ printf "%s/api" $marketplaceOrigin | quote }}
  CNAME_TARGET: {{ $marketplaceHost | quote }}

  # ---- checkout redirect targets --------------------------------------------
  CHECKOUT_SUCCESS_URL: {{ printf "%s/checkout/success" $marketplaceOrigin | quote }}
  CHECKOUT_CANCEL_URL: {{ printf "%s/checkout/cancel" $marketplaceOrigin | quote }}
{{- end }}
