# Copyright 2024 - 2025 Block, Inc.
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
#
# frozen_string_literal: true

require "elastic_graph/support/config"
require "elastic_graph/errors"
require "elastic_graph/schema_artifacts/from_disk"
require "elastic_graph/support/from_yaml_file"

module ElasticGraph
  # Namespace for all code related to ElasticGraph schema artifacts.
  # Schema artifacts are generated by {SchemaDefinition} and dumped to disk.
  # Later, they are used to power ElasticGraph.
  module SchemaArtifacts
    extend Support::FromYamlFile

    # Builds a `SchemaArtifacts::FromDisk` instance using the provided YAML settings.
    #
    # @param parsed_yaml [Hash<String, Object>] hash parsed from a settings YAML file
    # @return [FromDisk]
    def self.from_parsed_yaml(parsed_yaml)
      config = Config.from_parsed_yaml(parsed_yaml) || Config.new
      FromDisk.new(config.directory)
    end

    # @private
    Config = Support::Config.define(:directory) do
      # @implements Config
      json_schema at: "schema_artifacts",
        optional: false,
        description: "Configuration for schema artifact management used by all parts of ElasticGraph.",
        properties: {
          directory: {
            description: "Path to the directory where schema artifacts are stored.",
            examples: ["config/schema/artifacts"],
            default: "config/schema/artifacts",
            type: "string",
            minLength: 1
          }
        }
    end
  end
end
