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[":boardId"]["$patch"]>;
type RequestType = InferRequestType<typeof client.api.boards[":boardId"]["$patch"]>;

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

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

  return mutation;
}; 