for (let i = 0; i < groups.length; i++) {
    const group = groups?.[i];
    if (!group) {
      throw new Error("group is undefined");
    }