# typed: strict

module JobHelper
  extend T::Sig

  # @param good_job_uuid [String]
  # @return [GoodJob::Job]
  sig { params(good_job_uuid: String).returns(GoodJob::Job) }
  def self.find_job(good_job_uuid)
    GoodJob::Job.find(good_job_uuid)
  end

  # @param good_job_uuid [String]
  # @return [void]
  sig { params(good_job_uuid: String).void }
  def self.perform_job(good_job_uuid)
    job = find_job(good_job_uuid)
    job_args_deserialized =
      T.let(
        ActiveJob::Arguments.deserialize(job.serialized_params),
        T::Array[[String, T.untyped]],
      )
    job_args = job_args_deserialized.find { |key, _| key == "arguments" }
    raise("no arguments for #{job.job_class}") if job_args.nil?
    job_args = T.cast(job_args[1], T::Array[T.untyped])
    job_instance =
      job.job_class&.constantize&.new ||
        raise("no job_class for #{job.job_class}")
    job_instance.arguments = job_args
    job_instance&.perform(*job_args)
  end
end
