# Extension Registry Configuration - Nickel Format (Multi-Instance)
# Auto-generated by provisioning TypeDialog
# Edit via: nu provisioning/.typedialog/provisioning/platform/scripts/configure.nu extension-registry {mode}
# Or manually edit and validate with: nickel typecheck

let registry_schema = import "../schemas/extension-registry.ncl" in

{
  extension_registry | registry_schema.RegistryConfig = {
    # Server Configuration
    server = {
      {%- if server_host %}
      host = "{{ server_host }}",
      {%- endif %}
      {%- if server_port %}
      port = {{ server_port }},
      {%- endif %}
      {%- if server_workers %}
      workers = {{ server_workers }},
      {%- endif %}
      {%- if server_enable_cors is defined %}
      enable_cors = {{ server_enable_cors | lower }},
      {%- endif %}
      {%- if server_enable_compression is defined %}
      enable_compression = {{ server_enable_compression | lower }},
      {%- endif %}
    },

    # Git-based Source Backends (Multi-Instance)
    {%- if gitea_instances %}
    sources = {
      # Gitea instances
      {%- if gitea_instances | length > 0 %}
      gitea = [
        {%- for instance in gitea_instances %}
        {
          {%- if instance.id %}
          id = "{{ instance.id }}",
          {%- endif %}
          url = "{{ instance.url }}",
          organization = "{{ instance.organization }}",
          token_path = "{{ instance.token_path }}",
          {%- if instance.timeout_seconds %}
          timeout_seconds = {{ instance.timeout_seconds }},
          {%- endif %}
          {%- if instance.verify_ssl is defined %}
          verify_ssl = {{ instance.verify_ssl | lower }},
          {%- endif %}
        },
        {%- endfor %}
      ],
      {%- endif %}

      # Forgejo instances
      {%- if forgejo_instances | length > 0 %}
      forgejo = [
        {%- for instance in forgejo_instances %}
        {
          {%- if instance.id %}
          id = "{{ instance.id }}",
          {%- endif %}
          url = "{{ instance.url }}",
          organization = "{{ instance.organization }}",
          token_path = "{{ instance.token_path }}",
          {%- if instance.timeout_seconds %}
          timeout_seconds = {{ instance.timeout_seconds }},
          {%- endif %}
          {%- if instance.verify_ssl is defined %}
          verify_ssl = {{ instance.verify_ssl | lower }},
          {%- endif %}
        },
        {%- endfor %}
      ],
      {%- endif %}

      # GitHub instances
      {%- if github_instances | length > 0 %}
      github = [
        {%- for instance in github_instances %}
        {
          {%- if instance.id %}
          id = "{{ instance.id }}",
          {%- endif %}
          organization = "{{ instance.organization }}",
          token_path = "{{ instance.token_path }}",
          {%- if instance.timeout_seconds %}
          timeout_seconds = {{ instance.timeout_seconds }},
          {%- endif %}
          {%- if instance.verify_ssl is defined %}
          verify_ssl = {{ instance.verify_ssl | lower }},
          {%- endif %}
        },
        {%- endfor %}
      ],
      {%- endif %}
    },
    {%- endif %}

    # OCI Registry Distribution Backends (Multi-Instance)
    {%- if oci_instances %}
    distributions = {
      oci = [
        {%- for instance in oci_instances %}
        {
          {%- if instance.id %}
          id = "{{ instance.id }}",
          {%- endif %}
          registry = "{{ instance.registry }}",
          namespace = "{{ instance.namespace }}",
          {%- if instance.auth_token_path %}
          auth_token_path = "{{ instance.auth_token_path }}",
          {%- endif %}
          {%- if instance.timeout_seconds %}
          timeout_seconds = {{ instance.timeout_seconds }},
          {%- endif %}
          {%- if instance.verify_ssl is defined %}
          verify_ssl = {{ instance.verify_ssl | lower }},
          {%- endif %}
        },
        {%- endfor %}
      ],
    },
    {%- endif %}

    # Cache Configuration
    {%- if cache_capacity or cache_ttl_seconds %}
    cache = {
      {%- if cache_capacity %}
      capacity = {{ cache_capacity }},
      {%- endif %}
      {%- if cache_ttl_seconds %}
      ttl_seconds = {{ cache_ttl_seconds }},
      {%- endif %}
      {%- if cache_enable_metadata_cache is defined %}
      enable_metadata_cache = {{ cache_enable_metadata_cache | lower }},
      {%- endif %}
      {%- if cache_enable_list_cache is defined %}
      enable_list_cache = {{ cache_enable_list_cache | lower }},
      {%- endif %}
    },
    {%- endif %}

    # Legacy Single-Instance Configuration (Auto-Migrated)
    # Only used for backward compatibility with older configs
    {%- if legacy_gitea_url %}
    gitea = {
      url = "{{ legacy_gitea_url }}",
      organization = "{{ legacy_gitea_organization }}",
      token_path = "{{ legacy_gitea_token_path }}",
      {%- if legacy_gitea_timeout_seconds %}
      timeout_seconds = {{ legacy_gitea_timeout_seconds }},
      {%- endif %}
      {%- if legacy_gitea_verify_ssl is defined %}
      verify_ssl = {{ legacy_gitea_verify_ssl | lower }},
      {%- endif %}
    },
    {%- endif %}

    {%- if legacy_oci_registry %}
    oci = {
      registry = "{{ legacy_oci_registry }}",
      namespace = "{{ legacy_oci_namespace }}",
      {%- if legacy_oci_auth_token_path %}
      auth_token_path = "{{ legacy_oci_auth_token_path }}",
      {%- endif %}
      {%- if legacy_oci_timeout_seconds %}
      timeout_seconds = {{ legacy_oci_timeout_seconds }},
      {%- endif %}
      {%- if legacy_oci_verify_ssl is defined %}
      verify_ssl = {{ legacy_oci_verify_ssl | lower }},
      {%- endif %}
    },
    {%- endif %}
  },
}
