import { queryKeys } from '@/core/react-query/keys'
import { evaluationsService } from '@/services/evaluations.service'
import { type CreateEvaluation } from '@/types/evaluations.types'
import { useMutation, useQueryClient } from '@tanstack/react-query'

type CreateEvaluationVariables = {
  id: number
  payload: CreateEvaluation
}

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

  return useMutation({
    mutationFn: ({ id, payload }: CreateEvaluationVariables) =>
      evaluationsService.store(id, payload),

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

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