apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "rise.fullname" . }}-config
  labels:
    {{- include "rise.labels" . | nindent 4 }}
data:
  # A "production.yaml" already exists in the Rise image, so we use "local.yaml" only to override production
  # defaults.
  local.yaml: |
{{- $config := deepCopy .Values.config }}
{{- if eq .Values.logs.backend "loki" }}
{{- $lokiUrl := .Values.logs.loki.externalUrl | default (printf "http://%s-loki:3100" .Release.Name) }}
{{- $deploymentLogs := dict "type" "loki" "url" $lokiUrl }}
{{- with .Values.logs.loki.tenantId }}
{{- $_ := set $deploymentLogs "tenant_id" . }}
{{- end }}
{{- if .Values.logs.loki.bearerTokenSecret.name }}
{{- $_ := set $deploymentLogs "bearer_token_env" "RISE_LOKI_BEARER_TOKEN" }}
{{- end }}
{{- $retentionHint := .Values.logs.loki.retentionHint | default "" }}
{{- if and (not $retentionHint) .Values.logs.loki.enabled }}
{{- /* Only inherit the bundled Loki retention when we actually installed it.
       For an external Loki, the operator should set retentionHint explicitly.
       Use `default ""` so a user who overrides limits_config without
       retention_period yields an empty string (omitted below), not null. */}}
{{- $retentionHint = dig "loki" "limits_config" "retention_period" "" .Values.loki }}
{{- end }}
{{- if $retentionHint }}
{{- $_ := set $deploymentLogs "retention_hint" $retentionHint }}
{{- end }}
{{- $_ := set $deploymentLogs "timeout_secs" (.Values.logs.loki.timeoutSeconds | int) }}
{{- $_ := set $deploymentLogs "labels" (dict
    "project" .Values.logs.loki.labels.project
    "deployment_id" .Values.logs.loki.labels.deploymentId
) }}
{{- $_ := set $config "deployment_logs" $deploymentLogs }}
{{- else }}
{{- $_ := set $config "deployment_logs" (dict "type" "kubernetes") }}
{{- end }}
{{ $config | toYaml | nindent 4 }}
