apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "langfuse.fullname" . }}-worker
  labels:
    {{- include "langfuse.labels" . | nindent 4 }}
spec:
  {{- if not .Values.langfuse.worker.hpa.enabled }}
  replicas: {{ .Values.langfuse.worker.replicas | default .Values.langfuse.replicas }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "langfuse.selectorLabels" . | nindent 6 }}
      app: worker
  template:
    metadata:
      {{- with .Values.langfuse.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "langfuse.selectorLabels" . | nindent 8 }}
        app: worker
    spec:
      {{- with (.Values.langfuse.worker.image.pullSecrets | default .Values.langfuse.image.pullSecrets) }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "langfuse.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.langfuse.podSecurityContext | nindent 8 }}
      {{- if .Values.langfuse.extraInitContainers }}
      initContainers:
        {{- toYaml .Values.langfuse.extraInitContainers | nindent 8 }}
      {{- end }}
      {{- if .Values.langfuse.extraVolumes }}
      volumes:
        {{- toYaml .Values.langfuse.extraVolumes | nindent 8 }}
      {{- end }}
      containers:
        {{- if .Values.langfuse.extraContainers }}
        {{- toYaml .Values.langfuse.extraContainers | nindent 8 }}
        {{- end }}
        - name: {{ .Chart.Name }}-worker
          securityContext:
            {{- toYaml .Values.langfuse.securityContext | nindent 12 }}
          image: "{{ .Values.langfuse.worker.image.repository }}:{{ coalesce .Values.langfuse.worker.image.tag .Values.langfuse.image.tag .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.langfuse.worker.image.pullPolicy | default .Values.langfuse.image.pullPolicy }}
          env:
            {{- include "langfuse.commonEnv" . | nindent 12 }}
            {{- if .Values.langfuse.additionalEnv }}
              {{- toYaml .Values.langfuse.additionalEnv | nindent 12 }}
            {{- end }}
          livenessProbe:
            httpGet:
              path: /api/health
              port: http
            initialDelaySeconds: {{ .Values.langfuse.worker.livenessProbe.initialDelaySeconds | default 20 }}
            periodSeconds: {{ .Values.langfuse.worker.livenessProbe.periodSeconds | default 10 }}
            timeoutSeconds: {{ .Values.langfuse.worker.livenessProbe.timeoutSeconds | default 5 }}
            successThreshold: {{ .Values.langfuse.worker.livenessProbe.successThreshold | default 1 }}
            failureThreshold: {{ .Values.langfuse.worker.livenessProbe.failureThreshold | default 5 }}
          resources:
            {{- toYaml (.Values.langfuse.worker.resources | default .Values.langfuse.resources) | nindent 12 }}
          {{- if .Values.langfuse.extraVolumeMounts }}
          volumeMounts:
            {{- toYaml .Values.langfuse.extraVolumeMounts | nindent 12 }}
          {{- end }}
      {{- with .Values.langfuse.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.langfuse.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.langfuse.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
