apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "cognithor.fullname" . }}
  labels:
    {{- include "cognithor.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "cognithor.selectorLabels" . | nindent 6 }}
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "cognithor.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if and .Values.ollama.enabled .Values.ollama.gpu.enabled }}
      nodeSelector:
        nvidia.com/gpu.present: "true"
        {{- with .Values.nodeSelector }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- else }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.ollama.enabled }}
      initContainers:
        - name: pull-models
          image: {{ .Values.ollama.image }}
          command:
            - /bin/sh
            - -c
            - |
              # Start ollama server in background
              ollama serve &
              sleep 5
              {{- range .Values.ollama.models }}
              echo "Pulling model {{ . }}..."
              ollama pull {{ . }}
              {{- end }}
              echo "All models pulled successfully."
              # Stop the background server
              kill %1 2>/dev/null || true
          volumeMounts:
            - name: ollama-data
              mountPath: /root/.ollama
          resources:
            {{- toYaml .Values.ollama.resources | nindent 12 }}
      {{- end }}
      containers:
        - name: cognithor
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- with .Values.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          args:
            - "--no-cli"
            - "--api-port"
            - "{{ .Values.service.port }}"
            - "--config"
            - "/etc/cognithor/config.yaml"
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          env:
            - name: JARVIS_HOME
              value: /data/.jarvis
            {{- if .Values.ollama.enabled }}
            - name: OLLAMA_HOST
              value: {{ include "cognithor.ollamaUrl" . | quote }}
            {{- end }}
          livenessProbe:
            httpGet:
              path: {{ .Values.probes.liveness.path }}
              port: http
            initialDelaySeconds: {{ .Values.probes.liveness.initialDelaySeconds }}
            periodSeconds: {{ .Values.probes.liveness.periodSeconds }}
          readinessProbe:
            httpGet:
              path: {{ .Values.probes.readiness.path }}
              port: http
            initialDelaySeconds: {{ .Values.probes.readiness.initialDelaySeconds }}
            periodSeconds: {{ .Values.probes.readiness.periodSeconds }}
          startupProbe:
            httpGet:
              path: {{ .Values.probes.startup.path }}
              port: http
            initialDelaySeconds: {{ .Values.probes.startup.initialDelaySeconds }}
            failureThreshold: {{ .Values.probes.startup.failureThreshold }}
            periodSeconds: {{ .Values.probes.startup.periodSeconds }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - name: config
              mountPath: /etc/cognithor
              readOnly: true
            {{- if .Values.persistence.data.enabled }}
            - name: data
              mountPath: /data
            {{- end }}
        {{- if .Values.ollama.enabled }}
        - name: ollama
          image: {{ .Values.ollama.image }}
          ports:
            - name: ollama
              containerPort: 11434
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: ollama
            initialDelaySeconds: 10
            periodSeconds: 30
          readinessProbe:
            httpGet:
              path: /
              port: ollama
            initialDelaySeconds: 5
            periodSeconds: 10
          resources:
            {{- toYaml .Values.ollama.resources | nindent 12 }}
          {{- if .Values.ollama.gpu.enabled }}
          env:
            - name: NVIDIA_VISIBLE_DEVICES
              value: all
          {{- end }}
          volumeMounts:
            - name: ollama-data
              mountPath: /root/.ollama
          {{- if .Values.ollama.gpu.enabled }}
            {{- if gt (int .Values.ollama.gpu.count) 0 }}
          resources:
            limits:
              nvidia.com/gpu: {{ .Values.ollama.gpu.count }}
            {{- end }}
          {{- end }}
        {{- end }}
      volumes:
        - name: config
          configMap:
            name: {{ include "cognithor.fullname" . }}-config
        {{- if .Values.persistence.data.enabled }}
        - name: data
          persistentVolumeClaim:
            claimName: {{ include "cognithor.fullname" . }}-data
        {{- else }}
        - name: data
          emptyDir: {}
        {{- end }}
        {{- if .Values.ollama.enabled }}
        {{- if .Values.persistence.ollama.enabled }}
        - name: ollama-data
          persistentVolumeClaim:
            claimName: {{ include "cognithor.fullname" . }}-ollama
        {{- else }}
        - name: ollama-data
          emptyDir: {}
        {{- end }}
        {{- end }}
