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

import { client } from "@/lib/rpc";
import { Member } from "../types";

interface UseGetMembersProps {
  workspaceId: string;
}

interface GetMembersResponse {
  total: number;
  documents: any[]; // Using any[] to avoid TypeScript errors
}

export const useGetMembers = ({ workspaceId }: UseGetMembersProps) => {
  const query = useQuery<GetMembersResponse>({
    queryKey: ["members", workspaceId],
    queryFn: async () => {
      const response = await client.api.members.$get({
        query: { workspaceId },
      });

      if (!response.ok) {
        throw new Error("Failed to fetch members");
      }

      const { data } = await response.json();
      return data;
    },
    enabled: !!workspaceId,
  });

  return query;
};
