import { useMutation } from "@tanstack/vue-query";
import { requests } from "../../../api/agent";
import { useInvalidateShootings } from "./use-invalidate-shootings";
import { useToast } from "../../../utils/composables/use-toast";
import { BaseComposableOption } from "../../../types/composable.types";
import { PhotoDTO } from "@studio-m/app-types/src/shooting.types";

export const useDeletePhotos = (options: BaseComposableOption = {}) => {
  const { invalidateShootings } = useInvalidateShootings();
  const toast = useToast();

  const mutation = useMutation({
    mutationFn: async (photos: PhotoDTO[]) => {
      const photoIds = photos.map((photo) => ({ id: photo.id }));
      return await requests.delete("photos", {
        json: { photoIds },
      });
    },
    onSuccess: async (data) => {
      await invalidateShootings();
      toast.success({ message: "Photos supprimées avec succès" });
      options.onSuccess?.(data);
    },
    onError: (error) => {
      toast.error({ message: "Erreur lors de la suppression des photos" });
      options.onError?.(error);
    },
  });

  const deletePhotos = async (photos: PhotoDTO[]) => {
    if (photos.length === 0) return false;

    try {
      await mutation.mutateAsync(photos);
      return true;
    } catch {
      return false;
    }
  };

  return {
    deletePhotos,
  };
};
