"use client";

import { useQuery } from "@tanstack/react-query";

import { getLivePlenaryDashboard } from "../service/live-plenary-service";

// The published numbers only change when CDC-GPSF presses Live, so they can be
// kept for 5 minutes before being fetched again.
const LIVE_PLENARY_STALE_TIME_MS = 5 * 60 * 1000;

export const livePlenaryQueryKeys = {
  all: ["live-dashboard", "plenary"] as const,
};

// The plenary dashboard every role sees. Filtering happens in the browser, so
// this takes no parameters.
export function useLivePlenaryDashboard() {
  const query = useQuery({
    queryKey: livePlenaryQueryKeys.all,
    queryFn: getLivePlenaryDashboard,
    staleTime: LIVE_PLENARY_STALE_TIME_MS,
  });

  return {
    dashboard: query.data ?? null,
    isPublished: Boolean(query.data),
    isLoading: query.isLoading,
    error:
      query.error instanceof Error
        ? query.error.message
        : query.error
          ? "Unable to load the plenary dashboard."
          : null,
  };
}
