{{- if .Values.postgres.enabled }}
---
# Headless Service for StatefulSet
apiVersion: v1
kind: Service
metadata:
  name: postgres
  namespace: dating-app
  labels:
    app: postgres
spec:
  ports:
  - port: {{ .Values.postgres.service.port }}
    targetPort: {{ .Values.postgres.service.port }}
    name: postgres
  clusterIP: None  # Headless service for StatefulSet
  selector:
    app: postgres

---
# PostgreSQL StatefulSet
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
  namespace: dating-app
  labels:
    app: postgres
spec:
  serviceName: postgres
  replicas: {{ .Values.postgres.replicas }}
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      securityContext:
        fsGroup: 999
      containers:
      - name: postgres
        image: {{ .Values.postgres.image }}
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: {{ .Values.postgres.service.port }}
          name: postgres
        env:
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              name: postgres-credentials
              key: username
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: postgres-credentials
              key: password
        - name: POSTGRES_DB
          valueFrom:
            secretKeyRef:
              name: postgres-credentials
              key: database
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        resources:
          requests:
            memory: {{ .Values.postgres.resources.requests.memory }}
            cpu: {{ .Values.postgres.resources.requests.cpu }}
          limits:
            memory: {{ .Values.postgres.resources.limits.memory }}
            cpu: {{ .Values.postgres.resources.limits.cpu }}
        volumeMounts:
        - name: postgres-storage
          mountPath: /var/lib/postgresql/data
        - name: init-scripts
          mountPath: /docker-entrypoint-initdb.d
        livenessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - pg_isready -U $POSTGRES_USER
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 3
        readinessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - pg_isready -U $POSTGRES_USER
          initialDelaySeconds: 10
          periodSeconds: 5
          timeoutSeconds: 2
          failureThreshold: 3
      volumes:
      - name: init-scripts
        configMap:
          name: postgres-init-scripts
          defaultMode: 0755
  volumeClaimTemplates:
  - metadata:
      name: postgres-storage
    spec:
      accessModes:
        - ReadWriteOnce
      {{- if .Values.postgres.persistence.storageClass }}
      storageClassName: {{ .Values.postgres.persistence.storageClass }}
      {{- end }}
      resources:
        requests:
          storage: {{ .Values.postgres.persistence.size }}

{{- end }}
spec:
  type: {{ .Values.postgres.service.type | default "ClusterIP" }}
  selector:
    app: postgres
  ports:
  - port: {{ .Values.postgres.service.port }}
    targetPort: {{ .Values.postgres.service.port }}
    protocol: TCP
    name: postgres
{{- end }}
