import { APIResponse, api } from "../API/API";

export interface ProjectRoleChange {
  username: string;
  role: "project_manager" | "member" | "";
  projectname: string;
}

export default function ChangeRole(roleChangeInfo: ProjectRoleChange): void {
  if (
    roleChangeInfo.username === "" ||
    roleChangeInfo.role === "" ||
    roleChangeInfo.projectname === ""
  ) {
    // FOR DEBUG
    // console.log(roleChangeInfo.role + ": Role");
    // console.log(roleChangeInfo.projectname + ": P-Name");
    // console.log(roleChangeInfo.username + ": U-name");
    alert("You have to select a role");
    return;
  }
  api
    .changeUserRole(roleChangeInfo, localStorage.getItem("accessToken") ?? "")
    .then((response: APIResponse<void>) => {
      if (response.success) {
        alert("Role changed successfully");
        location.reload();
      } else {
        alert(response.message);
        console.error(response.message);
      }
    })
    .catch((error) => {
      alert(error);
      console.error("An error occurred during change:", error);
    });
}
