// imports

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

// types

export type CreateMessageRequest = {
    content: string;
    attachments: ServerFile[];
    ticket_id: number | string;
};

const useCreateMessage = () => {
    // state

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

    // methods

    const handleCreateMessage = async (params: CreateMessageRequest) => {
        const { data } = await axios.post(
            API_ENDPOINTS.tickets.create_message,
            {
                content: params.content,
                attachments: params.attachments.map((i) => i.id),
                ticket: params.ticket_id,
            }
        );
        return data;
    };

    return useMutation({
        mutationFn: (messageData: CreateMessageRequest) =>
            handleCreateMessage(messageData),
    });
};

export default useCreateMessage;
