# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
module Auth0
  module Api
    module V2
      # Methods to use the logs endpoints
      module Logs
        attr_reader :logs_path

        # Retrieves log entries that match the specified search criteria.
        # @see https://auth0.com/docs/api/v2#!/Logs/get_logs
        # @param options [hash]
        #   * :q [string] Query in Lucene query string syntax.
        #   * :page [integer] The page number. Zero based.
        #   * :per_page [integer] The amount of entries per page. Default: 50. Max value: 100.
        #   * :sort [string] The field to use for sorting. 1 == ascending and -1 == descending.
        #   * :fields [string] A comma separated list of fields to include or exclude from the result.
        #   * :include_fields [boolean] True if the fields specified are to be included in the result, false otherwise.
        #   * :include_totals [string] True if a query summary must be included in the result, false otherwise.
        #   * :from [string] For checkpoint pagination, the ID from which to start selection from.
        #   * :take [integer] For checkpoint pagination, the number of entries to retrieve. Default is 50.
        # Default: 50. Max value: 100.
        #
        # @return [json] Returns the list of existing log entries.
        def logs(options = {})
          request_params = {
            q: options.fetch(:q, nil),
            page: options.fetch(:page, nil),
            per_page: options.fetch(:per_page, nil),
            sort: options.fetch(:sort, nil),
            fields: options.fetch(:fields, nil),
            include_fields: options.fetch(:include_fields, nil),
            include_totals: options.fetch(:include_totals, nil),
            from: options.fetch(:from, nil),
            take: options.fetch(:take, nil)
          }
          if request_params[:take].to_i > 100
            raise Auth0::InvalidParameter, 'The total amount of entries to retrieve should be less than 100'
          end
          if request_params[:per_page].to_i > 100
            raise Auth0::InvalidParameter, 'The total amount of entries per page should be less than 100'
          end
          get(logs_path, request_params)
        end
        alias get_logs logs

        # Retrieves log entries that match the specified search criteria.
        # @see https://auth0.com/docs/api/v2#!/Logs/get_logs_by_id
        # @param log_id [string] The log_id of the log to retrieve.
        #
        # @return [json] Returns the log with the given id if exists.
        def log(log_id)
          raise Auth0::MissingParameter, 'Must supply a valid log_id' if log_id.to_s.empty?
          path = "#{logs_path}/#{log_id}"
          get(path)
        end
        alias get_logs_by_id log

        # Logs API path
        def logs_path
          @logs_path ||= '/api/v2/logs'
        end
      end
    end
  end
end
