{{/*
  HugoBlox Theme Engine: Generates CSS Variables for Light and Dark modes
  
  Input: Theme config dict from get_theme_config helper:
    - mode: 'light' | 'dark' | 'system'
    - light: Light theme pack name
    - dark: Dark theme pack name
    - colors: Global color overrides { primary, secondary, neutral }
    - colors_light: Light-mode specific color overrides
    - colors_dark: Dark-mode specific color overrides
    - surfaces: Semantic surface color overrides
*/}}

{{ $config := . }}
{{ $light_theme_name := $config.light | default "default" }}
{{ $dark_theme_name := $config.dark | default "default" }}
{{ $global_colors := $config.colors | default dict }}
{{ $colors_light := $config.colors_light | default dict }}
{{ $colors_dark := $config.colors_dark | default dict }}
{{ $surfaces_overrides := $config.surfaces | default dict }}

{{/* Load Theme Data using the new loader */}}
{{ if not hugo.Data.themes }}
  {{ warnf "THEME ENGINE ERROR: hugo.Data.themes is empty! Check modules/blox-tailwind/data/themes/" }}
{{ end }}

{{ $light_theme := partial "functions/load_theme_pack" $light_theme_name }}
{{ $dark_theme := partial "functions/load_theme_pack" $dark_theme_name }}

{{/* Fallback for missing themes */}}
{{ if not $light_theme }}
  {{ warnf "THEME ENGINE WARNING: Light theme '%s' not found or invalid. Defaulting to 'default'." $light_theme_name }}
  {{ $light_theme = partial "functions/load_theme_pack" "default" }}
{{ end }}
{{ if not $dark_theme }}
  {{ warnf "THEME ENGINE WARNING: Dark theme '%s' not found or invalid. Defaulting to 'default'." $dark_theme_name }}
  {{ $dark_theme = partial "functions/load_theme_pack" "default" }}
{{ end }}

{{/* Resolve effective light/dark mode objects from theme files */}}
{{ $light_base := $light_theme.light | default $light_theme }}
{{ $dark_base := $dark_theme.dark | default $dark_theme }}

{{/* ═══════════════════════════════════════════════════════════════════════════
    APPLY COLOR OVERRIDES
    Priority: colors_light/colors_dark → colors (global) → theme pack defaults
═══════════════════════════════════════════════════════════════════════════ */}}

{{/* Apply global color overrides to LIGHT mode */}}
{{ if $light_base }}
  {{ $light_colors := merge (dict) ($light_base.colors | default dict) }}
  
  {{/* Global overrides */}}
  {{ with $global_colors.primary }}
    {{ $light_colors = merge $light_colors (dict "primary" .) }}
  {{ end }}
  {{ with $global_colors.secondary }}
    {{ $light_colors = merge $light_colors (dict "secondary" .) }}
  {{ end }}
  {{ with $global_colors.neutral }}
    {{ $light_colors = merge $light_colors (dict "neutral" .) }}
  {{ end }}
  
  {{/* Mode-specific overrides (highest priority) */}}
  {{ with $colors_light.primary }}
    {{ $light_colors = merge $light_colors (dict "primary" .) }}
  {{ end }}
  {{ with $colors_light.secondary }}
    {{ $light_colors = merge $light_colors (dict "secondary" .) }}
  {{ end }}
  {{ with $colors_light.neutral }}
    {{ $light_colors = merge $light_colors (dict "neutral" .) }}
  {{ end }}
  
  {{ $light_base = merge $light_base (dict "colors" $light_colors) }}
  
  {{/* Apply surface overrides (only non-empty values) */}}
  {{ if $surfaces_overrides }}
    {{ $light_surfaces := merge (dict) ($light_base.surfaces | default dict) }}
    {{ with $surfaces_overrides.background }}
      {{ if ne . "" }}
        {{ $light_surfaces = merge $light_surfaces (dict "background" .) }}
      {{ end }}
    {{ end }}
    {{ with $surfaces_overrides.foreground }}
      {{ if ne . "" }}
        {{ $light_surfaces = merge $light_surfaces (dict "foreground" .) }}
      {{ end }}
    {{ end }}
    {{ with $surfaces_overrides.header }}
      {{ $header := merge (dict) ($light_surfaces.header | default dict) }}
      {{ with .background }}
        {{ if ne . "" }}
          {{ $header = merge $header (dict "background" .) }}
        {{ end }}
      {{ end }}
      {{ with .foreground }}
        {{ if ne . "" }}
          {{ $header = merge $header (dict "foreground" .) }}
        {{ end }}
      {{ end }}
      {{ if gt (len $header) 0 }}
        {{ $light_surfaces = merge $light_surfaces (dict "header" $header) }}
      {{ end }}
    {{ end }}
    {{ with $surfaces_overrides.footer }}
      {{ $footer := merge (dict) ($light_surfaces.footer | default dict) }}
      {{ with .background }}
        {{ if ne . "" }}
          {{ $footer = merge $footer (dict "background" .) }}
        {{ end }}
      {{ end }}
      {{ with .foreground }}
        {{ if ne . "" }}
          {{ $footer = merge $footer (dict "foreground" .) }}
        {{ end }}
      {{ end }}
      {{ if gt (len $footer) 0 }}
        {{ $light_surfaces = merge $light_surfaces (dict "footer" $footer) }}
      {{ end }}
    {{ end }}
    {{ $light_base = merge $light_base (dict "surfaces" $light_surfaces) }}
  {{ end }}
{{ end }}

