import { useQuery } from '@tanstack/react-query';
import { useInject } from '@/lib/di/hooks/useInject';
import { TEAM_SERVICE_TOKEN } from '@/lib/di/tokens';
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';

export function useTeamMembership(teamId: string, driverId: string) {
  const teamService = useInject(TEAM_SERVICE_TOKEN);

  const queryResult = useQuery({
    queryKey: ['teamMembership', teamId, driverId],
    queryFn: async () => {
      const result = await teamService.getMembership(teamId, driverId);
      if (result.isErr()) {
        throw result.getError();
      }
      return result.unwrap();
    },
    enabled: !!teamId && !!driverId,
  });

  return enhanceQueryResult(queryResult);
}