{{/* Common metadata fields are metadata fields that are required for all organisms
for Loculus to work.
In almost all cases (except for versionComment), these fields are not supposed to
be input by the user but are instead generated by the system.
They are defined here so that they can't be misconfigured in values.yaml.
Beware: if a field here is supposed to be user submitted and preprocessed, you need
it's not enough to add the field here, you need to add it in other places.
See https://github.com/loculus-project/loculus/pull/3141 for an example */}}
{{- define "loculus.commonMetadata" }}
fields:
  - name: accessionVersion
    displayName: Accession version
    type: string
    notSearchable: true
    hideOnSequenceDetailsPage: true
    includeInDownloadsByDefault: true
    definition: {{ printf "The %s `accession` and `version`, uniquely identifying the specific version of the sequence record (e.g. `%s000001.1`)." $.Values.name $.Values.accessionPrefix | quote }}
  - name: accession
    displayName: Accession
    type: string
    notSearchable: true
    hideOnSequenceDetailsPage: true
    definition: {{ printf "A unique identifier assigned to the sequence record by %s (e.g. `%s000001`)." $.Values.name $.Values.accessionPrefix | quote }}
  - name: version
    displayName: Version
    type: int
    hideOnSequenceDetailsPage: true
    definition: "The version number of the sequence record, incremented each time the sequence is revised."
  - name: submissionId
    displayName: Submission ID
    type: string
    header: Submission details
    orderOnDetailsPage: 5000
    enableSubstringSearch: true
    includeInDownloadsByDefault: true
    definition: "The sample identifier provided by the submitter."
  - name: isRevocation
    displayName: Is revocation
    type: boolean
    autocomplete: true
    hideOnSequenceDetailsPage: true
    definition: "Indicator of whether the sequence record is revoked."
  - name: submitter
    displayName: Submitter
    type: string
    generateIndex: true
    autocomplete: true
    hideOnSequenceDetailsPage: true
    header: Submission details
    orderOnDetailsPage: 5010
    definition: {{ printf "Name of the %s user who submitted the sequence record." $.Values.name | quote }}
  - name: groupName
    type: string
    generateIndex: true
    autocomplete: true
    header: Submission details
    displayName: Submitting group
    includeInDownloadsByDefault: true
    orderOnDetailsPage: 5020
    definition: "Name of the group that submitted the sequence record."
    customDisplay:
      type: submittingGroup
      displayGroup: group
  - name: groupId
    type: int
    autocomplete: true
    header: Submission details
    displayName: Submitting group (numeric ID)
    orderOnDetailsPage: 5030
    definition: "Numeric ID of the group that submitted the sequence record."
    customDisplay:
      type: submittingGroup
      displayGroup: group
  - name: submittedAtTimestamp
    type: timestamp
    displayName: Date submitted
    header: Submission details
    orderOnDetailsPage: 5040
    definition: {{ printf "Date and time on which the sequence record was submitted to %s." $.Values.name | quote }}
  - name: submittedDate
    type: string
    hideOnSequenceDetailsPage: true
    generateIndex: true
    autocomplete: true
    displayName: Date submitted (exact)
    orderOnDetailsPage: 5050
    definition: {{ printf "Date on which the sequence record was submitted to %s." $.Values.name | quote }}
  - name: releasedAtTimestamp
    type: timestamp
    displayName: Date released
    header: Submission details
    columnWidth: 100
    orderOnDetailsPage: 5060
    definition: {{ printf "Date and time on which the sequence record was released on %s." $.Values.name | quote }}
  - name: releasedDate
    type: string
    hideOnSequenceDetailsPage: true
    generateIndex: true
    autocomplete: true
    displayName: Date released (exact)
    columnWidth: 100
    orderOnDetailsPage: 5070
    definition: {{ printf "Date on which the sequence record was released on %s." $.Values.name | quote }}
  {{- if $.Values.dataUseTerms.enabled }}
  - name: dataUseTerms
    type: string
    generateIndex: true
    autocomplete: true
    displayName: Data use terms
    initiallyVisible: true
    includeInDownloadsByDefault: true
    definition: "The terms under which the sequence record may be used; either `OPEN` or `RESTRICTED`."
    customDisplay:
      type: dataUseTerms
    header: Data use terms
    orderOnDetailsPage: 610
    orderInSearchDisplay: 10
  - name: dataUseTermsRestrictedUntil
    type: date
    displayName: Data use terms restricted until
    hideOnSequenceDetailsPage: true
    header: Data use terms
    orderOnDetailsPage: 620
    definition: "The date until which the sequence record is restricted use."
  - name: dataBecameOpenAt
    type: date
    displayName: Date data became open
    hideOnSequenceDetailsPage: true
    header: Data use terms
    orderOnDetailsPage: 625
    definition: "The date on which the sequence record transitioned from restricted to open access."
  {{- if $.Values.dataUseTerms.urls }}
  - name: dataUseTermsUrl
    displayName: Data use terms URL
    type: string
    notSearchable: true
    header: Data use terms
    includeInDownloadsByDefault: true
    definition: "Link to the full text of the data use terms applicable to the sequence record."
    customDisplay:
      type: link
      url: "__value__"
    orderOnDetailsPage: 630
  {{- end}}
  {{- end}}
  - name: versionStatus
    displayName: Version status
    type: string
    autocomplete: true
    hideOnSequenceDetailsPage: true
    definition: "Indicates whether this is the latest version of the sequence record (`LATEST_VERSION`), an earlier version (`REVISED`), or has been revoked (`REVOKED`)."
  - name: versionComment
    type: string
    displayName: Version comment
    header: Submission details
    orderOnDetailsPage: 5000
    definition: "Reason for revising sequences, or other general comments concerning a specific version."
  - name: pipelineVersion
    displayName: Pipeline version
    type: int
    notSearchable: true
    hideOnSequenceDetailsPage: true
    definition: "The version of the processing pipeline used to process the sequence record."
{{- end}}

