# typed: strict
module Domain::Fa::HasCountFailedInQueue
  extend T::Sig
  extend ActiveSupport::Concern

  included do
    sig { params(queue_name: T.any(String, T::Array[String])).returns(Integer) }
    def count_failed_in_queue(queue_name)
      GoodJob::Job
        .where(
          queue_name: queue_name,
          finished_at: nil,
          performed_at: nil,
          error: nil,
        )
        .where(
          [
            "(serialized_params->'exception_executions' = '{}')",
            "(serialized_params->'exception_executions' is null)",
          ].join(" OR "),
        )
        .count
    end
  end
end
