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

export function useTeamJoinRequests(teamId: string, currentUserId: string, isOwner: boolean) {
  const teamJoinService = useInject(TEAM_JOIN_SERVICE_TOKEN);

  const queryResult = useQuery({
    queryKey: ['teamJoinRequests', teamId, currentUserId, isOwner],
    queryFn: async () => {
      const result = await teamJoinService.getJoinRequests(teamId, currentUserId, isOwner);
      if (result.isErr()) {
        const error = result.getError();
        throw new ApiError(error.message, 'SERVER_ERROR', { timestamp: new Date().toISOString() });
      }
      return result.unwrap();
    },
    enabled: !!teamId && !!currentUserId,
  });

  return enhanceQueryResult(queryResult);
}
