import { parseAdminAuth } from "@/lib/api/admin-auth-context";
import { fail, ok } from "@/lib/response";
import { AuthError } from "@/modules/auth/errors";
import { requirePermission } from "@/modules/auth/rbac";
import { RPA_QUEUE_NAME } from "@/lib/constants/rpa";
import { getQuoteRpaQueueCounts } from "@/workers/rpa/queue";

export async function GET(request: Request) {
  let auth;
  try {
    auth = parseAdminAuth(request);
    requirePermission(auth, "rpa:operate");
  } catch (error) {
    if (error instanceof AuthError) {
      return fail(error.code, error.message, error.httpStatus);
    }
    throw error;
  }

  void auth;

  const counts = await getQuoteRpaQueueCounts();

  return ok({
    queue_name: RPA_QUEUE_NAME,
    waiting: counts.waiting,
    active: counts.active,
    delayed: counts.delayed,
    failed: counts.failed,
    completed: counts.completed,
    depth: counts.waiting + counts.active + counts.delayed,
  });
}
