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


export function useHomes() {
    return useQuery({
        queryKey: ['homes'],
        queryFn: homeService.getAll,
    })
}

export function useHome(id: string) {
    return useQuery({
        queryKey: ['home', id],
        queryFn: () => homeService.getById(id),
        enabled: !!id,
    })
}

export function useCreateHome() {
    const queryClient = useQueryClient()
    return useMutation({
        mutationFn: (data: FormData) => homeService.create(data),
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['homes'] })
        },
    })
}

export function useUpdateHome() {
    const queryClient = useQueryClient()
    return useMutation({
        mutationFn: ({ id, data }: { id: string; data: FormData }) => homeService.update(id, data),
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['homes'] })
        },
    })
}

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