/**
 * Orchestrator React Query hooks
 */
import { useMutation, useQueryClient } from '@tanstack/react-query';
import * as orchestratorService from '../services/orchestrator';
import { ApiError } from '../client';

// Mutations
export const useRunDailyWorkflow = (
 options?: Parameters<typeof useMutation>[0]
) => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (tenantId: string) => 
      orchestratorService.runDailyWorkflow(tenantId),
    onSuccess: (_, tenantId) => {
      // Invalidate queries to refresh dashboard data after workflow execution
      queryClient.invalidateQueries({ queryKey: ['procurement', 'plans'] });
      queryClient.invalidateQueries({ queryKey: ['production', 'batches'] });
      queryClient.invalidateQueries({ queryKey: ['forecasts'] });
      // Also invalidate dashboard queries to refresh stats
      queryClient.invalidateQueries({ queryKey: ['dashboard', 'stats'] });
      queryClient.invalidateQueries({ queryKey: ['dashboard'] });
    },
    ...options,
 });
};
