import { mutationOptions } from '@tanstack/react-query';
import { invalidateTrainingPolicyData } from '@/lib/query-invalidation';
import {
  activateTrainingPolicy,
  cloneTrainingPolicy,
  createTrainingPolicy,
  deactivateTrainingPolicy,
  updateTrainingPolicy
} from './service';
import type { TrainingPolicyMutationPayload } from './types';

function invalidateTrainingPolicyQueries() {
  return invalidateTrainingPolicyData();
}

export const createTrainingPolicyMutation = mutationOptions({
  mutationFn: (data: TrainingPolicyMutationPayload) => createTrainingPolicy(data),
  onSuccess: invalidateTrainingPolicyQueries
});

export const updateTrainingPolicyMutation = mutationOptions({
  mutationFn: ({ id, values }: { id: number; values: TrainingPolicyMutationPayload }) =>
    updateTrainingPolicy(id, values),
  onSuccess: invalidateTrainingPolicyQueries
});

export const cloneTrainingPolicyMutation = mutationOptions({
  mutationFn: ({ id }: { id: number }) => cloneTrainingPolicy(id),
  onSuccess: invalidateTrainingPolicyQueries
});

export const activateTrainingPolicyMutation = mutationOptions({
  mutationFn: ({ id }: { id: number }) => activateTrainingPolicy(id),
  onSuccess: invalidateTrainingPolicyQueries
});

export const deactivateTrainingPolicyMutation = mutationOptions({
  mutationFn: ({ id }: { id: number }) => deactivateTrainingPolicy(id),
  onSuccess: invalidateTrainingPolicyQueries
});
