# SPDX-License-Identifier: Apache-2.0
#
#  The OpenSearch Contributors require contributions made to
#  this file be licensed under the Apache-2.0 license or a
#  compatible open source license.
#
#  Modifications Copyright OpenSearch Contributors. See
#  GitHub history for details.

module LogStash; module Outputs; class OpenSearch
  class TemplateManager
    # To be mixed into the opensearch plugin base
    def self.install_template(plugin)
      return unless plugin.manage_template
      if plugin.template
        plugin.logger.info("Using mapping template from", :path => plugin.template)
        template = read_template_file(plugin.template)
      else
        plugin.logger.info("Using a default mapping template", :version => plugin.maximum_seen_major_version,
                                                               :ecs_compatibility => plugin.ecs_compatibility)
        template = load_default_template(plugin.maximum_seen_major_version, plugin.ecs_compatibility, plugin.client.legacy_template?)
      end

      plugin.logger.debug("Attempting to install template", template: template)
      install(plugin.client, template_name(plugin), template, plugin.template_overwrite)
    end

    private
    def self.load_default_template(major_version, ecs_compatibility, legacy_template)
      template_path = default_template_path(major_version, ecs_compatibility, legacy_template)
      read_template_file(template_path)
    rescue => e
      fail "Failed to load default template for OpenSearch v#{major_version} with ECS #{ecs_compatibility}; caused by: #{e.inspect}"
    end

    def self.install(client, template_name, template, template_overwrite)
      client.template_install(template_name, template, template_overwrite)
    end

    def self.template_settings(plugin, template)
      template['settings']
    end

    def self.template_name(plugin)
      plugin.template_name
    end

    def self.default_template_path(major_version, ecs_compatibility=:disabled, legacy_template=true)
      template_version = major_version
      suffix = legacy_template ? "" : "_index"
      default_template_name = "templates/ecs-#{ecs_compatibility}/#{template_version}x#{suffix}.json"
      ::File.expand_path(default_template_name, ::File.dirname(__FILE__))
    end

    def self.read_template_file(template_path)
      raise ArgumentError, "Template file '#{template_path}' could not be found" unless ::File.exists?(template_path)
      template_data = ::IO.read(template_path)
      LogStash::Json.load(template_data)
    end
  end
end end end
