import { queryKeys } from '@/core/react-query/keys'
import { postsService } from '@/services/posts.service'
import { type ChangeStatePost } from '@/types/posts.types'
import { useMutation, useQueryClient } from '@tanstack/react-query'

type ChangePostStateVariables = {
  id: number
  payload: ChangeStatePost
}

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

  return useMutation({
    mutationFn: ({ id, payload }: ChangePostStateVariables) =>
      postsService.updateState(id, payload),

    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({
        queryKey: queryKeys.posts.detail(variables.id),
      })

      queryClient.invalidateQueries({
        queryKey: queryKeys.posts.lists(),
      })
    },
  })
}
