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

export function useServices() {
    return useQuery({
        queryKey: ['services'],
        queryFn: serviceService.getAll,
    })
}

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

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

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

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