import { useQuery } from '@tanstack/react-query';
import { useIsAuthenticated } from '@/hooks';
import { ticketsApi } from '../apis';
import { ticketKeys } from '../keys';
import { TICKETS_GC_TIME, TICKETS_LIST_STALE_TIME } from '../constants';

/**
 * The chrome support-entry unread badge (§3.1) — a single number summed across the caller's tickets, or
 * `null` when there's no signal to show (the real path, until REQ-059 lands). Callers must render the
 * badge only when this is a positive number — never a fake "0 unread" placeholder. Gated on
 * authentication, same posture as `useUnreadCount`.
 */
export function useSupportUnreadTotal(): number | null {
  const isAuthenticated = useIsAuthenticated();
  const { data } = useQuery({
    queryKey: ticketKeys.unreadTotal(),
    queryFn: () => ticketsApi.getUnreadTotal(),
    enabled: isAuthenticated,
    staleTime: TICKETS_LIST_STALE_TIME,
    gcTime: TICKETS_GC_TIME,
  });
  return data ?? null;
}
