{{- $fullName := include "api.fullname" . -}}
{{- $name := "php" -}}
{{- $data := dict "name" $name "Chart" .Chart "Release" .Release "Values" .Values -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $fullName }}-{{ $name }}
  labels:
    {{- include "api.labels" $data | nindent 4 }}
spec:
  replicas: {{ .Values.php.replicaCount }}
  selector:
    matchLabels:
      {{- include "api.selectorLabels" $data | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "api.selectorLabels" $data | nindent 8 }}
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "api.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      initContainers:
        - name: init-php
          image: "{{ .Values.php.image.repository }}:{{ .Values.php.image.tag | default .Chart.AppVersion }}"
          command: ['/bin/bash', '-c']
          args: ['bin/console doctrine:migrations:migrate --no-interaction']
          env:
            - name: APP_DEBUG
              valueFrom:
                configMapKeyRef:
                  name: {{ $fullName }}
                  key: debug
            - name: APP_ENV
              valueFrom:
                configMapKeyRef:
                  name: {{ $fullName }}
                  key: env
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: {{ $fullName }}
                  key: database-url
      containers:
        - name: {{ .Chart.Name }}-{{ $name }}
          image: "{{ .Values.php.image.repository }}:{{ .Values.php.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.php.image.pullPolicy }}
          env:
            - name: TRUSTED_HOSTS
              valueFrom:
                configMapKeyRef:
                  name: {{ $fullName }}
                  key: trusted-hosts
            - name: TRUSTED_PROXIES
              valueFrom:
                configMapKeyRef:
                  name: {{ $fullName }}
                  key: trusted-proxies
            - name: APP_ENV
              valueFrom:
                configMapKeyRef:
                  name: {{ $fullName }}
                  key: env
            - name: APP_DEBUG
              valueFrom:
                configMapKeyRef:
                  name: {{ $fullName }}
                  key: debug
            - name: CORS_ALLOW_ORIGIN
              valueFrom:
                configMapKeyRef:
                  name: {{ $fullName }}
                  key: cors-allow-origin
            - name: VARNISH_URL
              valueFrom:
                configMapKeyRef:
                  name: {{ $fullName }}
                  key: varnish-url
            - name: APP_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ $fullName }}
                  key: secret
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: {{ $fullName }}
                  key: database-url
            - name: MERCURE_PUBLISH_URL
              valueFrom:
                configMapKeyRef:
                  name: {{ $fullName }}
                  key: mercure-publish-url
            - name: MERCURE_SUBSCRIBE_URL
              valueFrom:
                configMapKeyRef:
                  name: {{ $fullName }}
                  key: mercure-subscribe-url
            - name: MERCURE_JWT_TOKEN
              valueFrom:
                secretKeyRef:
                  name: {{ $fullName }}
                  key: mercure-jwt-token
          ports:
            - containerPort: 9000
          readinessProbe:
            tcpSocket:
              port: 9000
            initialDelaySeconds: 120
            periodSeconds: 3
          livenessProbe:
            tcpSocket:
              port: 9000
            initialDelaySeconds: 120
            periodSeconds: 3
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
