import { useMutation, UseMutationOptions } from '@tanstack/react-query';
import { useInject } from '@/lib/di/hooks/useInject';
import { RACE_SERVICE_TOKEN } from '@/lib/di/tokens';
import { ApiError } from '@/lib/api/base/ApiError';
import type { FileProtestCommandDTO } from '@/lib/types/generated/FileProtestCommandDTO';

export function useFileProtest(
  options?: Omit<UseMutationOptions<void, ApiError, FileProtestCommandDTO>, 'mutationFn'>
) {
  const raceService = useInject(RACE_SERVICE_TOKEN);

  return useMutation<void, ApiError, FileProtestCommandDTO>({
    mutationFn: async (command) => {
      const result = await raceService.fileProtest(command);
      if (result.isErr()) {
        const error = result.getError();
        throw new ApiError(error.message, 'SERVER_ERROR', { timestamp: new Date().toISOString() });
      }
    },
    ...options,
  });
}
