import { toast } from "sonner";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { InferRequestType, InferResponseType } from "hono";

import { client } from "@/lib/rpc";

type ResponseType = InferResponseType<typeof client.api.boards.$post>;
type RequestType = InferRequestType<typeof client.api.boards.$post>;

export const useCreateBoard = () => {
  const queryClient = useQueryClient();

  const mutation = useMutation<ResponseType, Error, RequestType>({
    mutationFn: async ({ json }) => {
      const response = await client.api.boards.$post({ json });
      return await response.json();
    },
    onSuccess: () => {
      toast.success("Board created");
      queryClient.invalidateQueries({ queryKey: ["boards"] });
    },
    onError: () => {
      toast.error("Failed to create board");
    },
  });

  return mutation;
}; 