{{/* Apply global color overrides to DARK mode */}}
{{ if $dark_base }}
  {{ $dark_colors := merge (dict) ($dark_base.colors | default dict) }}
  
  {{/* Global overrides */}}
  {{ with $global_colors.primary }}
    {{ $dark_colors = merge $dark_colors (dict "primary" .) }}
  {{ end }}
  {{ with $global_colors.secondary }}
    {{ $dark_colors = merge $dark_colors (dict "secondary" .) }}
  {{ end }}
  {{ with $global_colors.neutral }}
    {{ $dark_colors = merge $dark_colors (dict "neutral" .) }}
  {{ end }}
  
  {{/* Mode-specific overrides (highest priority) */}}
  {{ with $colors_dark.primary }}
    {{ $dark_colors = merge $dark_colors (dict "primary" .) }}
  {{ end }}
  {{ with $colors_dark.secondary }}
    {{ $dark_colors = merge $dark_colors (dict "secondary" .) }}
  {{ end }}
  {{ with $colors_dark.neutral }}
    {{ $dark_colors = merge $dark_colors (dict "neutral" .) }}
  {{ end }}
  
  {{ $dark_base = merge $dark_base (dict "colors" $dark_colors) }}
  
  {{/* Apply surface overrides (only non-empty values) */}}
  {{ if $surfaces_overrides }}
    {{ $dark_surfaces := merge (dict) ($dark_base.surfaces | default dict) }}
    {{ with $surfaces_overrides.background }}
      {{ if ne . "" }}
        {{ $dark_surfaces = merge $dark_surfaces (dict "background" .) }}
      {{ end }}
    {{ end }}
    {{ with $surfaces_overrides.foreground }}
      {{ if ne . "" }}
        {{ $dark_surfaces = merge $dark_surfaces (dict "foreground" .) }}
      {{ end }}
    {{ end }}
    {{ with $surfaces_overrides.header }}
      {{ $header := merge (dict) ($dark_surfaces.header | default dict) }}
      {{ with .background }}
        {{ if ne . "" }}
          {{ $header = merge $header (dict "background" .) }}
        {{ end }}
      {{ end }}
      {{ with .foreground }}
        {{ if ne . "" }}
          {{ $header = merge $header (dict "foreground" .) }}
        {{ end }}
      {{ end }}
      {{ if gt (len $header) 0 }}
        {{ $dark_surfaces = merge $dark_surfaces (dict "header" $header) }}
      {{ end }}
    {{ end }}
    {{ with $surfaces_overrides.footer }}
      {{ $footer := merge (dict) ($dark_surfaces.footer | default dict) }}
      {{ with .background }}
        {{ if ne . "" }}
          {{ $footer = merge $footer (dict "background" .) }}
        {{ end }}
      {{ end }}
      {{ with .foreground }}
        {{ if ne . "" }}
          {{ $footer = merge $footer (dict "foreground" .) }}
        {{ end }}
      {{ end }}
      {{ if gt (len $footer) 0 }}
        {{ $dark_surfaces = merge $dark_surfaces (dict "footer" $footer) }}
      {{ end }}
    {{ end }}
    {{ $dark_base = merge $dark_base (dict "surfaces" $dark_surfaces) }}
  {{ end }}
{{ end }}

{{/* ═══════════════════════════════════════════════════════════════════════════
    GENERATE CSS OUTPUT
═══════════════════════════════════════════════════════════════════════════ */}}

<style>
  /* stylelint-disable */
  /* HugoBlox Theme Engine - Light: {{ $light_theme_name }}, Dark: {{ $dark_theme_name }} */
  
  /* 1. Light Mode (Default :root) */
  :root {
    {{ if $light_base }}
      {{ partial "functions/theme_variables" (dict "data" $light_base "is_dark" false) | safeCSS }}
    {{ else }}
      /* ERROR: Failed to load light theme '{{ $light_theme_name }}' */
    {{ end }}
  }

  /* 2. Dark Mode (.dark class) */
  .dark {
    {{ if $dark_base }}
      {{ partial "functions/theme_variables" (dict "data" $dark_base "is_dark" true) | safeCSS }}
    {{ else }}
      /* ERROR: Failed to load dark theme '{{ $dark_theme_name }}' */
    {{ end }}
  }
  /* stylelint-enable */
</style>
