apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "fhirflare-ig-toolkit.fullname" . }}
  labels:
    {{- include "fhirflare-ig-toolkit.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount | default 1 }}
  selector:
    matchLabels:
      {{- include "fhirflare-ig-toolkit.selectorLabels" . | nindent 6 }}
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        {{- include "fhirflare-ig-toolkit.selectorLabels" . | nindent 8 }}
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      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 }}
          args: ["supervisord", "-c", "/etc/supervisord.conf"]
          env:
            - name: APP_BASE_URL
              value: {{ .Values.config.appBaseUrl | default "http://localhost:5000" | quote }}
            - name: APP_MODE
              value: {{ .Values.config.appMode | default "lite" | quote }}
            - name: FLASK_APP
              value: {{ .Values.config.flaskApp | default "app.py" | quote }}
            - name: FLASK_ENV
              value: {{ .Values.config.flaskEnv | default "development" | quote }}
            - name: HAPI_FHIR_URL
              value: {{ .Values.config.externalHapiServerUrl | default "http://external-hapi-fhir:8080/fhir" | quote }}
            - name: NODE_PATH
              value: {{ .Values.config.nodePath | default "/usr/lib/node_modules" | quote }}
            - name: TMPDIR
              value: "/tmp-dir"
          ports:
            - name: http
              containerPort: {{ .Values.service.port | default 5000 }}
              protocol: TCP
          volumeMounts:
            - name: logs
              mountPath: /app/logs
            - name: tmp-dir
              mountPath: /tmp-dir
          {{- with .Values.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.livenessProbe }}
          livenessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.readinessProbe }}
          readinessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      volumes:
        - name: logs
          emptyDir: {}
        - name: tmp-dir
          emptyDir: {}
      # Always require Intel 64-bit architecture nodes
      nodeSelector:
        kubernetes.io/arch: amd64
      {{- with .Values.nodeSelector }}
      # Merge with user-defined nodeSelectors if any
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}