{{- $version := include "traefik.proxyVersion" $ }}
{{- if (ne $version "experimental-v3.0") }}
  {{- if (semverCompare "<v3.0.0-0" $version) }}
    {{- fail "ERROR: This version of the Chart only supports Traefik Proxy v3" -}}
  {{- end }}
{{- end }}

{{- if and .Values.hub.enabled (not (contains "traefik-hub" .Values.image.repository)) }}
  {{- fail "ERROR: traefik-hub image is required when enabling Traefik Hub" -}}
{{- end }}

{{- if and (.Values.providers.kubernetesGateway).enabled (and (semverCompare "<v3.1.0-rc3" $version) (not .Values.experimental.kubernetesGateway.enabled)) }}
  {{- fail "ERROR: Before traefik v3.1.0-rc3, kubernetesGateway is experimental. Enable it by setting experimental.kubernetesGateway.enabled to true" -}}
{{- end }}

{{- if and (.Values.providers.knative).enabled (not .Values.experimental.knative) }}
  {{- fail "ERROR: Knative is experimental. Enable it by setting experimental.knative to true" -}}
{{- end }}

{{- if and (.Values.providers.kubernetesGateway.enabled) (.Values.gateway.defaultScope) (not .Values.providers.kubernetesGateway.experimentalChannel) }}
  {{- fail "ERROR: The Gateway 'defaultScope' field is experimental. Enable it by setting providers.kubernetesGateway.experimentalChannel=true" }}
{{- end }}

{{- if .Values.rbac.namespaced }}
  {{- if .Values.providers.kubernetesGateway.enabled }}
    {{- fail "ERROR: Kubernetes Gateway provider requires ClusterRole. RBAC cannot be namespaced." }}
  {{- end }}
  {{- if and (not .Values.providers.kubernetesIngress.enabled) (not .Values.providers.kubernetesCRD.enabled) }}
    {{- fail "ERROR: namespaced rbac requires Kubernetes CRD or Kubernetes Ingress provider." }}
  {{- end }}
{{- end }}

{{- if and (semverCompare "<v3.2.0-0" $version) (.Values.experimental.fastProxy.enabled)}}
  {{- fail "ERROR: fastProxy is an experimental feature only available for traefik >= v3.2.0." }}
{{- end }}

{{- if and (semverCompare "<v3.3.0-0" $version) (.Values.experimental.abortOnPluginFailure)}}
  {{- fail "ERROR: abortOnPluginFailure is an experimental feature only available for traefik >= v3.3.0." }}
{{- end }}

{{- if and (semverCompare "<v3.6.0-0" $version) (.Values.experimental.knative)}}
  {{- fail "ERROR: Knative provider is an experimental feature only available for traefik >= v3.6.0." }}
{{- end }}

{{- if and (semverCompare "<v3.6.2-0" $version) (.Values.providers.kubernetesIngressNginx).enabled}}
  {{- fail "ERROR: Kubernetes Ingress NGINX provider is only available for traefik >= v3.6.2." }}
{{- end }}

{{- if and (semverCompare "<3.2.0-0" $version) (.Values.providers.kubernetesGateway.nativeLBByDefault)}}
  {{- fail "ERROR: nativeLBByDefault has been introduced in Kubernetes Gateway provider in v3.2.0" }}
{{- end }}

{{- if and (semverCompare "<3.5.0-0" $version) (.Values.providers.kubernetesIngress.strictPrefixMatching)}}
  {{- fail "ERROR: strictPrefixMatching is a feature only available for traefik >= v3.5.0." }}
{{- end }}

{{- if and (semverCompare "<3.5.2-0" $version) (eq .Values.logs.access.format "genericCLF")}}
  {{- fail "ERROR: genericCLF is an accesslog format option only available for traefik >= v3.5.2." }}
{{- end }}

{{- if and (semverCompare "<v3.2.0-0" $version) (.Values.metrics.otlp.serviceName)}}
  {{- fail "ERROR: serviceName is a feature only available for traefik >= v3.2.0." }}
{{- end }}

{{- if and (semverCompare "<v3.5.3-0" $version) (.Values.metrics.otlp.resourceAttributes)}}
  {{- fail "ERROR: resourceAttributes with otlp on metrics is a feature only available for traefik >= v3.5.3." }}
{{- end }}

