import { useMutation, useQueryClient } from '@tanstack/react-query';
import { adminApi } from '../apis';
import { adminKeys } from '../keys';
import type { HolidayInput } from '../types';

/**
 * Add or edit a holiday (upsert keyed on `holidayDate`). Invalidate the holidays sub-tree so every cached
 * range reflects the change. The client never computes the next-business-day shift — it only maintains the
 * calendar the server uses for payout scheduling (phase §5).
 */
export function useUpsertHoliday() {
  const queryClient = useQueryClient();
  return useMutation<void, unknown, HolidayInput>({
    mutationFn: (input) => adminApi.upsertHoliday(input),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: adminKeys.holidays() });
    },
  });
}