{{/* Patches schema by adding to it and overwriting overlapping fields by the value in metadataAdd*/}}
{{- define "loculus.patchMetadataSchema" -}}
{{- $patchedSchema := deepCopy . }}
{{- $metadata := .metadata | default (list) }}
{{- $toAdd := . | dig "metadataAdd" list | default (list) }}
{{- $metadataMap := dict -}}
{{- range $metadata }}
  {{- $key := .name }}
  {{- $metadataMap = merge $metadataMap (dict $key .) -}}
{{- end -}}
{{- range $toAdd }}
  {{- $key := .name }}
  {{- $metadataMap = set $metadataMap $key . -}}
{{- end -}}
{{- $patchedMetadata := list -}}
{{- range $key, $value := $metadataMap }}
  {{- $patchedMetadata = append $patchedMetadata $value -}}
{{- end -}}
{{- set $patchedSchema "metadata" $patchedMetadata | toYaml -}}
{{- end -}}

{{/* Generate website config from passed config object */}}
{{- define "loculus.generateWebsiteConfig" }}
name: {{ quote $.Values.name }}
logo: {{ $.Values.logo | toYaml | nindent 6 }}
{{ if $.Values.sequenceFlagging }}
sequenceFlagging: {{ $.Values.sequenceFlagging | toYaml | nindent 6 }}
{{ end }}
{{ if $.Values.gitHubMainUrl }}
gitHubMainUrl: {{ quote $.Values.gitHubMainUrl }}
{{ end }}
{{ if $.Values.gitHubIssuesUrl }}
gitHubIssuesUrl: {{ quote $.Values.gitHubIssuesUrl }}
{{ end }}
{{ if $.Values.issuesEmail }}
issuesEmail: {{ quote $.Values.issuesEmail }}
{{ end }}
{{ if $.Values.bannerMessageURL }}
bannerMessageURL: {{ quote $.Values.bannerMessageURL }}
{{ end }}
{{ if $.Values.bannerMessage }}
bannerMessage: {{ quote $.Values.bannerMessage }}
{{ else if or $.Values.runDevelopmentMainDatabase $.Values.runDevelopmentKeycloakDatabase }}
bannerMessage: "Warning: Development or Keycloak main database is enabled. Development environment only."
{{ end }}
{{ if $.Values.submissionBannerMessageURL }}
submissionBannerMessageURL: {{ quote $.Values.submissionBannerMessageURL }}
{{ end }}
{{ if $.Values.submissionBannerMessage }}
submissionBannerMessage: {{ quote $.Values.submissionBannerMessage }}
{{ end }}
{{ if $.Values.gitHubEditLink }}
gitHubEditLink: {{ quote $.Values.gitHubEditLink }}
{{ end }}
{{ if $.Values.welcomeMessageHTML }}
welcomeMessageHTML: {{ quote $.Values.welcomeMessageHTML }}
{{end}}
{{ if $.Values.additionalHeadHTML }}
additionalHeadHTML: {{ quote $.Values.additionalHeadHTML }}
{{end}}

