import { useMutation } from "@tanstack/vue-query";
import { BaseComposableOption } from "../../../types/composable.types";
import { requests } from "../../../api/agent";
import { useInvalidateShootings } from "./use-invalidate-shootings";
import { UpdatePhotosOrderDTO } from "@studio-m/app-types";

export const useUpdatePhotosOrder = (options: BaseComposableOption = {}) => {
  const { invalidateShootings } = useInvalidateShootings();

  const updatePhotosMutation = useMutation({
    mutationFn: async (data: UpdatePhotosOrderDTO) => {
      await requests.put("photos/order", data);
    },
    onSuccess: async () => {
      await invalidateShootings();

      if (options?.onSuccess) {
        await options.onSuccess();
      }
    },
    onError: async (error: Error) => {
      if (options?.onError) {
        await options.onError(error);
      }
    },
  });

  return {
    updatePhotosOrder: updatePhotosMutation.mutateAsync,
    isUpdating: updatePhotosMutation.isPending,
  };
};
