{{- if gt (.Values.replicaCount | int) 1 }}
  {{- fail "replicaCount > 1 is not supported: WebSocket connection state is in-memory and not shared across replicas" }}
{{- end }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "supersync.fullname" . }}
  labels:
    {{- include "supersync.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  {{- if .Values.supersync.persistence.enabled }}
  strategy:
    type: Recreate
  {{- end }}
  selector:
    matchLabels:
      {{- include "supersync.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "supersync.labels" . | nindent 8 }}
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "supersync.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      initContainers:
        - name: migrate-db
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ['sh', '-c', 'sh scripts/migrate-deploy.sh']
          env:
            {{- if .Values.postgresql.enabled }}
            - name: POSTGRES_USER
              value: {{ .Values.postgresql.username | quote }}
            - name: POSTGRES_DB
              value: {{ .Values.postgresql.database | quote }}
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.postgresql.existingSecret | default (include "supersync.postgresql.fullname" .) }}
                  key: POSTGRES_PASSWORD
            - name: DATABASE_URL
              value: "postgresql://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@{{ include "supersync.postgresql.fullname" . }}:5432/$(POSTGRES_DB)"
            {{- else }}
            - name: DATABASE_URL
              {{- if .Values.externalDatabase.existingSecret }}
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.externalDatabase.existingSecret }}
                  key: DATABASE_URL
              {{- else }}
              value: {{ .Values.externalDatabase.url | quote }}
              {{- end }}
            {{- end }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
        - name: copy-public
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ['sh', '-c', 'cp -r /app/public/* /tmp/public/']
          volumeMounts:
            - name: public
              mountPath: /tmp/public
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.supersync.port }}
              protocol: TCP
          envFrom:
            - configMapRef:
                name: {{ include "supersync.fullname" . }}
          env:
            - name: JWT_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.supersync.existingSecret | default (include "supersync.fullname" .) }}
                  key: JWT_SECRET
            {{- if .Values.postgresql.enabled }}
            - name: POSTGRES_USER
              value: {{ .Values.postgresql.username | quote }}
            - name: POSTGRES_DB
              value: {{ .Values.postgresql.database | quote }}
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.postgresql.existingSecret | default (include "supersync.postgresql.fullname" .) }}
                  key: POSTGRES_PASSWORD
            - name: DATABASE_URL
              value: "postgresql://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@{{ include "supersync.postgresql.fullname" . }}:5432/$(POSTGRES_DB)"
            {{- else }}
            - name: DATABASE_URL
              {{- if .Values.externalDatabase.existingSecret }}
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.externalDatabase.existingSecret }}
                  key: DATABASE_URL
              {{- else }}
              value: {{ .Values.externalDatabase.url | quote }}
              {{- end }}
            {{- end }}
            {{- if .Values.supersync.smtp.enabled }}
            - name: SMTP_USER
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.supersync.smtp.existingSecret | default (include "supersync.fullname" .) }}
                  key: SMTP_USER
            - name: SMTP_PASS
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.supersync.smtp.existingSecret | default (include "supersync.fullname" .) }}
                  key: SMTP_PASS
            {{- end }}
          startupProbe:
            httpGet:
              path: /health
              port: http
            initialDelaySeconds: 10
            periodSeconds: 5
            failureThreshold: 30
          livenessProbe:
            httpGet:
              path: /health
              port: http
            periodSeconds: 30
            timeoutSeconds: 5
          readinessProbe:
            httpGet:
              path: /health
              port: http
            periodSeconds: 10
            timeoutSeconds: 5
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - name: tmp
              mountPath: /tmp
            - name: public
              mountPath: /app/public
            - name: data
              mountPath: /app/data
      volumes:
        - name: tmp
          emptyDir: {}
        - name: public
          emptyDir: {}
        - name: data
          {{- if .Values.supersync.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ include "supersync.fullname" . }}
          {{- else }}
          emptyDir: {}
          {{- 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 }}
