import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { wagonTypesService } from '@/services/wagon-types.service';

export const WAGON_TYPES_QUERY_KEY = ['wagon-types'];

export function useWagonTypes() {
  return useQuery({
    queryKey: WAGON_TYPES_QUERY_KEY,
    queryFn: () => wagonTypesService.getWagonTypes(),
  });
}

export function useCreateWagonType() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: wagonTypesService.create,
    onSuccess: () => qc.invalidateQueries({ queryKey: WAGON_TYPES_QUERY_KEY }),
  });
}

export function useUpdateWagonType() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ id, data }: { id: string; data: Record<string, unknown> }) =>
      wagonTypesService.update(id, data),
    onSuccess: () => qc.invalidateQueries({ queryKey: WAGON_TYPES_QUERY_KEY }),
  });
}

export function useDeleteWagonType() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: wagonTypesService.delete,
    onSuccess: () => qc.invalidateQueries({ queryKey: WAGON_TYPES_QUERY_KEY }),
  });
}
