// imports

import { useMutation } from "@tanstack/vue-query";
import { API_ENDPOINTS } from "~/constants";

// types

export type UploadAttachmentRequest = {
    file: File;
};

export type UploadAttachmentResponse = {
    id: number;
    file_link: string;
    date: string;
    size: number;
    name: string;
};

// methods

export const handleUploadAttachment = async ({
    file,
}: UploadAttachmentRequest) => {
    // state

    const { $axios: axios } = useNuxtApp();

    const { data } = await axios.post<UploadAttachmentResponse>(
        API_ENDPOINTS.tickets.upload_attachment,
        {
            file,
        },
        {
            headers: {
                "Content-Type": "multipart/form-data",
            },
        }
    );
    return data;
};

// composable

const useUploadAttachment = () => {
    return useMutation({
        mutationFn: (data: UploadAttachmentRequest) =>
            handleUploadAttachment({ file: data.file }),
    });
};

export default useUploadAttachment;
