{{/* Returns sections from front matter, linked data, or from data. */}}
{{- $sections := .Params.sections -}}
{{- $source := "front matter" -}}

{{/* 1) Linked-data: sections_source */}}
{{- with .Params.sections_source -}}
  {{- $key := . -}}
  {{- if hasPrefix $key "pages/" -}}{{- $key = (replace $key "pages/" "") -}}{{- end -}}
  {{- with index hugo.Data.pages $key -}}
    {{- $base := .sections -}}
    {{- if $sections -}}
      {{- $merged := slice -}}
      {{- range $i, $b := $base -}}
        {{- $ov := index $sections $i -}}
        {{- if $ov -}}
          {{- $merged = $merged | append (partial "functions/deep_merge" (dict "a" $b "b" $ov)) -}}
        {{- else -}}
          {{- $merged = $merged | append $b -}}
        {{- end -}}
      {{- end -}}
      {{- if gt (len $sections) (len $base) -}}
        {{- range $i, $ov := $sections -}}
          {{- if ge $i (len $base) -}}
            {{- $merged = $merged | append $ov -}}
          {{- end -}}
        {{- end -}}
      {{- end -}}
      {{- $sections = $merged -}}
    {{- else -}}
      {{- $sections = $base -}}
    {{- end -}}
    {{- $source = printf "data/pages/%s.yaml (sections_source)" $key -}}
  {{- else -}}
    {{- partial "functions/logger" (dict "page" . "level" "warn" "id" (printf "sections-source-missing-%s" $key) "message" (printf "sections_source '%s' not found under data/pages/" $key) "source" (dict "type" "partial" "name" "hbx/sections")) -}}
  {{- end -}}
{{- end -}}

{{/* 2) Fallbacks if still no sections */}}
{{- if not $sections -}}
  {{/* Fallback: Legacy data-driven pages */}}
  {{- $p := "home" -}}{{/* Default to "home" for homepage */}}
  {{- with .File -}}
    {{- $basename := .ContentBaseName -}}
    {{- if $basename -}}
      {{- $p = $basename -}}
      {{/* Special case: homepage _index.md should use "home" data file */}}
      {{- if eq $p "_index" -}}
        {{- $p = "home" -}}
      {{- end -}}
    {{- end -}}
  {{- end -}}
  {{/* Ensure homepage always uses "home" data file */}}
  {{- if eq $.RelPermalink "/" -}}
    {{- $p = "home" -}}
  {{- end -}}
  {{- with index hugo.Data.pages $p -}}
    {{- $sections = .sections -}}
    {{- $source = printf "data/pages/%s.yaml" $p -}}
  {{- end -}}
{{- end -}}

{{/* 3) Per-section ref: load from data/blocks/<slug>.yaml and deep-merge with inline (inline wins) */}}
{{- if $sections -}}
  {{- $resolved := slice -}}
  {{- range $i, $s := $sections -}}
    {{- $sec := $s -}}
    {{- with $s.ref -}}
      {{- $ref := . | lower -}}
      {{- $path := cond (hasPrefix $ref "blocks/") $ref (printf "blocks/%s" $ref) -}}
      {{- $parts := split $path "/" -}}
      {{- $data := hugo.Data -}}
      {{- range $j, $p := $parts -}}
        {{- if $data -}}
          {{- $data = index $data $p -}}
        {{- end -}}
      {{- end -}}
      {{- if $data -}}
        {{- /* shallow merge */ -}}
        {{- $merged := merge $data $sec -}}
        {{- /* merge 'content' */ -}}
        {{- $ac := index $data "content" -}}{{- $bc := index $sec "content" -}}
        {{- if and (reflect.IsMap $ac) (reflect.IsMap $bc) -}}
          {{- $merged = merge $merged (dict "content" (merge $ac $bc)) -}}
        {{- end -}}
        {{- /* merge 'design' */ -}}
        {{- $ad := index $data "design" -}}{{- $bd := index $sec "design" -}}
        {{- if and (reflect.IsMap $ad) (reflect.IsMap $bd) -}}
          {{- $merged = merge $merged (dict "design" (merge $ad $bd)) -}}
        {{- end -}}
        {{- $sec = $merged -}}
      {{- else -}}
        {{- partial "functions/logger" (dict "page" $ "level" "warn" "id" (printf "section-ref-missing-%s" $ref) "message" (printf "HBX: section ref '%s' not found under data/blocks/" $ref) "source" (dict "type" "partial" "name" "hbx/sections")) -}}
      {{- end -}}
    {{- end -}}
    {{- $resolved = $resolved | append $sec -}}
  {{- end -}}
  {{- $sections = $resolved -}}
{{- end -}}

{{- if $sections -}}
  {{- partial "functions/logger" (dict "page" . "level" "info" "message" (printf "✅  Using sections from %s for page %s (found %d sections)" $source $.RelPermalink (len $sections)) "source" (dict "type" "partial" "name" "hbx/sections")) -}}
{{- else -}}
  {{- partial "functions/logger" (dict "page" . "level" "warn" "id" (printf "sections-none-%s" $.RelPermalink) "message" (printf "HBX: No sections found for page %s (checked %s)" $.RelPermalink $source) "source" (dict "type" "partial" "name" "hbx/sections")) -}}
{{- end -}}
{{- return $sections -}}
