import {useState} from 'react';
import {EventContext} from "./EventContext";
import getAllEvents from "../../utils/events/getAllEvents.js";
import createEvent from "../../utils/events/createEvent.js";
import deleteEvent from "../../utils/events/deleteEvent.js";


export function EventProvider({ children }) {

    const [events, setEvents] = useState([]);

    const updateEvent = async () => {
        const result = await getAllEvents();
        setEvents(result.data.data);
    }

    const addEvent = async (name, description, start, end) => {
        await createEvent(name, description, start, end);
        await updateEvent();
    }

    const removeEvent = async (eventId) => {
        const result = await deleteEvent(eventId);
        await updateEvent();
        return result;
    }

    return (
        <EventContext.Provider value={{ events, updateEvent, addEvent, removeEvent }}>
            {children}
        </EventContext.Provider>
    );
}