{{/*
---
cmem helpers
---
*/}}

{{- define "magda.var_dump" -}}
{{- . | mustToPrettyJson | printf "\nThe JSON output of the dumped var is: \n%s" | fail }}
{{- end -}}

{{/*
Expand the name of the chart.
*/}}
{{- define "cmem.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "cmem.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "cmem.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "cmem.labels" -}}
helm.sh/chart: {{ include "cmem.chart" . }}
{{ include "cmem.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "cmem.selectorLabels" -}}
app.kubernetes.io/name: {{ include "cmem.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Image Pull Secret
*/}}
{{- define "eccenca.imagePullSecret" }}
{{- printf "{\"%s\": {\"auth\": \"%s\", \"username\": \"%s\", \"password\": \"%s\"}}" .Values.imageCredentials.registry (printf "%s:%s" .Values.imageCredentials.username .Values.imageCredentials.password | b64enc) .Values.imageCredentials.username .Values.imageCredentials.password | b64enc }}
{{- end }}

{{/*
---
Explore helpers
---
*/}}

{{- define "cmem.name-explore" -}}
{{- "explore" }}
{{- end }}

{{- define "cmem.servicename-explore" -}}
{{- default .Chart.Name .Values.explore.service.name | trunc 63 | trimSuffix "-" }}
{{- end }}

{{- define "cmem.labels-explore" -}}
helm.sh/chart: {{ include "cmem.chart" . }}
{{ include "cmem.selectorLabels-explore" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{- define "cmem.selectorLabels-explore" -}}
{{ include "cmem.selectorLabels" . }}
app.kubernetes.io/component: {{ include "cmem.name-explore" . }}
{{- end }}

{{/*
---
Dataintegration helpers
---
*/}}

{{- define "cmem.name-di" -}}
{{- "dataintegration" }}
{{- end }}

{{- define "cmem.servicename-di" -}}
{{- default .Chart.Name .Values.dataintegration.service.name | trunc 63 | trimSuffix "-" }}
{{- end }}

{{- define "cmem.labels-di" -}}
helm.sh/chart: {{ include "cmem.chart" . }}
{{ include "cmem.selectorLabels-di" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{- define "cmem.selectorLabels-di" -}}
{{ include "cmem.selectorLabels" . }}
app.kubernetes.io/component: {{ include "cmem.name-di" . }}
{{- end }}

{{/*
---
GraphInsights helpers
---
*/}}

{{- define "cmem.name-graphinsights" -}}
{{- "graphinsights" }}
{{- end }}

{{- define "cmem.servicename-graphinsights" -}}
{{- default .Chart.Name .Values.graphinsights.service.name | trunc 63 | trimSuffix "-" }}
{{- end }}

{{- define "cmem.labels-graphinsights" -}}
helm.sh/chart: {{ include "cmem.chart" . }}
{{ include "cmem.selectorLabels-graphinsights" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{- define "cmem.selectorLabels-graphinsights" -}}
{{ include "cmem.selectorLabels" . }}
app.kubernetes.io/component: {{ include "cmem.name-graphinsights" . }}
{{- end }}

{{/*
Create an image map from the Chart annotations.
The map is keyed by the image name.
*/}}
{{- define "annotations.map" -}}
{{- $imageMap := dict -}}
{{- if .Chart.Annotations.images -}}
  {{- $wrappedYaml := printf "list:\n%s" .Chart.Annotations.images -}}
  {{- $parsed := $wrappedYaml | fromYaml -}}
  {{- $imageList := $parsed.list -}}
  {{- range $index, $image := $imageList -}}
    {{- $_ := set $imageMap (index $image "name") $image -}}
  {{- end -}}
{{- end -}}
{{- $imageMap | toYaml -}}
{{- end -}}

{{/*
Render image reference with values.yaml override support.
Usage: include "cmem.image" (dict "context" . "name" "cmemc" "values" .Values.bootstrap.image)
Parameters:
  - context: The root context (.)
  - name: The image name from Chart annotations (e.g., "cmemc")
  - values: The values.yaml image object (e.g., .Values.bootstrap.image)
Returns: Full image reference (repository:tag)
*/}}
{{- define "cmem.image" -}}
{{- $context := .context -}}
{{- $name := .name -}}
{{- $valuesImage := .values -}}
{{- $result := "" -}}
{{- if and $valuesImage $valuesImage.repository $valuesImage.tag -}}
  {{- /* Use values.yaml override if both repository and tag are defined */}}
  {{- $result = printf "%s:%s" $valuesImage.repository $valuesImage.tag -}}
{{- else -}}
  {{- /* Fall back to Chart annotations default */}}
  {{- $imageMap := include "annotations.map" $context | fromYaml -}}
  {{- $imageData := index $imageMap $name -}}
  {{- if $imageData -}}
    {{- $result = $imageData.image -}}
  {{- else -}}
    {{- fail (printf "Image '%s' not found in Chart annotations" $name) -}}
  {{- end -}}
{{- end -}}
{{- $result -}}
{{- end -}}

{{/*
---
CA Bundle Path helpers
---
*/}}

{{/*
Get CA bundle path for cmemc (bootstrap)
Supports architecture-based automatic selection or manual override
*/}}
{{- define "cmem.cmemc-ca-bundle-path" -}}
{{- if ne .Values.bootstrap.default_ca_bundle_path "" -}}
  {{- /* Use manual override if provided */}}
  {{- .Values.bootstrap.default_ca_bundle_path -}}
{{- else -}}
  {{- /* Auto-select based on architecture */}}
  {{- if eq .Values.bootstrap.architecture "arm64" -}}
    {{- "/usr/local/cpython-3.13.8-linux-aarch64-gnu/lib/python3.13/site-packages/pip/_vendor/certifi/cacert.pem" -}}
  {{- else -}}
    {{- "/usr/local/cpython-3.13.8-linux-x86_64-gnu/lib/python3.13/site-packages/pip/_vendor/certifi/cacert.pem" -}}
  {{- end -}}
{{- end -}}
{{- end -}}

{{/*
Get CA bundle path for DataIntegration
Supports architecture-based automatic selection or manual override
*/}}
{{- define "cmem.di-ca-bundle-path" -}}
{{- if ne .Values.dataintegration.config.di_ca_bundle_path "" -}}
  {{- /* Use manual override if provided */}}
  {{- .Values.dataintegration.config.di_ca_bundle_path -}}
{{- else -}}
  {{- /* Auto-select based on architecture */}}
  {{- if eq .Values.dataintegration.config.architecture "arm64" -}}
    {{- "/usr/local/cpython-3.13.12-linux-aarch64-gnu/lib/python3.13/site-packages/pip/_vendor/certifi/cacert.pem" -}}
  {{- else -}}
    {{- "/usr/local/cpython-3.13.12-linux-x86_64-gnu/lib/python3.13/site-packages/pip/_vendor/certifi/cacert.pem" -}}
  {{- end -}}
{{- end -}}
{{- end -}}
