# frozen_string_literal: true

module Gitlab
  module GithubImport
    module Stage
      class ImportPullRequestsWorker # rubocop:disable Scalability/IdempotentWorker
        include ApplicationWorker

        data_consistency :always

        include StageMethods

        resumes_work_when_interrupted!

        # client - An instance of Gitlab::GithubImport::Client.
        # project - An instance of Project.
        def import(client, project)
          info(
            project.id,
            message: "starting importer",
            importer: 'Importer::PullRequestsImporter',
            Labkit::Fields::GL_ORGANIZATION_ID => project.organization_id
          )

          waiter = Importer::PullRequestsImporter
            .new(project, client)
            .execute

          AdvanceStageWorker.perform_async(
            project.id,
            { waiter.key => waiter.jobs_remaining },
            'collaborators'
          )
        end
      end
    end
  end
end
