import { reactive, ref } from "vue";
import { useMutation } from "@tanstack/vue-query";
import { requests } from "../../../api/agent";
import { useToast } from "../../../utils/composables/use-toast";
import { BaseComposableOption } from "../../../types/composable.types";
import { ShareShootingDTO } from "@studio-m/app-types/src/shooting.types";

export const useShareCustomerGallery = (
  options: BaseComposableOption<{ id: string }> = {}
) => {
  const { success, error } = useToast();

  const emails = ref<string>("");

  const payload: Omit<ShareShootingDTO, "shootingId"> = reactive({
    emails: [],
    message: "",
    name: "",
  });

  const mutation = useMutation({
    mutationFn: async (customerId: string) => {
      payload.emails = emails.value.split(",").map((email) => email.trim());
      return await requests.post(
        `customers/${customerId}/share-gallery`,
        payload
      );
    },
    onSuccess: () => {
      success({ message: "La galerie a été partagée avec succès" });
      options.onSuccess?.();
    },
    onError: () => {
      error({
        message: "Une erreur est survenue lors du partage de la galerie",
      });
      options.onError?.();
    },
    retry: false,
  });

  const shareGallery = (customerId: string) => {
    return mutation.mutateAsync(customerId);
  };

  return {
    shareGallery,
    isSharing: mutation.isPending,
    payload,
    emails,
  };
};
