# Extension Registry Validator
# Multi-instance configuration validator

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

{
  validate_registry_config | registry_schema.RegistryConfig -> Array String = fun config =>
    let errors = [] in

    # Server port validation
    let errors = if config.server.port < 1024 || config.server.port > 65535
                 then errors @ ["Server port must be between 1024 and 65535"]
                 else errors in

    # Workers validation
    let errors = if config.server.workers < constraints.registry.workers.min
                 then errors @ ["Workers below minimum (#{constraints.registry.workers.min})"]
                 else if config.server.workers > constraints.registry.workers.max
                      then errors @ ["Workers above maximum (#{constraints.registry.workers.max})"]
                      else errors in

    # Cache capacity validation
    let errors = if config.cache.capacity < constraints.registry.cache_capacity.min
                 then errors @ ["Cache capacity below minimum (#{constraints.registry.cache_capacity.min})"]
                 else if config.cache.capacity > constraints.registry.cache_capacity.max
                      then errors @ ["Cache capacity above maximum (#{constraints.registry.cache_capacity.max})"]
                      else errors in

    # Cache TTL validation
    let errors = if config.cache.ttl_seconds < constraints.registry.cache_ttl.min
                 then errors @ ["Cache TTL below minimum (#{constraints.registry.cache_ttl.min})"]
                 else if config.cache.ttl_seconds > constraints.registry.cache_ttl.max
                      then errors @ ["Cache TTL above maximum (#{constraints.registry.cache_ttl.max})"]
                      else errors in

    # Validate multi-instance Gitea configurations
    let validate_gitea_instances = fun instances =>
      std.array.fold (fun acc inst =>
        let inst_errors = [] in
        let inst_errors = if inst.url == ""
                         then inst_errors @ ["Gitea URL cannot be empty"]
                         else inst_errors in
        let inst_errors = if inst.organization == ""
                         then inst_errors @ ["Gitea organization cannot be empty"]
                         else inst_errors in
        let inst_errors = if inst.token_path == ""
                         then inst_errors @ ["Gitea token_path cannot be empty"]
                         else inst_errors in
        acc @ inst_errors
      ) [] instances in

    let errors = errors @ validate_gitea_instances config.sources.gitea in

    # Validate multi-instance Forgejo configurations
    let validate_forgejo_instances = fun instances =>
      std.array.fold (fun acc inst =>
        let inst_errors = [] in
        let inst_errors = if inst.url == ""
                         then inst_errors @ ["Forgejo URL cannot be empty"]
                         else inst_errors in
        let inst_errors = if inst.organization == ""
                         then inst_errors @ ["Forgejo organization cannot be empty"]
                         else inst_errors in
        let inst_errors = if inst.token_path == ""
                         then inst_errors @ ["Forgejo token_path cannot be empty"]
                         else inst_errors in
        acc @ inst_errors
      ) [] instances in

    let errors = errors @ validate_forgejo_instances config.sources.forgejo in

    # Validate multi-instance GitHub configurations
    let validate_github_instances = fun instances =>
      std.array.fold (fun acc inst =>
        let inst_errors = [] in
        let inst_errors = if inst.organization == ""
                         then inst_errors @ ["GitHub organization cannot be empty"]
                         else inst_errors in
        let inst_errors = if inst.token_path == ""
                         then inst_errors @ ["GitHub token_path cannot be empty"]
                         else inst_errors in
        acc @ inst_errors
      ) [] instances in

    let errors = errors @ validate_github_instances config.sources.github in

    # Validate multi-instance OCI configurations
    let validate_oci_instances = fun instances =>
      std.array.fold (fun acc inst =>
        let inst_errors = [] in
        let inst_errors = if inst.registry == ""
                         then inst_errors @ ["OCI registry cannot be empty"]
                         else inst_errors in
        let inst_errors = if inst.namespace == ""
                         then inst_errors @ ["OCI namespace cannot be empty"]
                         else inst_errors in
        acc @ inst_errors
      ) [] instances in

    let errors = errors @ validate_oci_instances config.distributions.oci in

    # At least one backend must be configured
    let has_sources = std.array.length config.sources.gitea > 0
                   || std.array.length config.sources.forgejo > 0
                   || std.array.length config.sources.github > 0 in
    let has_distributions = std.array.length config.distributions.oci > 0 in
    let has_legacy_gitea = config.gitea != null in
    let has_legacy_oci = config.oci != null in

    let errors = if !has_sources && !has_distributions && !has_legacy_gitea && !has_legacy_oci
                 then errors @ ["At least one backend must be configured (sources or distributions)"]
                 else errors in

    errors,
}
