import getXSRFToken from "../getXSRF.js";

export default async function createEvent(name, description) {
    const csrfToken = await getXSRFToken();

    try {
        const response = await fetch(`http://${import.meta.env.VITE_API_URL}/api/events/create`, {
            method: 'POST',
            credentials: 'include',
            headers: {
                'Content-Type': 'application/json',
                'X-XSRF-TOKEN': csrfToken,
                'Accept': 'application/json'
            },
            body: JSON.stringify({ name, description }),
        });

        const data = await response.json();

        if (!response.ok) {
            if (response.status === 422) {
                return {
                    status: 422,
                    errors: data.errors
                };
            }

            throw new Error(`Erreur HTTP ${response.status}`);
        }

        return { status: response.status, data };

    } catch (error) {
        return { status: "error", error };
    }
}
