import { pikku } from "@/pikku/http"
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { UpdateBackOfficeCandidateInput } from '@heygermany/functions/.pikku/http/pikku-http-wirings-map.gen.d.js'

type BackOfficeCandidatePatch = Omit<UpdateBackOfficeCandidateInput, 'candidateId'> & {
  analysisIssues?: string[]
}

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

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