import { pikku } from "@/pikku/http"
import type { DB } from '@heygermany/sdk'
import { useMutation, useQueryClient } from "@tanstack/react-query"

type LanguageUpdatePayload = Partial<Omit<DB.CandidateLanguage, 'candidateId' | 'languageId' | 'createdAt' | 'lastUpdatedAt'>>

export const useUpdateCandidateLanguage = (candidateId: string) => {
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: async (data: LanguageUpdatePayload) =>
      await pikku().patch("/backoffice/candidate/:candidateId/language", { candidateId, ...data }),
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: ['backoffice', 'candidate', candidateId]
      })
      queryClient.invalidateQueries({
        queryKey: ['candidate', candidateId]
      })
    }
  })
}
