# frozen_string_literal: true

module Security
  # Service for counting the number of vulnerability findings for
  # an array of report types within a pipeline
  #
  class VulnerabilityCountingService
    # @param [Ci::Pipeline] pipeline
    # @param Array[String] report_types Summary report types. Valid values are members of Vulnerabilities::Finding::REPORT_TYPES
    def initialize(pipeline, report_types)
      @pipeline = pipeline
      @report_types = report_types
    end

    def execute
      vulnerabilities = ::Security::PipelineVulnerabilitiesFinder.new(pipeline: @pipeline, params: { report_type: @report_types }).execute
      vulnerabilities.findings.group_by(&:report_type).compact.transform_values(&:size).reverse_merge(no_counts)
    end

    private

    def no_counts
      @report_types.zip([0].cycle).to_h
    end
  end
end
