import { useMutation, useQueryClient } from "@tanstack/react-query";
import { CreateUser, UpdateUser } from "../schemas/users";
import { updateUserAction, createUserAction, deleteUserAction, updateProfileAction } from "../actions/actions";

// Create mutation
export function useCreateUser() {
  const queryClient = useQueryClient();
  const mutation = useMutation({
    mutationFn: (data: CreateUser) => createUserAction(data),
    onSuccess: () => queryClient.invalidateQueries({ queryKey: ['users'] }),
    // onError: (e) => console.error('Error:', e),
  })
  return mutation
}

// Update mutation
export function useUpdateUser() {
  const queryClient = useQueryClient();
  const mutation = useMutation({
    mutationFn: (data: UpdateUser) => updateUserAction(data),
    onSuccess: () => queryClient.invalidateQueries({ queryKey: ['users'] }),
    onError: (e) => console.error('Error:', e)
  })
  return mutation;
}

export function useUpdateProfile() {
  const queryClient = useQueryClient();
  const mutation = useMutation({
    mutationFn: (data: UpdateUser) => updateProfileAction(data),
    onSuccess: () => queryClient.invalidateQueries({ queryKey: ['users'] }),
    // onError: (e) => console.error('Error:', e)
  })
  return mutation;
}

// Delete mutation
export function useDeleteUser() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => deleteUserAction(id),
    onSuccess: () => queryClient.invalidateQueries({ queryKey: ['users'] }),
    onError: (e) => console.error('Error:', e)
  })
}