# 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'
require 'contrast/agent/version'
require 'base64'

module Contrast
  module Agent
    module Reporting
      # This class will build the required headers for agent reporting to TS
      class Headers
        attr_reader :app_name, :api_key, :agent_version, :app_language, :app_path, :app_version, :authorization,
                    :server_name, :server_path, :server_type, :content_type, :encoding

        ENCODING = 'base64'
        CONTENT_TYPE = 'application/json'

        include Contrast::Utils::ObjectShare

        def initialize
          @app_name = Base64.strict_encode64(Contrast::APP_CONTEXT.app_name)
          @api_key = Contrast::API.api_key
          @agent_version = [RUBY, Contrast::Agent::VERSION].join(SPACE)
          @app_language = RUBY
          @app_path = Base64.strict_encode64(Contrast::APP_CONTEXT.path)
          @app_version = Contrast::APP_CONTEXT.app_version
          @authorization = Base64.strict_encode64("#{ Contrast::API.username }:#{ Contrast::API.service_key }")
          @server_name = Base64.strict_encode64(Contrast::APP_CONTEXT.server_name)
          @server_path = Base64.strict_encode64(Contrast::APP_CONTEXT.server_path)
          @server_type = Base64.strict_encode64(Contrast::APP_CONTEXT.server_type)
          @content_type = CONTENT_TYPE
          @encoding = ENCODING
        end

        def to_hash
          {
              app_name: @app_name,
              api_key: @api_key,
              agent_version: @agent_version,
              app_language: @app_language,
              app_path: @app_path,
              app_version: @app_version,
              authorization: @authorization,
              server_name: @server_name,
              server_path: @server_path,
              server_type: @server_type,
              content_type: @content_type,
              encoding: @encoding
          }.cs__freeze
        end
      end
    end
  end
end