import { mutationOptions } from '@tanstack/react-query';
import { getQueryClient } from '@/lib/query-client';
import { invalidateTrainingMatrixData } from '@/lib/query-invalidation';
import { upsertEmployeeDirectoryTarget } from './service';
import { employeeDirectoryKeys } from './queries';
import type { EmployeeTargetMutationPayload } from './types';

export const upsertEmployeeDirectoryTargetMutation = mutationOptions({
  mutationFn: ({
    id,
    values
  }: {
    id: number;
    values: EmployeeTargetMutationPayload;
  }) => upsertEmployeeDirectoryTarget(id, values),
  onSuccess: (_data, variables) => {
    const queryClient = getQueryClient();
    return Promise.all([
      invalidateTrainingMatrixData(queryClient),
      queryClient.invalidateQueries({
        queryKey: employeeDirectoryKeys.target(variables.id, variables.values.year)
      })
    ]);
  }
});
