{{- if and (.Values.authEnabled) (not .Values.existingPasswordSecret) -}}

{{ $password := randAlphaNum 10 | b64enc | quote }}

{{- if .Values.neo4jPassword }}
{{ $password = .Values.neo4jPassword | b64enc | quote }}
{{- end -}}

{{- $name := include "neo4j.secrets.fullname" . }}
{{- $secret := (lookup "v1" "Secret" .Release.Namespace $name) }}
{{- if $secret }}
{{ $password = index $secret.data "neo4j-password" }}
{{- end -}}

apiVersion: v1
kind: Secret
metadata:
  name: {{ $name }}
  labels:
    app.kubernetes.io/name: {{ template "neo4j.name" . }}
    helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
    app.kubernetes.io/instance: {{ .Release.Name | quote }}
type: Opaque
data:
  neo4j-password: {{ $password }}
{{- end -}}
