import { CorporateUser, Group, User } from "@/interfaces/user";
import axios from "axios";

export const isUserFromCorporate = async (userID: string) => {
  const groups = (await axios.get<Group[]>(`/api/groups?participant=${userID}`))
    .data;
  const users = (await axios.get<User[]>("/api/users/list")).data;

  const adminTypes = groups.map(
    (g) => users.find((u) => u.id === g.admin)?.type,
  );
  return adminTypes.includes("corporate");
};

export const getUserCorporate = async (userID: string) => {
  const groups = (await axios.get<Group[]>(`/api/groups?participant=${userID}`))
    .data;
  const users = (await axios.get<User[]>("/api/users/list")).data;

  const admins = groups
    .map((g) => users.find((u) => u.id === g.admin))
    .filter((x) => x?.type === "corporate");
  return admins.length > 0 ? (admins[0] as CorporateUser) : undefined;
};
