// imports

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

// types

export type CreateTicketRequest = {
    ticket_category: string | undefined;
    order_id: number | undefined;
    subject: string;
    content: string;
    attachments: ServerFile[];
};

const useCreateTicket = () => {
    // state

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

    // methods

    const handleCreateTicket = async (params: CreateTicketRequest) => {
        const { data } = await axios.post(API_ENDPOINTS.tickets.create, {
            message: {
                content: params.content,
                attachments: params.attachments.map((i) => i.id),
            },
            subject: params.subject,
            ticket_category: params.ticket_category,
            order_id: params.order_id,
        });
        return data;
    };

    return useMutation({
        mutationFn: (ticketData: CreateTicketRequest) =>
            handleCreateTicket(ticketData),
    });
};

export default useCreateTicket;
