import { mutationOptions } from '@tanstack/react-query';
import { getQueryClient } from '@/lib/query-client';
import { invalidateTrainingRecordData } from '@/lib/query-invalidation';
import { reviewTrainingRecord } from './service';
import { trainingRecordKeys } from './queries';
import type { TrainingRecordReviewPayload } from './types';

export const reviewTrainingRecordMutation = mutationOptions({
  mutationFn: ({ id, values }: { id: number; values: TrainingRecordReviewPayload }) =>
    reviewTrainingRecord(id, values),
  onSuccess: (_data, variables) => {
    const queryClient = getQueryClient();
    return Promise.all([
      invalidateTrainingRecordData(queryClient),
      queryClient.invalidateQueries({ queryKey: trainingRecordKeys.detail(variables.id) })
    ]);
  }
});