enableLoginNavigationItem: {{ $.Values.website.websiteConfig.enableLoginNavigationItem }}
enableSubmissionNavigationItem: {{ $.Values.website.websiteConfig.enableSubmissionNavigationItem }}
enableSubmissionPages: {{ $.Values.website.websiteConfig.enableSubmissionPages }}
readOnlyMode: {{ $.Values.readOnlyMode | default false }}
enableSeqSets: {{ $.Values.seqSets.enabled }}
{{- if $.Values.seqSets.fieldsToDisplay }}
seqSetsFieldsToDisplay: {{ $.Values.seqSets.fieldsToDisplay | toJson }}
{{- end }}
{{- if $.Values.seqSets.graphs }}
seqSetsGraphs: {{ $.Values.seqSets.graphs | toJson }}
{{- end }}
enableDataUseTerms: {{ $.Values.dataUseTerms.enabled }}
{{ if $.Values.dataUseTerms.agreementHTML }}
dataUseTermsAgreementHTML: {{ quote $.Values.dataUseTerms.agreementHTML }}
{{- end }}
accessionPrefix: {{ quote $.Values.accessionPrefix }}
dateFieldForGroupGraph: {{ if $.Values.dateFieldForGroupGraph }}{{ quote $.Values.dateFieldForGroupGraph }}{{ else }}null{{ end }}
{{- $commonMetadata := (include "loculus.commonMetadata" . | fromYaml).fields }}
organisms:
  {{- range $_, $item := (include "loculus.enabledOrganisms" . | fromJson).organisms }}
{{- $key := $item.key }}
{{- $instance := $item.contents }}
  {{ $key }}:
    schema:
      {{- with ($instance.schema | include "loculus.patchMetadataSchema" | fromYaml) }}
      organismName: {{ quote .organismName }}
      {{ if .linkOuts }}
      linkOuts:
        {{- range $linkOut := .linkOuts }}
        - name: {{ quote $linkOut.name }}
          url: {{ quote $linkOut.url }}
          {{- if $linkOut.maxNumberOfRecommendedEntries }}
          maxNumberOfRecommendedEntries: {{ $linkOut.maxNumberOfRecommendedEntries }}
          {{- end }}
          {{- if $linkOut.onlyForReferences }}
          onlyForReferences: {{ $linkOut.onlyForReferences | toYaml | nindent 12 }}
          {{- end }}
          {{- if $linkOut.category }}
          category: {{ quote $linkOut.category }}
          {{- end }}
        {{- end }}
      {{- end }}
      loadSequencesAutomatically: {{ .loadSequencesAutomatically | default false }}
      {{ if .richFastaHeaderFields}}
      richFastaHeaderFields: {{ toJson .richFastaHeaderFields }}
      {{ end }}
      {{- include "loculus.submissionDataTypes" . | nindent 6 }}
      {{- $nucleotideSequences := .nucleotideSequences | default (list "main")}}
      {{ if .image }}
      image: {{ .image }}
      {{ end }}
      {{ if .description }}
      description: {{ quote .description }}
      {{ end }}
      primaryKey: accessionVersion
      inputFields: {{- include "loculus.inputFields" . | nindent 8 }}
        - name: versionComment
          displayName: Version comment
          definition: "Reason for revising sequences or other general comments concerning a specific version."
          example: "Fixed an issue in previous version where low-coverage nucleotides were erroneously filled with reference sequence."
          desired: true
      {{ if .files }}
      files: {{ .files | toYaml | nindent 8 }}
      {{ end }}
      metadata:
        {{- $args := dict "metadata" (concat $commonMetadata .metadata) "referenceGenomes" $instance.referenceGenomes}}
        {{ $metadata := include "loculus.generateWebsiteMetadata" $args | fromYaml }}
        {{ $metadata.fields | toYaml | nindent 8 }}
        {{ if .files }}
        {{- range .files }}
        - name: {{ .name }}
          {{- if .displayName }}
          displayName: {{ .displayName }}
          {{- end }}
          type: string
          header: "Files"
          noInput: true
          customDisplay:
            type: fileList
        {{- end }}
        {{ end }}
      {{ if .metadataTemplate }}
      metadataTemplate:
        {{ .metadataTemplate | toYaml | nindent 8}}
      {{ end }}
      {{ omit .website "multiFieldSearches" | toYaml | nindent 6 }}
      {{- if .website.multiFieldSearches }}
      {{- $perSegmentFields := dict }}
      {{- range (concat $commonMetadata .metadata) }}
      {{- if .perSegment }}{{- $_ := set $perSegmentFields .name true }}{{- end }}
      {{- end }}
      {{- $segments := (include "loculus.getNucleotideSegmentNames" $instance.referenceGenomes | fromYaml).segments }}
      {{- $isSegmented := gt (len $segments) 1 }}
      multiFieldSearches:
      {{- range .website.multiFieldSearches }}
        - name: {{ .name }}
          displayName: {{ .displayName }}
          {{- if hasKey . "orderInSearchDisplay" }}
          orderInSearchDisplay: {{ .orderInSearchDisplay }}
          {{- end }}
          fields:
          {{- range .fields }}
          {{- if and $isSegmented (hasKey $perSegmentFields .) }}
          {{- $field := . }}
          {{- range $segments }}
            - {{ printf "%s_%s" $field . }}
          {{- end }}
          {{- else }}
            - {{ . }}
          {{- end }}
          {{- end }}
      {{- end }}
      {{- end }}
      {{- end }}
    referenceGenomes:
      {{ $instance.referenceGenomes | toYaml | nindent 6 }}
  {{- end }}
{{- end }}

