# typed: true
module HasBulkEnqueueJobs
  extend ActiveSupport::Concern
  included do
    def bulk_enqueue_jobs(&block)
      # must set total_limit to 0 before generating jobs,
      # else the instances will have their own total_limit set
      old_limit = Scraper::JobBase.good_job_concurrency_config[:total_limit]
      Scraper::JobBase.good_job_concurrency_config[:total_limit] = nil

      key_to_job =
        GoodJob::Bulk
          .capture(&block)
          .map { |job| [job.good_job_concurrency_key, job] }
          .to_h

      ReduxApplicationRecord.transaction do
        existing_keys =
          GoodJob::Job.where(concurrency_key: key_to_job.keys).pluck(
            :concurrency_key,
          )
        existing_keys.each { |key| key_to_job.delete(key) }
        GoodJob::Bulk.enqueue(key_to_job.values)
      end
    ensure
      Scraper::JobBase.good_job_concurrency_config[:total_limit] = old_limit
    end
  end
end
