import { useMutation } from "@tanstack/vue-query";
import { ref } from "vue";
import { requests } from "../../../api/agent";
import { useInvalidateShootings } from "./use-invalidate-shootings";
import { useToast } from "../../../utils/composables/use-toast";

export const useDuplicateShooting = () => {
  const { invalidateShootings } = useInvalidateShootings();
  const toast = useToast();
  const isLoading = ref(false);

  const mutation = useMutation({
    mutationFn: async (params: { shootingId: string; customerId?: string }) => {
      const response = await requests.post(
        `shootings/${params.shootingId}/duplicate`,
        { customerId: params.customerId }
      );
      return response;
    },
    onSuccess: async () => {
      await invalidateShootings();
      toast.success({ message: "Galerie dupliquée avec succès" });
    },
  });

  const duplicateShooting = async (shootingId: string, customerId?: string) => {
    isLoading.value = true;
    try {
      await mutation.mutateAsync({ shootingId, customerId });
    } finally {
      isLoading.value = false;
    }
  };

  return {
    duplicateShooting,
    isLoading: isLoading.value
  };
};
