# frozen_string_literal: true

module Security
  class VulnerabilityUUID
    def self.generate(
      report_type:, primary_identifier_fingerprint:, location_fingerprint:, project_id:, tracked_context:
    )
      # A nil context means we're working with Security Findings that aren't on a tracked branch.
      # A default context means there's likely existing DB data using the old format and we don't want to break that
      if tracked_context.nil? || tracked_context.is_default?
        return generate_v1(report_type, primary_identifier_fingerprint, location_fingerprint, project_id)
      end

      generate_v2(report_type, primary_identifier_fingerprint, location_fingerprint, project_id, tracked_context.id)
    end

    def self.generate_v1(report_type, primary_identifier_fingerprint, location_fingerprint, project_id)
      Gitlab::UUID.v5(
        "#{report_type}-#{primary_identifier_fingerprint}-#{location_fingerprint}-#{project_id}"
      )
    end

    def self.generate_v2(report_type, primary_identifier_fingerprint, location_fingerprint, project_id, context_id)
      Gitlab::UUID.v5(
        "#{report_type}-#{primary_identifier_fingerprint}-#{location_fingerprint}-#{project_id}-#{context_id}"
      )
    end
  end
end
