apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "backend.fullname" . }}
  labels:
    {{- include "backend.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "backend.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "backend.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "backend.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: PORT
              value: "{{ .Values.port }}"
            - name: MONGODB_URL
              value: "mongodb://{{ .Values.mongodb.fullnameOverride}}:{{ .Values.mongodb.service.port }}"
            - name: NODE_ENV
              value: "production"
            - name: JWT_SECRET
              value: "secretz"
          ports:
            - name: primary
              containerPort: {{ .Values.port }}
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /health
              port: primary
          readinessProbe:
            httpGet:
              path: /health
              port: primary
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
