# frozen_string_literal: true

module Resolvers
  class VulnerabilitySeveritiesCountResolver < VulnerabilitiesBaseResolver
    include Gitlab::Utils::StrongMemoize

    type Types::VulnerabilitySeveritiesCountType, null: true

    argument :project_id, [GraphQL::ID_TYPE],
             required: false,
             description: 'Filter vulnerabilities by project.'

    argument :report_type, [Types::VulnerabilityReportTypeEnum],
             required: false,
             description: 'Filter vulnerabilities by report type.'

    argument :severity, [Types::VulnerabilitySeverityEnum],
             required: false,
             description: 'Filter vulnerabilities by severity.'

    argument :state, [Types::VulnerabilityStateEnum],
             required: false,
             description: 'Filter vulnerabilities by state.'

    argument :scanner, [GraphQL::STRING_TYPE],
             required: false,
             description: 'Filter vulnerabilities by scanner.'

    def resolve(**args)
      return Vulnerability.none unless vulnerable

      Hash.new(0)
        .merge(vulnerabilities(args).grouped_by_severity.count)
    end

    private

    def vulnerabilities(filters)
      Security::VulnerabilitiesFinder.new(vulnerable, filters).execute
    end
  end
end
