{{- if .Values.server.enabled }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "postgresql.fullname" . }}
  labels:
    {{- include "postgresql.labels" . | nindent 4 }}
spec:
  serviceName: {{ include "postgresql.fullname" . }}
  replicas: 1
  selector:
    matchLabels:
      {{- include "postgresql.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "postgresql.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "postgresql.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: postgresql
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: postgres
              containerPort: 5432
              protocol: TCP
          env:
            - name: POSTGRES_USER
              value: {{ .Values.auth.username | quote }}
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "postgresql.secretName" . }}
                  key: {{ include "postgresql.secretKey" . }}
            - name: POSTGRES_DB
              value: {{ .Values.auth.database | quote }}
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            exec:
              command:
                - /bin/sh
                - -c
                - pg_isready -U {{ .Values.auth.username }}
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
            successThreshold: {{ .Values.livenessProbe.successThreshold }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            exec:
              command:
                - /bin/sh
                - -c
                - pg_isready -U {{ .Values.auth.username }}
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
          {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - name: data
              mountPath: /var/lib/postgresql/data
            - name: config
              mountPath: /etc/postgresql
            {{- if .Values.initScripts.enabled }}
            - name: init-scripts
              mountPath: /docker-entrypoint-initdb.d
            {{- end }}
            {{- if .Values.tls.enabled }}
            - name: tls-certs
              mountPath: /etc/postgresql/certs
              readOnly: true
            {{- end }}
        
        {{- if .Values.stunnel.enabled }}
        - name: stunnel
          image: "{{ .Values.stunnel.image.repository }}:{{ .Values.stunnel.image.tag }}"
          imagePullPolicy: {{ .Values.stunnel.image.pullPolicy }}
          ports:
            - name: stunnel
              containerPort: {{ .Values.stunnel.port }}
              protocol: TCP
          volumeMounts:
            - name: stunnel-config
              mountPath: /etc/stunnel/stunnel.conf
              subPath: stunnel.conf
            {{- if .Values.stunnel.certificatesSecret }}
            - name: stunnel-certs
              mountPath: /etc/stunnel/certs
              readOnly: true
            {{- end }}
        {{- end }}
        
        {{- if .Values.metrics.enabled }}
        - name: metrics
          image: "{{ .Values.metrics.image.repository }}:{{ .Values.metrics.image.tag }}"
          imagePullPolicy: {{ .Values.metrics.image.pullPolicy }}
          ports:
            - name: metrics
              containerPort: 9187
              protocol: TCP
          env:
            - name: DATA_SOURCE_NAME
              value: "postgresql://{{ .Values.auth.username }}:$(POSTGRES_PASSWORD)@localhost:5432/{{ .Values.auth.database }}?sslmode=disable"
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "postgresql.secretName" . }}
                  key: {{ include "postgresql.secretKey" . }}
          resources:
            {{- toYaml .Values.metrics.resources | nindent 12 }}
        {{- end }}
      
      volumes:
        - name: config
          configMap:
            name: {{ include "postgresql.fullname" . }}-config
        {{- if .Values.initScripts.enabled }}
        - name: init-scripts
          configMap:
            name: {{ include "postgresql.fullname" . }}-init-scripts
        {{- end }}
        {{- if .Values.tls.enabled }}
        - name: tls-certs
          secret:
            secretName: {{ .Values.tls.certificatesSecret }}
            defaultMode: 0600
        {{- end }}
        {{- if .Values.stunnel.enabled }}
        - name: stunnel-config
          configMap:
            name: {{ include "postgresql.fullname" . }}-stunnel-config
        {{- if .Values.stunnel.certificatesSecret }}
        - name: stunnel-certs
          secret:
            secretName: {{ .Values.stunnel.certificatesSecret }}
            defaultMode: 0600
        {{- end }}
        {{- end }}
      
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
  
  {{- if .Values.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: data
        {{- with .Values.persistence.annotations }}
        annotations:
          {{- toYaml . | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
          {{- range .Values.persistence.accessModes }}
          - {{ . | quote }}
          {{- end }}
        {{- if .Values.persistence.storageClass }}
        storageClassName: {{ .Values.persistence.storageClass | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.persistence.size | quote }}
        {{- with .Values.persistence.selector }}
        selector:
          {{- toYaml . | nindent 10 }}
        {{- end }}
  {{- else }}
        - name: data
          emptyDir: {}
  {{- end }}
{{- end }}
