{{- if .Values.ui.enabled -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "unitycatalog.ui.fullname" . }}
  labels:
    {{- include "unitycatalog.ui.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.ui.deployment.replicaCount }}
  selector:
    matchLabels:
      {{- include "unitycatalog.ui.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.ui.deployment.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "unitycatalog.ui.labels" . | nindent 8 }}
        {{- with .Values.ui.deployment.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- with .Values.ui.deployment.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "unitycatalog.serviceAccountName" . }}
      {{- with .Values.ui.deployment.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      initContainers:
        - name: wait-for-server
          image: "{{ .Values.ui.deployment.initContainer.image.repository }}:{{ .Values.ui.deployment.initContainer.image.tag }}"
          imagePullPolicy: {{ .Values.ui.deployment.initContainer.image.pullPolicy }}
          command:
            - /bin/sh
            - -c
            - |-
              until nc -z {{ include "unitycatalog.server.fullname" . }} {{ .Values.server.service.port }}
              do
                echo "Waiting for server ({{ include "unitycatalog.server.fullname" . }}:{{ .Values.server.service.port }}) to be ready..."
                sleep 2
              done
      containers:
        - name: ui
          {{- with .Values.ui.deployment.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          image: "{{ .Values.ui.deployment.image.repository }}:{{ .Values.ui.deployment.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.ui.deployment.image.pullPolicy }}
          ports:
            - name: ui
              containerPort: {{ .Values.ui.deployment.port }}
              protocol: TCP
          command:
            - /bin/bash
            - -c
            - |-
              jq --arg proxy "{{ include "unitycatalog.server.apiEndpoint" . }}" '.proxy = $proxy' package.json > temp.json
              mv temp.json package.json
              yarn start
          env:
          {{- if .Values.ui.deployment.host }}
            - name: HOST
              value: "{{ .Values.ui.deployment.host }}"
          {{- end }}
          {{- if .Values.ui.deployment.disableHostCheck }}
            - name: DANGEROUSLY_DISABLE_HOST_CHECK
              value: "{{ .Values.ui.deployment.disableHostCheck }}"
          {{- end }}
          {{- if .Values.auth.enabled }}
            - name: REACT_APP_GOOGLE_AUTH_ENABLED
              value: "{{ eq .Values.auth.provider "google" }}"
            - name: REACT_APP_GOOGLE_CLIENT_ID
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.auth.clientSecretName }}
                  key: clientId
            - name: REACT_APP_OKTA_AUTH_ENABLED
              value: "{{ eq .Values.auth.provider "okta" }}"
            - name: REACT_APP_OKTA_DOMAIN
              value: "{{ .Values.auth.oktaDomain }}"
            - name: REACT_APP_OKTA_CLIENT_ID
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.auth.clientSecretName }}
                  key: clientId
            - name: REACT_APP_KEYCLOAK_AUTH_ENABLED
              value: "{{ eq .Values.auth.provider "keycloak" }}"
            - name: REACT_APP_KEYCLOAK_URL
              value: "{{ .Values.auth.authorizationUrl }}"
            - name: REACT_APP_KEYCLOAK_REALM_ID
              value: "{{ .Values.auth.keycloakRealmId }}"
            - name: REACT_APP_KEYCLOAK_CLIENT_ID
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.auth.clientSecretName }}
                  key: clientId
          {{- end }}
          {{- with .Values.ui.deployment.livenessProbe }}
          livenessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.ui.deployment.readinessProbe }}
          readinessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.ui.deployment.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.ui.deployment.volumeMounts }}
          volumeMounts:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      {{- with .Values.ui.deployment.volumes }}
      volumes:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.ui.deployment.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.ui.deployment.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.ui.deployment.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
{{- end }}