{{- define "loculus.standardWebsiteMetadata" }}
- type: {{ .type | default "string" | quote }}
  {{- if .definition }}
  definition: {{ .definition | quote }}
  {{- end }}
  {{- if .autocomplete }}
  autocomplete: {{ .autocomplete }}
  {{- end }}
  {{- if .enableSubstringSearch }}
  substringSearch: {{ .enableSubstringSearch }}
  {{- end}}
  {{- if .notSearchable }}
  notSearchable: {{ .notSearchable }}
  {{- end }}
  {{- if .initiallyVisible }}
  initiallyVisible: {{ .initiallyVisible }}
  {{- end }}
  {{- if .hideInSearchResultsTable }}
  hideInSearchResultsTable: {{ .hideInSearchResultsTable }}
  {{- end }}
  {{- if or (or (eq .type "timestamp") (eq .type "date")) .rangeSearch }}
  rangeSearch: true
  {{- end }}
  {{- if .rangeOverlapSearch }}
  rangeOverlapSearch: {{ .rangeOverlapSearch | toJson }}
  {{- end}}
  {{- if .lineageSystem }}
  lineageSearch: true
  {{- end }}
  {{- if .hierarchicalFilter }}
  hierarchicalSearch: true
  {{- end }}
  {{- if and .hierarchicalFilter .hierarchicalSearchLabel }}
  hierarchicalSearchLabel: {{ .hierarchicalSearchLabel }}
  {{- end }}
  {{- if .hideOnSequenceDetailsPage }}
  hideOnSequenceDetailsPage: {{ .hideOnSequenceDetailsPage }}
  {{- end }}
  {{- if .columnWidth }}
  columnWidth: {{ .columnWidth }}
  {{- end }}
  {{- if .order }}
  order: {{ .order }}
  {{- end }}
  {{- if .orderOnDetailsPage }}
  orderOnDetailsPage: {{ .orderOnDetailsPage }}
  {{- end }}
  {{- if .orderInSearchDisplay }}
  orderInSearchDisplay: {{ .orderInSearchDisplay }}
  {{- end }}
  {{- if .includeInDownloadsByDefault }}
  includeInDownloadsByDefault: {{ .includeInDownloadsByDefault }}
  {{- end }}
  {{- if .onlyForReference }}
  onlyForReference: {{ .onlyForReference }}
  {{- end }}
  {{- if .customDisplay }}
  customDisplay:
    type: {{ quote .customDisplay.type }}
    {{- if .customDisplay.url }}
    url: {{ .customDisplay.url }}
    {{- end }}
    {{- if .customDisplay.linkMenuItems }}
    linkMenuItems:
      {{- range .customDisplay.linkMenuItems }}
      - name: {{ quote .name }}
        url: {{ quote .url }}
      {{- end }}
    {{- end }}
    {{- if .customDisplay.displayGroup }}
    displayGroup: {{ quote .customDisplay.displayGroup }}
    {{- end }}
    {{- if .customDisplay.label }}
    label: {{ quote .customDisplay.label }}
    {{- end }}
    {{- if .customDisplay.html }}
    html: {{ .customDisplay.html }}
    {{- end }}
  {{- end }}
  {{- if .isSequenceFilter }}
  isSequenceFilter: {{ .isSequenceFilter }}
  {{- end }}
  {{- if .relatesToSegment }}
  relatesToSegment: {{ .relatesToSegment }}
  {{- end }}
  {{- if .percentage }}
  percentage: {{ .percentage }}
  {{- end }}
{{- end }}

