apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}-{{ .Values.backend.name }}
  namespace: {{ .Values.global.namespace }}
  labels:
    app: {{ .Release.Name }}-{{ .Values.backend.name }}
    component: backend
spec:
  replicas: {{ .Values.backend.replicaCount }}
  selector:
    matchLabels:
      app: {{ .Release.Name }}-{{ .Values.backend.name }}
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}-{{ .Values.backend.name }}
        component: backend
    spec:
      initContainers:
        - name: db-migration
          image: postgres:16-alpine
          command:
            - /bin/sh
            - -c
            - |
              echo "Waiting for database to be ready..."
              until pg_isready -h $DB_HOST -U $DB_USER; do
                echo "Database not ready, waiting..."
                sleep 2
              done
              echo "Database is ready, running migration..."
              PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -U $DB_USER -d $DB_NAME -f /migration/migrate.sql
              echo "Migration completed successfully"
          env:
            - name: DB_HOST
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-db-credentials
                  key: DB_HOST
            - name: DB_PORT
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-db-credentials
                  key: DB_PORT
            - name: DB_NAME
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-db-credentials
                  key: DB_NAME
            - name: DB_USER
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-db-credentials
                  key: DB_USER
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-db-credentials
                  key: DB_PASSWORD
          volumeMounts:
            - name: migration-script
              mountPath: /migration
      containers:
        - name: {{ .Values.backend.name }}
          image: "{{ .Values.backend.image.repository }}:{{ .Values.backend.image.tag }}"
          imagePullPolicy: {{ .Values.backend.image.pullPolicy }}
          ports:
            - containerPort: {{ .Values.backend.service.targetPort }}
              name: http
              protocol: TCP
          env:
            {{- if .Values.backend.env }}
            {{- range $key, $value := .Values.backend.env }}
            - name: {{ $key }}
              value: {{ $value | quote }}
            {{- end }}
            {{- end }}
          envFrom:
            - secretRef:
                name: {{ .Release.Name }}-db-credentials
            - secretRef:
                name: {{ .Release.Name }}-app-secrets
          startupProbe:
            httpGet:
              path: /docs
              port: http
            initialDelaySeconds: 15
            periodSeconds: 5
            timeoutSeconds: 3
            failureThreshold: 30
          livenessProbe:
            httpGet:
              path: /docs
              port: http
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /docs
              port: http
            initialDelaySeconds: 10
            periodSeconds: 5
            timeoutSeconds: 3
            failureThreshold: 2
          resources:
            requests:
              cpu: {{ .Values.backend.resources.requests.cpu }}
              memory: {{ .Values.backend.resources.requests.memory }}
            limits:
              cpu: {{ .Values.backend.resources.limits.cpu }}
              memory: {{ .Values.backend.resources.limits.memory }}
      volumes:
        - name: migration-script
          configMap:
            name: {{ .Release.Name }}-db-migration

