{{- define "hippocampus.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{- define "hippocampus.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 }}

{{- define "hippocampus.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

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

{{- define "hippocampus.selectorLabels" -}}
app.kubernetes.io/name: {{ include "hippocampus.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/* Common env injected into every CronJob container */}}
{{- define "hippocampus.commonEnv" -}}
- name: QDRANT_HOST
  value: {{ required "qdrant.host is required" .Values.qdrant.host | quote }}
- name: QDRANT_PORT
  value: {{ .Values.qdrant.port | quote }}
{{- if .Values.qdrant.apiKeySecret }}
- name: QDRANT_API_KEY
  valueFrom:
    secretKeyRef:
      name: {{ .Values.qdrant.apiKeySecret }}
      key: {{ .Values.qdrant.apiKeySecretKey }}
{{- end }}
- name: OLLAMA_HOST
  value: {{ required "ollama.host is required" .Values.ollama.host | quote }}
- name: EMBED_MODEL
  value: {{ .Values.ollama.embedModel | quote }}
- name: DB_TYPE
  value: {{ .Values.db.type | quote }}
{{- if eq .Values.db.type "sqlite" }}
- name: SQLITE_PATH
  value: {{ .Values.db.sqlitePath | quote }}
{{- else if eq .Values.db.type "postgres" }}
- name: POSTGRES_DSN
  {{- if .Values.db.postgres.existingSecret }}
  valueFrom:
    secretKeyRef:
      name: {{ .Values.db.postgres.existingSecret }}
      key: {{ .Values.db.postgres.secretKey }}
  {{- else }}
  value: {{ .Values.db.postgres.dsn | quote }}
  {{- end }}
{{- end }}
{{- end }}

{{/* State volume mount */}}
{{- define "hippocampus.stateMount" -}}
- name: state
  mountPath: /state
{{- end }}

{{/* State volume reference */}}
{{- define "hippocampus.stateVolume" -}}
- name: state
  persistentVolumeClaim:
    claimName: {{ if .Values.state.existingClaim }}{{ .Values.state.existingClaim }}{{ else }}{{ include "hippocampus.fullname" . }}-state{{ end }}
{{- end }}

{{/* Standard CronJob pod spec boilerplate */}}
{{- define "hippocampus.podSpec" -}}
restartPolicy: OnFailure
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
  {{- toYaml . | nindent 2 }}
{{- end }}
{{- with .Values.nodeSelector }}
nodeSelector:
  {{- toYaml . | nindent 2 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
  {{- toYaml . | nindent 2 }}
{{- end }}
{{- with .Values.podSecurityContext }}
securityContext:
  {{- toYaml . | nindent 2 }}
{{- end }}
{{- end }}
