import { useQuery } from '@tanstack/react-query';
import { apiFetch } from '../lib/api';

type LastSeenMap = Record<string, string | null>;

export function useLastSeenBatch(userIds: string[]): LastSeenMap {
  const sorted = [...userIds].sort();
  const joinedKey = sorted.join(',');

  const { data } = useQuery<LastSeenMap>({
    queryKey: ['last-seen', joinedKey],
    queryFn: () =>
      apiFetch<LastSeenMap>(`/api/presence/last-seen?userIds=${encodeURIComponent(joinedKey)}`),
    enabled: sorted.length > 0,
    staleTime: 30_000,
  });

  return data ?? {};
}
