# frozen_string_literal: true

module Types
  class VulnerabilityLocationType < BaseUnion
    UnexpectedReportType = Class.new(StandardError)

    graphql_name 'VulnerabilityLocation'
    description 'Represents a vulnerability location. The fields with data will depend on the vulnerability report type'

    possible_types VulnerabilityLocation::ContainerScanningType,
                   VulnerabilityLocation::DependencyScanningType,
                   VulnerabilityLocation::DastType,
                   VulnerabilityLocation::SastType,
                   VulnerabilityLocation::SecretDetectionType,
                   VulnerabilityLocation::CoverageFuzzingType

    def self.resolve_type(object, context)
      case object[:report_type]
      when 'container_scanning'
        VulnerabilityLocation::ContainerScanningType
      when 'dependency_scanning'
        VulnerabilityLocation::DependencyScanningType
      when 'dast', 'api_fuzzing'
        VulnerabilityLocation::DastType
      when 'sast'
        VulnerabilityLocation::SastType
      when 'secret_detection'
        VulnerabilityLocation::SecretDetectionType
      when 'coverage_fuzzing'
        VulnerabilityLocation::CoverageFuzzingType
      else
        raise UnexpectedReportType, "Report type must be one of #{::Vulnerabilities::Finding::REPORT_TYPES.keys}"
      end
    end
  end
end
