# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#	http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

module Elasticsearch
  module API
    <%- @module_namespace.each_with_index do |name, i| -%>
    <%= '  '*i %>module <%= name == 'xpack' ? 'XPack' : name.capitalize %>
    <%- end -%>
    <%= '  '*@namespace_depth %>module Actions

      <%= '  '*@namespace_depth %># <%= @spec['description'] || 'TODO: Description' %>
      <%= '  '*@namespace_depth %>#
<%# URL parts -%>
      <%- @spec['url']['parts'].each do |name,info| -%>
      <%- info['type'] = 'String' if info['type'] == 'enum' # Rename 'enums' to 'strings' -%>
      <%= '  '*@namespace_depth + "# @option arguments [#{info['type'] ? info['type'].capitalize : 'String'}] :#{name} #{info['description'] ? info['description'].strip : '[TODO]'}" + ( info['required'] ? ' (*Required*)' : '' ) -%><%= " (options: #{info['options'].join(', '.strip)})" if info['options'] -%>
      <%= "\n" -%>
      <%- end -%>
<%# Body -%>
<%= '  '*(@namespace_depth+3) + '# @option arguments [Hash] :body ' + (@spec['body']['description'] ? @spec['body']['description'].strip : 'TODO: Description') + (@spec['body']['required'] ? ' (*Required*)' : '') + "\n" if @spec['body'] -%>
<%# URL parameters -%>
      <%- @spec['url']['params'].each do |name,info| -%>
      <%- info['type'] = 'String' if info['type'] == 'enum' # Rename 'enums' to 'strings' -%>
      <%= '  '*@namespace_depth + "# @option arguments [#{info['type'] ? info['type'].capitalize : 'String'}] :#{name} #{info['description'] ? info['description'].strip : '[TODO]'}" -%><%= " (options: #{info['options'].join(', ')})" if info['options'] -%>
      <%=  "\n" -%>
      <%- end if @spec['url']['parts'] -%>
      <%= '  '*@namespace_depth -%>#
<%# Documentation link -%>
      <%= '  '*@namespace_depth %># @see <%= @spec['documentation'] ? @spec['documentation'] : "[TODO]" %>
      <%= '  '*@namespace_depth %>#
<%# Method definition -%>
      <%= '  '*@namespace_depth -%>def <%= @method_name %>(arguments={})
<%# Required arguments -%>
      <%- @spec['url']['parts'].select { |name, info| info['required'] }.each do |name, info| -%>
      <%= '  '*(@namespace_depth+1) + "raise ArgumentError, \"Required argument '#{name}' missing\" unless arguments[:#{name}]" + "\n" -%>
      <%- end -%>
<%- if @spec['body'] && @spec['body']['required'] -%>
      <%= '  '*(@namespace_depth+1) + "raise ArgumentError, \"Required argument 'body' missing\" unless arguments[:body]" + "\n" -%>
<%- end -%>
<%# Method, path, params, body  -%>
      <%- unless @spec['url']['params'].empty? -%>
      <%= '  '*@namespace_depth %>  valid_params = [
      <%= '  '*(@namespace_depth+2) %><%= @spec['url']['params'].keys.map { |k| ":#{k}" }.join(",\n#{'  '*(@namespace_depth+5)}") %> ]
      <%- end -%>
      <%= '  '*@namespace_depth %>  method = <%= @http_method %>
      <%= '  '*@namespace_depth %>  path   = "<%= @http_path %>"
      <%- unless @spec['url']['params'].empty? -%>
      <%= '  '*@namespace_depth %>  params = Elasticsearch::API::Utils.__validate_and_extract_params arguments, valid_params
      <%- else -%>
      <%= '  '*@namespace_depth %>  params = {}
      <%- end -%>
      <%= '  '*@namespace_depth %>  body   = <%= @spec['body'].nil? ? 'nil' : 'arguments[:body]' %>
<%# Perform request %>
      <%= '  '*@namespace_depth %>  perform_request(method, path, params, body).body
      <%= '  '*@namespace_depth %>end
      <%- @namespace_depth.downto(1) do |i| -%>
      <%= '  '*(i-1) %>end
      <%- end if @namespace_depth > 0 -%>
    end
  end
end
