import { ref } from "vue";
import { useToast } from "../../../utils/composables/use-toast";
import { apiUrl } from "../../../api/agent";

export const useDownloadPhotos = () => {
  const isDownloading = ref(false);
  const toast = useToast();

  const downloadAllPhotos = async (
    shootingId: string,
    shootingName?: string
  ) => {
    if (!shootingId) {
      toast.error({ message: "ID du shooting manquant" });
      return;
    }

    try {
      isDownloading.value = true;

      const downloadUrl = `${apiUrl}/shootings/${shootingId}/download-photos`;

      window.location.href = downloadUrl;
    } catch (error) {
      console.error("Erreur lors du téléchargement des photos:", error);
      toast.error({ message: "Erreur lors du téléchargement des photos" });
    } finally {
      setTimeout(() => {
        isDownloading.value = false;
      }, 1000);
    }
  };

  return {
    isDownloading,
    downloadAllPhotos,
  };
};
