export const getGroups = async ({ token }: { token: string }): Promise<Group[]> => {
  const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/groups`, {
    headers: {
      Authorization: `Bearer ${token}`
    }
  });

  const groups = (await res.json()) as Group[];

  if (!res.ok) {
    throw new Error('Failed to fetch groups');
  }

  if (!groups) {
    return [];
  }

  return groups;
};

export type Group = {
  id: number;
  name: string;
  chapter?: number;
  puzzle?: number;
};
