# Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

require 'contrast/utils/object_share'

module Contrast
  module Agent
    module Reporting
      module Settings
        # All of the apis to add new logging calls to the application at runtime
        class LogEnhancer
          ATTRIBUTES = %i[id api format level name type].cs__freeze
          LOG_LEVELS = %w[TRACE DEBUG INFO WARN ERROR].cs__freeze
          TYPES = %w[AUDIT ERROR SECURITY].cs__freeze

          # @return api [String]  The method signature to instrument, as understood by the agent.
          attr_accessor :api
          # @return format [String] The format of the message to log.
          attr_accessor :format
          # @return id [Integer] The identifier of the enhancer as defined by TeamServer.
          attr_accessor :id
          # @return name [String] The user defined name of the enhancer.
          attr_accessor :name

          # @return level [String] The level at which to log this message. Trace as 0 and Error as 4.
          # [ TRACE, DEBUG, INFO, WARN, ERROR ]
          def level
            @_level ||= Contrast::Utils::ObjectShare::EMPTY_STRING
          end

          # @param new_level [String] new level to set.
          # @return level [String] The level at which to log this message. Trace as 0 and Error as 4.
          # [ TRACE, DEBUG, INFO, WARN, ERROR ]
          def level= new_level
            @_level = new_level if LOG_LEVELS.include?(new_level)
          end

          # @return type [String] The type of log message to generate. Audit as 0, Security as 2.
          # [ AUDIT, ERROR, SECURITY ]
          def type
            @_type ||= Contrast::Utils::ObjectShare::EMPTY_STRING
          end

          # @param new_type [String] new type to set.
          # @return type [String] The type of log message to generate. Audit as 0, Security as 2.
          # [ AUDIT, ERROR, SECURITY ]
          def type= new_type
            @_type = new_type if TYPES.include?(new_type)
          end

          def to_controlled_hash
            {
                id: id,
                api: api,
                format: format,
                name: name, # rubocop:disable Security/Module/Name
                level: level,
                type: type
            }
          end
        end
      end
    end
  end
end