import { useCallback } from "react";
import { toast } from "sonner";

interface SpawnMutationOptions {
  onSuccess?: () => void;
  showToast?: boolean;
  successMessage?: string;
  errorMessage?: string;
}

export function useSpawnMutation<T>(
  mutationFn: any,
  options: SpawnMutationOptions = {}
) {
  const {
    onSuccess,
    showToast = true,
    successMessage = "Architect spawned",
    errorMessage = "Failed to spawn architect",
  } = options;

  const mutation = mutationFn({
    onSuccess: () => {
      if (showToast) {
        toast.success(successMessage);
      }
      onSuccess?.();
    },
    onError: () => {
      if (showToast) {
        toast.error(errorMessage);
      }
    },
  });

  const spawn = useCallback(
    (params: T) => {
      mutation.mutate(params);
    },
    [mutation]
  );

  return {
    spawn,
    isSpawning: mutation.isPending,
    error: mutation.error?.message,
    isError: mutation.isError,
  };
}