{{/* Generate website metadata from passed metadata array */}}
{{- define "loculus.generateWebsiteMetadata" }}
{{- $segmentsData := include "loculus.getNucleotideSegmentNames" .referenceGenomes | fromYaml -}}
{{- $rawUniqueSegments := $segmentsData.segments | default (list) -}}
{{- $displayNameMap := $segmentsData.displayNames | default (dict) -}}
{{- $isSegmented := gt (len $rawUniqueSegments) 1 }}
{{- $metadataList := .metadata }}
fields:
{{- range $metadataList }}
{{- if and $isSegmented .perSegment }}
{{- $currentItem := . }}
{{- range $segment := $rawUniqueSegments }}
{{- $segmentDisplayName := default $segment (get $displayNameMap $segment) -}}
{{- with $currentItem }}
{{ include "loculus.standardWebsiteMetadata" . }}
  name: {{ printf "%s_%s" .name $segment | quote }}
  {{- if .displayName }}
  displayName: {{ printf "%s %s" .displayName $segmentDisplayName | quote }}
  {{- end }}
  {{- if (default false .oneHeader)}}
  header: {{ (default "Other" .header) | quote }}
  {{- else }}
  header: {{ printf "%s %s" (default "Other" .header) $segmentDisplayName | quote }}
  {{- end }}
  relatesToSegment: {{ $segment }}
  {{- if .isSequenceFilter }}
  isSequenceFilter: true
  {{- end }}
  {{- if and .customDisplay .customDisplay.displayGroup }}
  customDisplay:
    type: {{ quote .customDisplay.type }}
    displayGroup: {{ printf "%s_%s" .customDisplay.displayGroup $segment | quote }}
    {{- if .customDisplay.label }}
    label: {{ printf "%s %s" .customDisplay.label $segmentDisplayName | quote }}
    {{- end }}
  {{- end }}
{{- end }}
{{- end }}
{{- else }}
{{ include "loculus.standardWebsiteMetadata" . }}
  name: {{ quote .name }}
  {{- if .displayName }}
  displayName: {{ quote .displayName }}
  {{- end }}
  header: {{ default "Other" .header }}
{{- end}}
{{- end}}
{{- end}}

