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

/**
 * Use to remove a user from the system
 * @param {string} props.usernameToDelete - The username of user to remove
 * @returns {boolean} True if removed, false if not
 * @example
 * const exampleUsername = "user";
 * DeleteUser({ usernameToDelete: exampleUsername });
 */

function DeleteUser(props: { usernameToDelete: string }): boolean {
  let removed = false;
  api
    .removeUser(
      props.usernameToDelete,
      localStorage.getItem("accessToken") ?? "",
    )
    .then((response: APIResponse<User>) => {
      if (response.success) {
        alert("User has been deleted!");
        location.reload();
        removed = true;
      } else {
        alert("User has not been deleted");
        console.error(response.message);
      }
    })
    .catch((error) => {
      alert("User has not been deleted");
      console.error("An error occurred during deletion:", error);
    });
  return removed;
}

export default DeleteUser;
