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

require 'contrast/agent/reporting/reporting_events/reporting_event'
require 'contrast/components/assess'
require 'contrast/components/logger'
require 'contrast/utils/string_utils'

module Contrast
  module Agent
    module Reporting
      # This is the new PreflightMessage class which will include all the needed information for the new reporting
      # system to report a single message, part of the main Preflight, to TeamServer. This message represents the
      # identifying information of a Finding/Trace, which TeamServer will use to determine if it requires the full
      # information of the Finding/Trace to be reported.
      class PreflightMessage
        include Contrast::Components::Logger::InstanceMethods

        # @return [String] the message identifier; rule_id,hash
        attr_accessor :data
        # @return [String] CRC checksum of the finding to which this message pertains
        attr_accessor :hash_code
        # @return [Array<Contrast::Agent::Reporting::RouteDiscovery] the route that triggered the finding to which this
        #   preflight applies.
        attr_reader :routes

        # The type of this event, as understood by TeamServer. While TRACE and APPUPDATE are both technically valid, we
        # only use the TRACE type.
        CODE = :TRACE

        def initialize
          @routes = []
        end

        # Convert the instance variables on the class, and other information, into the identifiers required for
        # TeamServer to process the JSON form of this message.
        #
        # @return [Hash]
        # @raise [ArgumentError]
        def to_controlled_hash
          begin
            validate
          rescue ArgumentError => e
            logger.error('PreflightMessage validation failed with: ', e)
            return
          end

          {
              code: CODE,
              app_language: Contrast::Utils::ObjectShare::RUBY,
              app_name: ::Contrast::APP_CONTEXT.name, # rubocop:disable Security/Module/Name
              app_version: ::Contrast::APP_CONTEXT.version,
              data: '',
              key: 0,
              routes: @routes.map(&:to_controlled_hash),
              session_id: ::Contrast::ASSESS.session_id
          }
        end

        # @raise [ArgumentError]
        def validate
          unless Contrast::Utils::StringUtils.present?(data)
            raise(ArgumentError, "#{ cs__class } did not have a proper data. Unable to continue.")
          end
          unless ::Contrast::ASSESS.session_id
            raise(ArgumentError, "#{ cs__class } did not have a proper session id. Unable to continue.")
          end

          nil
        end
      end
    end
  end
end