{{- if .Values.s2.deploy }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "trigger-v4.fullname" . }}-s2-spec
  labels:
    {{- $component := "s2" }}
    {{- include "trigger-v4.componentLabels" (dict "Chart" .Chart "Release" .Release "Values" .Values "component" $component) | nindent 4 }}
data:
  s2-spec.json: |
    {
      "$schema": "https://raw.githubusercontent.com/s2-streamstore/s2/main/cli/schema.json",
      "basins": [
        {
          "name": {{ .Values.s2.basin | quote }},
          "config": {
            "create_stream_on_append": true,
            "create_stream_on_read": true
          }
        }
      ]
    }
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "trigger-v4.fullname" . }}-s2
  labels:
    {{- $component := "s2" }}
    {{- include "trigger-v4.componentLabels" (dict "Chart" .Chart "Release" .Release "Values" .Values "component" $component) | nindent 4 }}
spec:
  replicas: 1
  serviceName: {{ include "trigger-v4.fullname" . }}-s2
  selector:
    matchLabels:
      {{- include "trigger-v4.componentSelectorLabels" (dict "Chart" .Chart "Release" .Release "Values" .Values "component" $component) | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/spec: {{ .Values.s2.basin | sha256sum }}
        {{- with .Values.s2.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "trigger-v4.componentSelectorLabels" (dict "Chart" .Chart "Release" .Release "Values" .Values "component" $component) | nindent 8 }}
    spec:
      {{- with .Values.s2.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: s2
          {{- with .Values.s2.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          image: "{{ .Values.s2.image.registry }}/{{ .Values.s2.image.repository }}:{{ .Values.s2.image.tag }}{{ with .Values.s2.image.digest }}@{{ . }}{{ end }}"
          imagePullPolicy: {{ .Values.s2.image.pullPolicy }}
          command:
            - "lite"
            - "--init-file"
            - "/config/s2-spec.json"
            - "--local-root"
            - "/data"
            - "--port"
            - {{ .Values.s2.service.targetPort | quote }}
          ports:
            - name: http
              containerPort: {{ .Values.s2.service.targetPort }}
              protocol: TCP
          {{- if .Values.s2.livenessProbe.enabled }}
          livenessProbe:
            tcpSocket:
              port: http
            initialDelaySeconds: {{ .Values.s2.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.s2.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.s2.livenessProbe.timeoutSeconds }}
            failureThreshold: {{ .Values.s2.livenessProbe.failureThreshold }}
            successThreshold: {{ .Values.s2.livenessProbe.successThreshold }}
          {{- end }}
          {{- if .Values.s2.readinessProbe.enabled }}
          readinessProbe:
            tcpSocket:
              port: http
            initialDelaySeconds: {{ .Values.s2.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.s2.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.s2.readinessProbe.timeoutSeconds }}
            failureThreshold: {{ .Values.s2.readinessProbe.failureThreshold }}
            successThreshold: {{ .Values.s2.readinessProbe.successThreshold }}
          {{- end }}
          resources:
            {{- toYaml .Values.s2.resources | nindent 12 }}
          volumeMounts:
            - name: spec
              mountPath: /config
              readOnly: true
            - name: data
              mountPath: /data
      volumes:
        - name: spec
          configMap:
            name: {{ include "trigger-v4.fullname" . }}-s2-spec
        {{- if not .Values.s2.persistence.enabled }}
        - name: data
          emptyDir: {}
        {{- end }}
  {{- if .Values.s2.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: data
        {{- if .Values.s2.persistence.retain }}
        annotations:
          helm.sh/resource-policy: keep
        {{- end }}
      spec:
        accessModes:
          - {{ .Values.s2.persistence.accessMode }}
        resources:
          requests:
            storage: {{ .Values.s2.persistence.size }}
        {{- if .Values.s2.persistence.storageClass }}
        storageClassName: {{ .Values.s2.persistence.storageClass }}
        {{- end }}
  {{- end }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "trigger-v4.fullname" . }}-s2
  labels:
    {{- $component := "s2" }}
    {{- include "trigger-v4.componentLabels" (dict "Chart" .Chart "Release" .Release "Values" .Values "component" $component) | nindent 4 }}
spec:
  type: {{ .Values.s2.service.type }}
  ports:
    - port: {{ .Values.s2.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    {{- include "trigger-v4.componentSelectorLabels" (dict "Chart" .Chart "Release" .Release "Values" .Values "component" $component) | nindent 4 }}
{{- end }}
