{{- $cozyConfig := lookup "v1" "ConfigMap" "cozy-system" "cozystack" }}
{{- $clusterDomain := (index $cozyConfig.data "cluster-domain") | default "cozy.local" }}
{{- $passwords := dict }}
{{- range $user, $u := .Values.users }}
  {{- if $u.password }}
    {{- $_ := set $passwords $user $u.password }}
  {{- else if not (index $passwords $user) }}
    {{- $_ := set $passwords $user (randAlphaNum 16) }}
  {{- end }}
{{- end }}

{{- if .Values.users }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ .Release.Name }}-credentials
stringData:
  {{- range $user, $u := .Values.users }}
  {{ quote $user }}: {{ quote (index $passwords $user) }}
  {{- end }}
{{- end }}

---

apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata:
  name: {{ .Release.Name }}-system
spec:
  chart:
    spec:
      chart: cozy-nats
      reconcileStrategy: Revision
      sourceRef:
        kind: HelmRepository
        name: cozystack-system
        namespace: cozy-system
      version: '>= 0.0.0-0'
  interval: 1m0s
  timeout: 5m0s
  values:
    nats:
      podTemplate:
        merge:
          spec:
            containers:
              - name: nats
                image: nats:2.10.17-alpine
                {{- if .Values.resources }}
                resources: {{- include "cozy-lib.resources.sanitize" (list .Values.resources $) | nindent 22 }}
                {{- else if ne .Values.resourcesPreset "none" }}
                resources: {{- include "cozy-lib.resources.preset" (list .Values.resourcesPreset $) | nindent 22 }}
                {{- end }}
      fullnameOverride: {{ .Release.Name }}
      config:
        cluster:
          routeURLs:
            k8sClusterDomain: {{ $clusterDomain }}
        {{- if or (gt (len $passwords) 0) (gt (len .Values.config.merge) 0) }}
        merge:
          {{- if gt (len $passwords) 0 }}
          accounts:
            A:
              users:
                {{- range $username, $password := $passwords }}
                - user: "{{ $username }}"
                  password: "{{ $password }}"
                {{- end }}
          {{- end }}
          {{- if and .Values.config (hasKey .Values.config "merge") }}
          {{ toYaml .Values.config.merge | nindent 12 }}
          {{- end }}
        {{- end }}
        {{- if and .Values.config (hasKey .Values.config "resolver") }}
        resolver:
          {{ toYaml .Values.config.resolver | nindent 12 }}
        {{- end }}
        cluster:
          enabled: true
          replicas: {{ .Values.replicas }}
        monitor:
          enabled: true
        jetstream:
          enabled: true
          fileStore:
            enabled: {{ .Values.jetstream.enabled }}
            pvc:
              enabled: true
              size: {{ .Values.jetstream.size }}
              {{- with .Values.storageClass }}
              storageClassName: {{ . }}
              {{- end }}
      promExporter:
        enabled: true
        podMonitor:
          enabled: true
      {{- if .Values.external }}
      service:
        merge:
          spec:
            type: LoadBalancer
      {{- end }}
