# frozen_string_literal: true

module Types
  module Dast
    class ProfileType < BaseObject
      graphql_name 'DastProfile'
      description 'Represents a DAST Profile'

      authorize :read_on_demand_scans

      field :id, ::Types::GlobalIDType[::Dast::Profile], null: false,
            description: 'ID of the profile.'

      field :name, GraphQL::STRING_TYPE, null: true,
            description: 'The name of the profile.'

      field :description, GraphQL::STRING_TYPE, null: true,
            description: 'The description of the scan.'

      field :dast_site_profile, DastSiteProfileType, null: true,
            description: 'The associated site profile.'

      field :dast_scanner_profile, DastScannerProfileType, null: true,
            description: 'The associated scanner profile.'

      field :branch, Dast::ProfileBranchType, null: true,
            description: 'The associated branch. Will always return `null` ' \
                         'if `dast_branch_selection` feature flag is disabled.',
            calls_gitaly: true

      field :edit_path, GraphQL::STRING_TYPE, null: true,
            description: 'Relative web path to the edit page of a profile.'

      def branch
        return unless Feature.enabled?(:dast_branch_selection, object.project, default_enabled: :yaml)

        object.branch
      end

      def edit_path
        Gitlab::Routing.url_helpers.edit_project_on_demand_scan_path(object.project, object)
      end
    end
  end
end
