{{- $adminToken := "" }}
{{- if and (eq .Values.vaultwarden.admin.enabled true) (not .Values.vaultwarden.admin.existingSecret) }}
{{- $adminToken = .Values.vaultwarden.admin.token | default (randAlphaNum 48) | b64enc | quote }}
{{- end -}}

{{- $databaseUrl := "" }}
{{- if and (ne .Values.database.type "sqlite") (not .Values.database.existingSecret) }}
{{- $databaseUrl = required "External databases need either URL or existing secret" .Values.database.url | b64enc | quote }}
{{- end -}}

{{- $smtpUser := "" }}
{{- if and (eq .Values.vaultwarden.smtp.enabled true ) (not .Values.vaultwarden.smtp.existingSecret) (.Values.vaultwarden.smtp.user) }}
{{- $smtpUser = .Values.vaultwarden.smtp.user | b64enc | quote }}
{{- end -}}

{{- $yubicoClientId := "" }}
{{- if and (eq .Values.vaultwarden.yubico.enabled true ) (not .Values.vaultwarden.yubico.existingSecret) }}
{{- $yubicoClientId = required "Yubico Client ID required" .Values.vaultwarden.yubico.clientId | toString | b64enc | quote }}
{{- end -}}

{{- if or (ne $adminToken "") (ne $databaseUrl "") (ne $smtpUser "" ) (ne $yubicoClientId "") }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "vaultwarden.fullname" . }}
  labels:
    {{- include "vaultwarden.labels" . | nindent 4 }}
data:
  {{- if ne $adminToken "" }}
  admin-token: {{ $adminToken }}
  {{- end }}
  {{- if ne $databaseUrl "" }}
  database-url: {{ $databaseUrl }}
  {{- end }}
  {{- if ne $smtpUser "" }}
  smtp-user: {{ $smtpUser }}
  smtp-password: {{ required "Must specify SMTP password" .Values.vaultwarden.smtp.password | b64enc | quote }}
  {{- end }}
  {{- if ne $yubicoClientId "" }}
  yubico-client-id: {{ $yubicoClientId }}
  yubico-secret-key: {{ required "Yubico Secret Key required" .Values.vaultwarden.yubico.secretKey | b64enc | quote }}
  {{- end }}
{{- end }}