import { mutationOptions } from '@tanstack/react-query';
import { invalidateTrainingRecordData } from '@/lib/query-invalidation';
import {
  createTrainingRecord,
  deleteTrainingRecord,
  importTrainingRecords,
  updateTrainingRecord
} from './service';
import type { TrainingRecordMutationPayload } from './types';

export const createTrainingRecordMutation = mutationOptions({
  mutationFn: (data: TrainingRecordMutationPayload) => createTrainingRecord(data),
  onSuccess: () => invalidateTrainingRecordData()
});

export const updateTrainingRecordMutation = mutationOptions({
  mutationFn: ({ id, values }: { id: number; values: TrainingRecordMutationPayload }) =>
    updateTrainingRecord(id, values),
  onSuccess: () => invalidateTrainingRecordData()
});

export const deleteTrainingRecordMutation = mutationOptions({
  mutationFn: (id: number) => deleteTrainingRecord(id),
  onSuccess: () => invalidateTrainingRecordData()
});

export const importTrainingRecordsMutation = mutationOptions({
  mutationFn: (file: File) => importTrainingRecords(file),
  onSuccess: () => invalidateTrainingRecordData()
});
