{{- if and .Values.redisOperator.enabled .Values.redisOperator.managed.enabled }}
{{- $name := .Values.redisOperator.name | default (printf "%s-redis" (include "sure.fullname" .)) -}}

{{/* Prefer managed.* if provided; fallback to top-level */}}
{{- $imgRepo := .Values.redisOperator.image.repository | default "quay.io/opstree/redis" -}}
{{- $imgTag  := .Values.redisOperator.image.tag        | default "v8.4.0" -}}
{{- $replicas := .Values.redisOperator.replicas | default 3 -}}
{{- $nodeSelector := (coalesce .Values.redisOperator.managed.nodeSelector .Values.redisOperator.nodeSelector) -}}
{{- $tolerations := (coalesce .Values.redisOperator.managed.tolerations  .Values.redisOperator.tolerations)  -}}
{{- $affinity    := (coalesce .Values.redisOperator.managed.affinity     .Values.redisOperator.affinity)     -}}
{{- $tsc         := (coalesce .Values.redisOperator.managed.topologySpreadConstraints .Values.redisOperator.topologySpreadConstraints) -}}
{{- $workloadResources := (coalesce .Values.redisOperator.managed.workloadResources .Values.redisOperator.workloadResources) -}}
{{- $persistence := (coalesce .Values.redisOperator.managed.persistence .Values.redisOperator.persistence) -}}
apiVersion: redis.redis.opstreelabs.in/v1beta2
kind: RedisReplication
metadata:
  name: {{ $name | quote }}
  labels:
    app.kubernetes.io/component: redis
    {{- include "sure.labels" . | nindent 4 }}
spec:
  clusterSize: {{ $replicas }}
  kubernetesConfig:
    image: {{ printf "%s:%s" $imgRepo $imgTag | quote }}
    redisSecret:
      name: {{ include "sure.redisSecretName" . }}
      key: {{ include "sure.redisPasswordKey" . }}
    {{- with $workloadResources }}
    resources:
      {{- toYaml . | nindent 6 }}
    {{- end }}
    {{- with $nodeSelector }}
    nodeSelector:
      {{- toYaml . | nindent 6 }}
    {{- end }}
    {{- with $tolerations }}
    tolerations:
      {{- toYaml . | nindent 6 }}
    {{- end }}
    {{- with $affinity }}
    affinity:
      {{- toYaml . | nindent 6 }}
    {{- end }}
    {{- with $tsc }}
    topologySpreadConstraints:
      {{- toYaml . | nindent 6 }}
    {{- end }}
  {{- if and $persistence $persistence.enabled }}
  storage:
    volumeClaimTemplate:
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: {{ $persistence.size | default "8Gi" }}
        {{- if $persistence.className }}
        storageClassName: {{ $persistence.className }}
        {{- end }}
  {{- end }}
{{- end }}
