apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "cleveragents.fullname" . }}
  labels:
    {{- include "cleveragents.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "cleveragents.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "cleveragents.selectorLabels" . | nindent 8 }}
    spec:
      automountServiceAccountToken: false
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "cleveragents.serviceAccountName" . }}
      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 }}
          command: ["python", "-m", "cleveragents"]
          args:
            - "server"
            - "serve"
            - "--app"
            - "cleveragents.a2a.asgi:app"
            - "--host"
            - {{ .Values.server.host | quote }}
            - "--port"
            - {{ .Values.server.port | quote }}
            - "--workers"
            - {{ .Values.server.workers | quote }}
            - "--log-level"
            - {{ .Values.server.logLevel | quote }}
          ports:
            - name: http
              containerPort: {{ .Values.server.port }}
              protocol: TCP
          envFrom:
            - configMapRef:
                name: {{ include "cleveragents.fullname" . }}-config
          env:
            {{- if .Values.database.existingSecret }}
            - name: CLEVERAGENTS_DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.database.existingSecret }}
                  key: {{ .Values.database.existingSecretKey }}
            {{- else if .Values.database.url }}
            - name: CLEVERAGENTS_DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: {{ include "cleveragents.fullname" . }}-db
                  key: database-url
            {{- else }}
            {{- fail "database configuration is required: set database.existingSecret or database.url" }}
            {{- end }}
            {{- if .Values.redis.enabled }}
            - name: CLEVERAGENTS_REDIS_HOST
              value: {{ include "cleveragents.redisHost" . }}
            - name: CLEVERAGENTS_REDIS_PORT
              value: "6379"
            {{- if .Values.redis.auth.enabled }}
            {{- if .Values.redis.auth.existingSecret }}
            - name: CLEVERAGENTS_REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.redis.auth.existingSecret }}
                  key: {{ .Values.redis.auth.existingSecretPasswordKey }}
            {{- else if .Values.redis.auth.password }}
            - name: CLEVERAGENTS_REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "cleveragents.fullname" . }}-redis
                  key: redis-password
            {{- else }}
            - name: CLEVERAGENTS_REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-redis
                  key: redis-password
            {{- end }}
            {{- end }}
            {{- end }}
            {{- with .Values.extraEnv }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          livenessProbe:
            {{- toYaml .Values.livenessProbe | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.readinessProbe | nindent 12 }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - name: tmp
              mountPath: /tmp
            - name: app-data
              mountPath: /app/data
            {{- with .Values.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
      volumes:
        - name: tmp
          emptyDir:
            sizeLimit: 100Mi
        - name: app-data
          emptyDir:
            sizeLimit: 1Gi
        {{- with .Values.extraVolumes }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
