import { useQuery } from "@tanstack/react-query";
import { api } from "@/lib/api";
import { useAuthStore } from "@/stores/auth";

export type TabloMember = {
  id: string;
  name: string;
  is_admin: boolean;
  email: string;
  avatar_url: string | null;
};

export const useTabloMembers = (tabloId: string | undefined) => {
  const session = useAuthStore((state) => state.session);

  return useQuery({
    queryKey: ["tablo-members", tabloId],
    queryFn: async () => {
      const { data } = await api.get<{ members: TabloMember[] }>(
        `/api/v1/tablos/members/${tabloId}`,
        {
          headers: {
            Authorization: `Bearer ${session?.access_token}`,
          },
        }
      );
      return data.members;
    },
    enabled: !!tabloId && !!session,
  });
};
