apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "barycenter.fullname" . }}
  labels:
    {{- include "barycenter.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  strategy:
    type: Recreate  # Use Recreate for SQLite (single writer)
  selector:
    matchLabels:
      {{- include "barycenter.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- if and .Values.config.authz.enabled (not .Values.authz.existingConfigMap) .Values.authz.policies }}
        checksum/authz-policies: {{ include (print $.Template.BasePath "/authz-policies-configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "barycenter.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "barycenter.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      {{- if .Values.userSync.enabled }}
      initContainers:
      - name: user-sync
        securityContext:
          {{- toYaml .Values.securityContext | nindent 12 }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        command:
          - barycenter
          - -c
          - /app/config/config.toml
          - sync-users
          - --file
          - /secrets/{{ .Values.userSync.secretKey }}
        env:
        - name: RUST_LOG
          value: "info"
        {{- if .Values.userSync.env }}
        {{- toYaml .Values.userSync.env | nindent 8 }}
        {{- end }}
        volumeMounts:
        - name: config
          mountPath: /app/config
          readOnly: true
        {{- if .Values.persistence.enabled }}
        - name: data
          mountPath: /app/data
        {{- end }}
        - name: user-sync-secret
          mountPath: /secrets
          readOnly: true
        resources:
          {{- toYaml .Values.userSync.resources | nindent 10 }}
      {{- end }}
      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: ["-c", "/app/config/config.toml"]
        ports:
        - name: http
          containerPort: {{ .Values.config.server.port }}
          protocol: TCP
        {{- if .Values.config.authz.enabled }}
        - name: authz
          containerPort: {{ .Values.config.authz.port | default (add .Values.config.server.port 2) }}
          protocol: TCP
        {{- end }}
        {{- if .Values.env }}
        env:
          {{- toYaml .Values.env | nindent 8 }}
        {{- end }}
        volumeMounts:
        - name: config
          mountPath: /app/config
          readOnly: true
        {{- if .Values.persistence.enabled }}
        - name: data
          mountPath: /app/data
        {{- end }}
        {{- if .Values.config.authz.enabled }}
        - name: authz-policies
          mountPath: {{ .Values.config.authz.policiesDir }}
          readOnly: true
        {{- end }}
        livenessProbe:
          {{- toYaml .Values.livenessProbe | nindent 10 }}
        readinessProbe:
          {{- toYaml .Values.readinessProbe | nindent 10 }}
        resources:
          {{- toYaml .Values.resources | nindent 10 }}
      volumes:
      - name: config
        configMap:
          name: {{ include "barycenter.fullname" . }}
      {{- if .Values.persistence.enabled }}
      - name: data
        persistentVolumeClaim:
          claimName: {{ include "barycenter.fullname" . }}-data
      {{- else }}
      - name: data
        emptyDir: {}
      {{- end }}
      {{- if .Values.userSync.enabled }}
      - name: user-sync-secret
        secret:
          secretName: {{ .Values.userSync.existingSecret }}
      {{- end }}
      {{- if .Values.config.authz.enabled }}
      - name: authz-policies
        configMap:
          name: {{ if .Values.authz.existingConfigMap }}{{ .Values.authz.existingConfigMap }}{{ else }}{{ include "barycenter.fullname" . }}-authz-policies{{ end }}
      {{- 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 }}
