import { CAM_BASE } from "../utils/config";
import { useMutation } from "@tanstack/react-query";
import { toast } from "sonner";

async function handleSoftReboot() {
  const response = await fetch(`${CAM_BASE}/api/restart-flexiai`);
  if (!response.ok) throw new Error("Failed to Software Reboot");
  return "Software reboot triggered!";
}

async function handleHardReboot() {
  const response = await fetch(`${CAM_BASE}/api/restart-hardware`);
  if (!response.ok) throw new Error("Failed to Hardware Reboot");
  return "Hardware reboot triggered!";
}

export const useReboots = () => {
  const softRebootMutation = useMutation({
    mutationKey: ["softReboot"],
    mutationFn: handleSoftReboot,

    onSuccess: () => toast.success("Software reboot triggered!"),
    onError: (error) => toast.error(error.message),
  });

  const hardRebootMutation = useMutation({
    mutationKey: ["hardReboot"],
    mutationFn: handleHardReboot,

    onSuccess: () => toast.success("Harware reboot triggered!"),
    onError: (error) => toast.error(error.message),
  });

  return { softRebootMutation, hardRebootMutation };
};
