import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"
import { serviceTitleService } from "@/services/serviceTitleService"


export function useServiceTitles() {
    return useQuery({
        queryKey: ["service-titles"],
        queryFn: serviceTitleService.getAll,
    })
}

export function useServiceTitle(id: string) {
    return useQuery({
        queryKey: ["service-titles", id],
        queryFn: () => serviceTitleService.getById(id),
        enabled: !!id,
    })
}

export function useCreateServiceTitle() {
    const queryClient = useQueryClient()

    return useMutation({
        mutationFn: serviceTitleService.create,
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ["service-titles"] })
        },
    })
}

export function useUpdateServiceTitle() {
    const queryClient = useQueryClient()

    return useMutation({
        mutationFn: ({ id, data }: { id: string; data: Parameters<typeof serviceTitleService.update>[1] }) =>
            serviceTitleService.update(id, data),
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ["service-titles"] })
        },
    })
}

export function useDeleteServiceTitle() {
    const queryClient = useQueryClient()

    return useMutation({
        mutationFn: serviceTitleService.delete,
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ["service-titles"] })
        },
    })
}
