import { useMutation, useQueryClient } from '@tanstack/react-query';
import { apiFetch } from '../services/apiClient';

async function deleteWord(wordId: string): Promise<void> {
  const response = await apiFetch(`/words/${wordId}`, { method: 'DELETE' });
  if (!response.ok) throw new Error(`HTTP error ${response.status}`);
}

/**
 * Mutation hook to permanently delete a word and all its definitions.
 * Invalidates all word queries on success.
 */
export function useDeleteWord() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: deleteWord,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['words'] });
    },
  });
}
