{{- if .Values.cnpg.cluster.enabled }}
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: {{ default (printf "%s-db" (include "sure.fullname" .)) .Values.cnpg.cluster.name }}
  labels:
    {{- include "sure.labels" . | nindent 4 }}
spec:
  instances: {{ .Values.cnpg.cluster.instances | default 1 }}
  {{- if .Values.cnpg.cluster.minSyncReplicas }}
  minSyncReplicas: {{ .Values.cnpg.cluster.minSyncReplicas }}
  {{- end }}
  {{- if .Values.cnpg.cluster.maxSyncReplicas }}
  maxSyncReplicas: {{ .Values.cnpg.cluster.maxSyncReplicas }}
  {{- end }}
  storage:
    size: {{ .Values.cnpg.cluster.storage.size | default "10Gi" }}
    {{ if .Values.cnpg.cluster.storage.storageClassName }}
    storageClass: {{ .Values.cnpg.cluster.storage.storageClassName }}
    {{ end }}
  {{ with .Values.cnpg.cluster.backup }}
  {{- $backup := deepCopy . }}
  {{- /* CNPG `spec.backup` does not support these historical/example keys */ -}}
  {{- $_ := unset $backup "ttl" -}}
  {{- if and (hasKey $backup "volumeSnapshot") (kindIs "map" $backup.volumeSnapshot) -}}
    {{- $_ := unset $backup.volumeSnapshot "enabled" -}}
  {{- end -}}

  {{- $method := (get $backup "method") -}}
  {{- if not $method -}}
    {{- if hasKey $backup "volumeSnapshot" -}}
      {{- $_ := set $backup "method" "volumeSnapshot" -}}
      {{- $method = "volumeSnapshot" -}}
    {{- else if hasKey $backup "barmanObjectStore" -}}
      {{- $_ := set $backup "method" "barmanObjectStore" -}}
      {{- $method = "barmanObjectStore" -}}
    {{- end -}}
  {{- end -}}

  {{- if not $method -}}
    {{- fail "cnpg.cluster.backup is set but no backup method could be inferred. Set cnpg.cluster.backup.method explicitly (e.g. volumeSnapshot or barmanObjectStore)." -}}
  {{- end -}}

  {{- if and (eq $method "volumeSnapshot") (not (hasKey $backup "volumeSnapshot")) -}}
    {{- fail "cnpg.cluster.backup.method=volumeSnapshot requires cnpg.cluster.backup.volumeSnapshot to be set" -}}
  {{- end -}}
  {{- if and (eq $method "barmanObjectStore") (not (hasKey $backup "barmanObjectStore")) -}}
    {{- fail "cnpg.cluster.backup.method=barmanObjectStore requires cnpg.cluster.backup.barmanObjectStore to be set" -}}
  {{- end -}}
  {{- if eq $method "volumeSnapshot" -}}
    {{- if kindIs "map" $backup.volumeSnapshot -}}
      {{- $_ := required "cnpg.cluster.backup.volumeSnapshot.className is required for volumeSnapshot backups" $backup.volumeSnapshot.className -}}
    {{- else -}}
      {{- fail "cnpg.cluster.backup.volumeSnapshot must be a map/object" -}}
    {{- end -}}
  {{- end -}}

  backup:
    {{- toYaml $backup | nindent 4 }}
  {{- end }}
  {{ with .Values.cnpg.cluster.plugins }}
  plugins:
    {{- toYaml . | nindent 4 }}
  {{ end }}
  {{- with .Values.cnpg.cluster.nodeSelector }}
  nodeSelector:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- with .Values.cnpg.cluster.affinity }}
  affinity:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- with .Values.cnpg.cluster.tolerations }}
  tolerations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- with .Values.cnpg.cluster.topologySpreadConstraints }}
  topologySpreadConstraints:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  bootstrap:
    initdb:
      database: {{ .Values.cnpg.cluster.appDatabase | default "sure" }}
      owner: {{ .Values.cnpg.cluster.appUser | default "sure" }}
      secret:
        name: {{ .Values.cnpg.cluster.existingSecret | default (default (printf "%s-db-app" (include "sure.fullname" .)) .Values.cnpg.cluster.secret.name) }}
      postInitSQL:
        - {{ printf "GRANT CONNECT ON DATABASE postgres TO \"%s\";" (.Values.cnpg.cluster.appUser | default "sure") | quote }}
        - {{ printf "ALTER ROLE \"%s\" CREATEDB;" (.Values.cnpg.cluster.appUser | default "sure") | quote }}
  {{- $secretName := .Values.cnpg.cluster.existingSecret | default (default (printf "%s-db-app" (include "sure.fullname" .)) .Values.cnpg.cluster.secret.name) }}
  {{- if not .Values.cnpg.cluster.existingSecret }}
  enableSuperuserAccess: false
  {{- end }}
  monitoring:
    enablePodMonitor: false
{{- end }}
---
{{- if and .Values.cnpg.cluster.enabled (and (not .Values.cnpg.cluster.existingSecret) .Values.cnpg.cluster.secret.enabled) }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ default (printf "%s-db-app" (include "sure.fullname" .)) .Values.cnpg.cluster.secret.name }}
  labels:
    {{- include "sure.labels" . | nindent 4 }}
type: Opaque
stringData:
  {{ .Values.cnpg.cluster.secret.usernameKey | default "username" }}: {{ .Values.cnpg.cluster.appUser | default "sure" }}
  {{ .Values.cnpg.cluster.secret.passwordKey | default "password" }}: {{ randAlphaNum 24 | quote }}
{{- end }}
