{{/*
Validation template to ensure external service configurations are provided when deploy: false
This template will fail the Helm deployment if external config is missing for required services
*/}}
{{- if not .Values.postgres.deploy }}
{{- if and (not .Values.postgres.external.databaseUrl) (not .Values.postgres.external.existingSecret) }}
{{- fail "PostgreSQL external configuration is required when postgres.deploy=false. Please provide either postgres.external.databaseUrl or postgres.external.existingSecret" }}
{{- end }}
{{- end }}

{{- if not .Values.redis.deploy }}
{{- if not .Values.redis.external.host }}
{{- fail "Redis external configuration is required when redis.deploy=false. Please provide redis.external.host" }}
{{- end }}
{{- end }}

{{- if not .Values.clickhouse.deploy }}
{{- if or (not .Values.clickhouse.external.host) (not .Values.clickhouse.external.username) }}
{{- fail "ClickHouse external configuration is required when clickhouse.deploy=false. Please provide clickhouse.external.host and clickhouse.external.username" }}
{{- end }}
{{- end }}

{{- if .Values.s3.deploy }}
{{- if and (not .Values.s3.auth.existingSecret) (not .Values.s3.auth.accessKeyId) (not .Values.s3.auth.rootUser) }}
{{- fail "S3 auth credentials are required when s3.deploy=true. Please provide either s3.auth.accessKeyId, s3.auth.existingSecret, or s3.auth.rootUser" }}
{{- end }}
{{- else }}
{{- if not .Values.s3.external.endpoint }}
{{- fail "S3 external configuration is required when s3.deploy=false. Please provide s3.external.endpoint" }}
{{- end }}
{{- if and (not .Values.s3.external.existingSecret) (or (not .Values.s3.external.accessKeyId) (not .Values.s3.external.secretAccessKey)) }}
{{- fail "S3 credentials are required when s3.deploy=false. Please provide either s3.external.existingSecret or both s3.external.accessKeyId and s3.external.secretAccessKey" }}
{{- end }}
{{- end }}

{{- if not .Values.electric.deploy }}
{{- if not .Values.electric.external.url }}
{{- fail "Electric external configuration is required when electric.deploy=false. Please provide electric.external.url" }}
{{- end }}
{{- end }}

{{- if and .Values.s2.deploy .Values.s2.external.endpoint }}
{{- fail "S2 configuration conflict: s2.external.endpoint is set but s2.deploy=true, so the external endpoint would be ignored. Set s2.deploy=false to use an external S2 endpoint, or clear s2.external.endpoint to use the bundled s2-lite." }}
{{- end }}

{{- if not .Values.registry.deploy }}
{{- if or (not .Values.registry.external.host) }}
{{- fail "Registry external configuration is required when registry.deploy=false. Please provide registry.external.host" }}
{{- end }}
{{- end }}

{{/*
This template produces no output but will fail the deployment if validation fails
*/}}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "trigger-v4.fullname" . }}-external-config-validation
  labels:
    {{- include "trigger-v4.labels" . | nindent 4 }}
  annotations:
    helm.sh/hook: pre-install,pre-upgrade
    helm.sh/hook-weight: "-10"
    helm.sh/hook-delete-policy: before-hook-creation
data:
  validation: "completed"