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

export function useAbouts(page = 1, limit = 10) {
    return useQuery({
        queryKey: ['abouts', page, limit],
        queryFn: () => aboutService.getAll(page, limit),
    })
}

export function useAbout(id: string | undefined) {
    return useQuery({
        queryKey: ['about', id],
        queryFn: () => aboutService.getById(id!),
        enabled: !!id,
    })
}

export function useCreateAbout() {
    const queryClient = useQueryClient()
    return useMutation({
        mutationFn: aboutService.create,
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['abouts'] })
        },
    })
}

export function useUpdateAbout() {
    const queryClient = useQueryClient()
    return useMutation({
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        mutationFn: ({ id, data }: { id: string; data: any }) => aboutService.update(id, data),
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['abouts'] })
            queryClient.invalidateQueries({ queryKey: ['about'] })
        },
    })
}

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