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

require 'contrast/components/logger'
require 'contrast/agent/reporting/reporting_events/reporting_event'

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.
      #
      # @attr_accessor data [String] the message identifier; rule_id,hash
      # @attr_accessor hash_code [String]
      # @attr_reader routes [Array<Contrast::Agent::Reporting::RouteDiscovery] the route that triggered the finding
      #   to which this preflight applies.
      class PreflightMessage
        attr_accessor :data, :hash_code
        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
          @app_language = Contrast::Utils::ObjectShare::RUBY
          @app_name = ::Contrast::APP_CONTEXT.app_name
          @app_version = ::Contrast::APP_CONTEXT.app_version
          @routes = []
          @agent_session_id_value = Contrast::APP_CONTEXT.session_id
        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
          validate
          {
              code: CODE,
              app_language: @app_language,
              app_name: @app_name,
              app_version: @app_version,
              data: '',
              key: 0,
              routes: @routes,
              session_id: @agent_session_id_value
          }
        end

        def validate
          raise(ArgumentError, "#{ cs__class } did not have a proper data. Unable to continue.") unless data
          raise(ArgumentError, "#{ cs__class } did not have a proper routes. Unable to continue.") if routes.empty?
          unless @app_name
            raise(ArgumentError, "#{ cs__class } did not have a proper application name. Unable to continue.")
          end
          unless @app_language
            raise(ArgumentError, "#{ cs__class } did not have a proper application language. Unable to continue.")
          end
          unless @app_version
            raise(ArgumentError, "#{ cs__class } did not have a proper application version. Unable to continue.")
          end
          unless @agent_session_id_value
            raise(ArgumentError, "#{ cs__class } did not have a proper session id. Unable to continue.")
          end

          nil
        end
      end
    end
  end
end