{{/* Generate backend config from passed config object */}}
{{- define "loculus.generateBackendConfig" }}
accessionPrefix: {{ quote $.Values.accessionPrefix }}
zstdCompressionLevel: {{ $.Values.zstdCompressionLevel }}
pipelineVersionUpgradeCheckIntervalSeconds: {{ $.Values.pipelineVersionUpgradeCheckIntervalSeconds }}
readOnlyMode: {{ $.Values.readOnlyMode | default false }}
name: {{ quote $.Values.name }}
dataUseTerms:
  {{$.Values.dataUseTerms | toYaml | nindent 2}}
{{- if .Values.fileSharing }}
fileSharing:
  {{ .Values.fileSharing | toYaml | nindent 2 }}
{{- end }}
websiteUrl: {{ include "loculus.websiteUrl" . }}
backendUrl: {{ include "loculus.backendUrl" . }}
organisms:
  {{- range $_, $item := (include "loculus.enabledOrganisms" . | fromJson).organisms }}
{{- $key := $item.key }}
{{- $instance := $item.contents }}
  {{ $key }}:
    schema:
      {{- with $instance.schema }}
      organismName: {{ quote .organismName }}
      {{- include "loculus.submissionDataTypes" . | nindent 6 }}
      {{- if .files }}
      files:
        {{ .files | toYaml | nindent 8 }}
      {{- end }}
      metadata:
        {{- $args := dict "metadata" (include "loculus.patchMetadataSchema" . | fromYaml).metadata "referenceGenomes" $instance.referenceGenomes }}
        {{- $metadata := include "loculus.generateBackendMetadata" $args | fromYaml }}
        {{ $metadata.fields | toYaml | nindent 8 }}
      externalMetadata:
        {{- $args := dict "metadata" (include "loculus.patchMetadataSchema" . | fromYaml).metadata "referenceGenomes" $instance.referenceGenomes }}
        {{- $metadata := include "loculus.generateBackendExternalMetadata" $args | fromYaml }}
        {{ $metadata.fields | default list | toYaml | nindent 8 }}
      earliestReleaseDate:
      {{- if .earliestReleaseDate }}
        {{ .earliestReleaseDate | toYaml | nindent 8 }}
      {{- else }}
        enabled: false
        externalFields: []
      {{- end }}
      {{- end }}
    referenceGenome:
      {{- $referenceGenome := include "loculus.mergeReferenceGenomes" $instance.referenceGenomes | fromYaml }}
      {{ $referenceGenome | toYaml | nindent 10 }}
  {{- end }}
{{- end }}

{{- define "loculus.generateReferenceGenome" }}
{{ if .nucleotideSequences }}
nucleotideSequences:
  {{ $nucleotideSequences := include "loculus.generateSequences" .nucleotideSequences | fromYaml }}
  {{ $nucleotideSequences.fields | toYaml | nindent 8 }}
{{ else }}
nucleotideSequences: []
{{ end }}
{{ if .genes }}
genes:
  {{ $genes := include "loculus.generateSequences" .genes | fromYaml }}
  {{ $genes.fields | toYaml | nindent 8 }}
{{ else }}
genes: []
{{ end }}
{{- end }}

{{- define "loculus.generateSequences" }}
{{- $sequences := . }}
fields:
  {{- range $sequence := $sequences }}
    - name: {{ printf "%s" $sequence.name | quote}}
      sequence: {{ printf "%s" $sequence.sequence | quote }}
  {{- end }}
{{- end }}

{{/* Generate backend metadata from passed metadata array */}}
{{- define "loculus.generateBackendMetadata" }}
{{- $rawUniqueSegments := (include "loculus.getNucleotideSegmentNames" .referenceGenomes | fromYaml).segments }}
{{- $isSegmented := gt (len $rawUniqueSegments) 1 }}
{{- $metadataList := .metadata }}
fields:
{{- range $metadataList }}
{{- $currentItem := . }}
{{- if and $isSegmented .perSegment }}
{{- range $segment := $rawUniqueSegments }}
{{- with $currentItem }}
  - name: {{ printf "%s_%s" .name $segment | quote }}
    type: {{ .type | default "string" | quote }}
{{- end }}
{{- end}}
{{- else }}
  - name: {{ quote .name }}
    type: {{ .type | default "string" | quote }}
{{- end}}
{{- end}}
  - name: versionComment
    type: "string"
{{- end}}

