import { useQuery } from '@tanstack/react-query';
import { orderService } from '../api/order.service';

/**
 * Hook to fetch details for a specific order.
 * Uses the general search endpoint filtering by ID as requested.
 */
export function useOrderDetails(orderId: number) {
  return useQuery({
    queryKey: ['orderDetails', orderId],
    enabled: !!orderId,
    queryFn: async () => {
      // The findOrders method returns an array. We search by orderId and take the first result.
      const orders = await orderService.findOrders({ orderId: orderId });
      return orders.length > 0 ? orders[0] : null;
    },
    staleTime: 1000 * 60 * 5, // 5 minutes
  });
}
