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

/** Prefix of every page/filter of one opportunity's volunteer roster. */
export const volunteerRegistrationsKey = (opportunityId: string | undefined) =>
  ["volunteer-registrations", opportunityId] as const;

/**
 * Reloads the roster from page one after a write (add, exclude, role change).
 *
 * A bare `refetch()` refetched only the page the cursor was on. Once the list
 * had auto-loaded page 2, that response was a retry of an already merged page,
 * which `nextPagedState` deliberately ignores, so an excluded volunteer stayed
 * on screen. Returning the cursor to page 1 and invalidating the whole prefix
 * makes the next response a page-1 reload, which replaces the list, and marks
 * every cached page stale so none is replayed.
 */
export async function refreshVolunteerRegistrations(
  queryClient: Pick<QueryClient, "invalidateQueries">,
  opportunityId: string | undefined,
  resetPage: () => void
): Promise<void> {
  resetPage();
  await queryClient.invalidateQueries({
    queryKey: volunteerRegistrationsKey(opportunityId),
  });
}
