---
# PVC for Backend media storage
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: backend-media-pvc
  namespace: dating-app
spec:
  accessModes:
    - ReadWriteMany
  {{- if .Values.backend.persistence.storageClass }}
  storageClassName: {{ .Values.backend.persistence.storageClass }}
  {{- end }}
  resources:
    requests:
      storage: {{ .Values.backend.persistence.size }}

---
# Backend Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend
  namespace: dating-app
  labels:
    app: backend
spec:
  replicas: {{ .Values.backend.replicas }}
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      initContainers:
      - name: db-init
        image: postgres:15-alpine
        command: ['sh', '-c', 'until pg_isready -h postgres.dating-app.svc.cluster.local -p {{ .Values.postgres.service.port }}; do echo waiting for db; sleep 2; done;']
      containers:
      - name: backend
        image: {{ .Values.backend.image.repository }}:{{ .Values.backend.image.tag }}
        imagePullPolicy: {{ .Values.backend.image.pullPolicy }}
        ports:
        - containerPort: {{ .Values.backend.service.targetPort }}
          name: http
        envFrom:
        - configMapRef:
            name: backend-config
        resources:
          requests:
            memory: {{ .Values.backend.resources.requests.memory }}
            cpu: {{ .Values.backend.resources.requests.cpu }}
          limits:
            memory: {{ .Values.backend.resources.limits.memory }}
            cpu: {{ .Values.backend.resources.limits.cpu }}
        volumeMounts:
        - name: media-storage
          mountPath: {{ .Values.backend.persistence.mountPath }}
        {{- if .Values.backend.probes.readiness.enabled }}
        readinessProbe:
          httpGet:
            path: {{ .Values.backend.probes.readiness.path }}
            port: {{ .Values.backend.service.targetPort }}
          initialDelaySeconds: {{ .Values.backend.probes.readiness.initialDelaySeconds }}
          periodSeconds: {{ .Values.backend.probes.readiness.periodSeconds }}
        {{- end }}
        {{- if .Values.backend.probes.liveness.enabled }}
        livenessProbe:
          httpGet:
            path: {{ .Values.backend.probes.liveness.path }}
            port: {{ .Values.backend.service.targetPort }}
          initialDelaySeconds: {{ .Values.backend.probes.liveness.initialDelaySeconds }}
          periodSeconds: {{ .Values.backend.probes.liveness.periodSeconds }}
        {{- end }}
      volumes:
      - name: media-storage
        persistentVolumeClaim:
          claimName: backend-media-pvc

---
# Backend Service
apiVersion: v1
kind: Service
metadata:
  name: backend
  namespace: dating-app
  labels:
    app: backend
spec:
  type: {{ .Values.backend.service.type }}
  selector:
    app: backend
  ports:
  - port: {{ .Values.backend.service.port }}
    targetPort: {{ .Values.backend.service.targetPort }}
    protocol: TCP
    name: http