{{- if and (not .Values.experimental.otlpLogs) (or (.Values.logs.general.otlp.enabled) (.Values.logs.access.otlp.enabled))}}
  {{- fail "ERROR: otlp on logs or access logs is an experimental feature and needs experimental.otlpLogs=true." }}
{{- end }}

{{- if and (semverCompare "<v3.3.0-0" $version) (.Values.logs.general.otlp.enabled)}}
  {{- fail "ERROR: otlp on logs is a feature only available for traefik >= v3.3.0." }}
{{- end }}

{{- if and (semverCompare "<v3.3.0-0" $version) (.Values.logs.access.otlp.enabled)}}
  {{- fail "ERROR: otlp on access logs is a feature only available for traefik >= v3.3.0." }}
{{- end }}

{{- if and (semverCompare "<v3.1.0-0" $version) .Values.tracing.safeQueryParams }}
  {{ fail "ERROR: safeQueryParams is a feature only available for traefik >= v3.1.0."}}
{{- end }}

{{- range $portName, $config := .Values.ports }}
  {{- if and (semverCompare "<v3.3.0-0" $version) (or (ne $config.observability.accessLogs nil) (ne $config.observability.metrics nil) (ne $config.observability.tracing nil) (ne $config.observability.tracingVerbosity nil)) }}
    {{ fail "ERROR: per entrypoint observability is a feature only available for traefik >= v3.3.0."}}
  {{- end }}
  {{- if and (semverCompare "<v3.5.0-0" $version) (ne $config.observability.traceVerbosity nil) }}
    {{ fail "ERROR: traceVerbosity is a feature only available for traefik >= v3.5.0."}}
  {{- end }}
{{- end }}

{{- if and (semverCompare "<v3.6.4-0" $version) (or
    (eq .Values.ports.websecure.http.sanitizePath false)
    .Values.ports.websecure.http.encodedCharacters.allowEncodedSlash
    .Values.ports.websecure.http.encodedCharacters.allowEncodedBackSlash
    .Values.ports.websecure.http.encodedCharacters.allowEncodedNullCharacter
    .Values.ports.websecure.http.encodedCharacters.allowEncodedSemicolon
    .Values.ports.websecure.http.encodedCharacters.allowEncodedPercent
    .Values.ports.websecure.http.encodedCharacters.allowEncodedQuestionMark
    .Values.ports.websecure.http.encodedCharacters.allowEncodedHash )}}
  {{- fail "ERROR: request path security options are only available for traefik >= v3.6.4." }}
{{- end }}

{{- if $.Values.hub.token -}}
  {{ $hubVersion := $.Values.oci_meta.enabled | ternary $.Values.oci_meta.images.hub.tag $.Values.image.tag }}
  {{ $hubVersion = ($.Values.global.azure.enabled | ternary $.Values.global.azure.images.hub.tag $hubVersion) }}
  {{ if not $hubVersion }}
    {{ fail "When using Traefik Hub image tag needs to be specified!" }}
  {{- end -}}

  {{ $hubVersion = (split "@" (default "v3" $hubVersion))._0 }}

  {{/* Consider non semver versions as latest one  */}}
  {{- if not (regexMatch "v[0-9]+.[0-9]+.[0-9]+" (default "" $hubVersion)) -}}
    {{ $hubVersion = "v3.99" }}
  {{- end }}

  {{- if semverCompare "<v3.19.0-0" $hubVersion }}
    {{ fail "ERROR: this Chart supports *only* Traefik Hub >= v3.19.0."}}
  {{- end }}

  {{- if and (not $.Values.tracing.otlp.enabled) .Values.hub.tracing.additionalTraceHeaders.enabled }}
    {{ fail "ERROR: additionalTraceHeaders needs tracing.otlp to be enabled."}}
  {{- end }}

  {{- with .Values.hub.pluginRegistry.sources }}
    {{- range $pluginName, $pluginConf := . }}
      {{- if not (hasKey $.Values.experimental.plugins $pluginName) }}
         {{ fail (printf "ERROR: pluginRegistry source %s is not used in exprimental.plugins." $pluginName) }}
      {{- end }}
    {{- end }}
  {{- end }}
{{- end }}
