import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { userService } from '@/services/userService'

export function useUsers(page = 1, limit = 10, search = '', deleted = false) {
    return useQuery({
        queryKey: ['users', page, limit, search, deleted],
        queryFn: () => {
            if (deleted) return userService.getDeleted(page, limit)
            return search ? userService.search(search, page, limit) : userService.getAll(page, limit)
        },
        placeholderData: (previousData) => previousData,
    })
}

export function useDeleteUser() {
    const queryClient = useQueryClient()
    return useMutation({
        mutationFn: userService.delete,
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['users'] })
        },
    })
}

export function useRestoreUser() {
    const queryClient = useQueryClient()
    return useMutation({
        mutationFn: userService.restore,
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['users'] })
        },
    })
}

export function useHardDeleteUser() {
    const queryClient = useQueryClient()
    return useMutation({
        mutationFn: userService.hardDelete,
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['users'] })
        },
    })
}