{{/* Generate backend metadata from passed metadata array */}}
{{- define "loculus.generateBackendExternalMetadata" }}
{{- $rawUniqueSegments := (include "loculus.getNucleotideSegmentNames" .referenceGenomes | fromYaml).segments }}
{{- $isSegmented := gt (len $rawUniqueSegments) 1 }}
{{- $metadataList := .metadata }}
fields:
{{- range $metadataList }}
{{- $currentItem := . }}
{{- if eq .header "INSDC" }}
{{- if and $isSegmented .perSegment }}
{{- range $segment := $rawUniqueSegments }}
{{- with $currentItem }}
  - name: {{ printf "%s_%s" .name $segment | quote }}
    type: {{ .type | default "string" | quote }}
    {{- if .required }}
    required: {{ .required }}
    {{- end }}
    externalMetadataUpdater: "ena"
{{- end }}
{{- end}}
{{- else }}
  - name: {{ quote .name }}
    type: {{ .type | default "string" | quote }}
    {{- if .required }}
    required: {{ .required }}
    {{- end }}
    externalMetadataUpdater: "ena"
{{- end}}
{{- end}}
{{- end}}
{{- end}}

{{- define "loculus.publicRuntimeConfig" }}
{{- $publicRuntimeConfig := $.Values.public }}
{{- $lapisUrlTemplate := "" }}
{{- if $publicRuntimeConfig.lapisUrlTemplate }}
  {{- $lapisUrlTemplate = $publicRuntimeConfig.lapisUrlTemplate }}
{{- else if eq $.Values.environment "server" }}
  {{- $lapisUrlTemplate = printf "https://lapis%s%s/%s" $.Values.subdomainSeparator $.Values.host "%organism%" }}
{{- else }}
  {{- $lapisUrlTemplate = printf "http://%s:8080/%%organism%%" $.Values.localHost }}
{{- end }}
{{- $externalLapisUrlConfig := dict "lapisUrlTemplate" $lapisUrlTemplate "config" $.Values }}
            "backendUrl": "{{ include "loculus.backendUrl" . }}",
            "lapisUrls": {{- include "loculus.generateExternalLapisUrls" $externalLapisUrlConfig | fromYaml | toJson }},
            "keycloakUrl":  "{{ include "loculus.keycloakUrl" . }}"
{{- end }}


{{/* Generate ENA submission config from passed config object */}}
{{- define "loculus.generateENASubmissionConfig" }}
enaOrganisms:
  {{- range $_, $item := (include "loculus.enabledOrganisms" . | fromJson).organisms }}
{{- $key := $item.key }}
{{- $instance := $item.contents }}
  {{- if $instance.enaDeposition }}
  {{- range $suborganismName, $configFile := $instance.enaDeposition -}}
  {{- if eq $suborganismName "singleReference" }}
  {{ $key }}:
  {{- else }}
  {{ $suborganismName }}:
    loculusOrganism: {{ quote $key }}
  {{- end }}
  {{- with $instance.schema }}
    {{ $configFile.configFile | toYaml | nindent 4 }}
    {{- if $configFile.referenceIdentifierField }}
    referenceIdentifierField: {{ quote $configFile.referenceIdentifierField }}
    {{- end }}
    organismName: {{ quote .organismName }}
    {{- $rawUniqueSegments := (include "loculus.getNucleotideSegmentNames" $instance.referenceGenomes | fromYaml).segments }}
    segments: {{ $rawUniqueSegments | toYaml | nindent 6 }}
    externalMetadata:
      {{- $args := dict
        "metadata" (include "loculus.patchMetadataSchema" . | fromYaml).metadata
        "referenceGenomes" $instance.referenceGenomes
      }}
      {{-  $metadata := include "loculus.generateBackendExternalMetadata" $args | fromYaml }}
      {{- $metadata.fields | default list | toYaml | nindent 6 }}
    {{- end }}
  {{- end }}
  {{- end }}
  {{- end }}
{{- end }}
