# frozen_string_literal: true
# Authenticate the assessment and update the session_id in submission.
class Course::Assessment::SessionLogService
  # @param [Course::Assessment] assessment The password protected assessment.
  # @param [string] session_id The current session ID.
  # @param [Course::Assessment::Submission] submission The current submission.
  def initialize(assessment, session_id, submission)
    @assessment = assessment
    @session_id = session_id
    @submission = submission
  end

  # Log submission access attempts for password-protected assessments.
  def log_submission_access(request)
    request_headers = request.headers.env.select do |k, _|
      k.in?(ActionDispatch::Http::Headers::CGI_VARIABLES) || k =~ /^HTTP_/
    end

    request_headers['USER_SESSION_ID'] = current_authentication_token
    request_headers['SUBMISSION_SESSION_ID'] = @submission.session_id

    @submission.logs.create(request: request_headers)
  end

  private

  def current_authentication_token
    REDIS.get(session_key)
  end

  def session_key
    "session_#{@session_id}_assessment_#{@assessment.id}_submission_#{@submission.id}_authentication_token"
  end
end
