import { ref } from 'vue';

const notifications = ref([]);
let nextId = 1;

export function useNotifications() {
    const addNotification = (message, type = 'info', duration = 4000) => {
        const notification = {
            id: nextId++,
            message,
            type, // 'success', 'error', 'warning', 'info'
            duration,
            timestamp: Date.now()
        };

        notifications.value.push(notification);

        // Auto-remove après la durée spécifiée
        if (duration > 0) {
            setTimeout(() => {
                removeNotification(notification.id);
            }, duration);
        }

        return notification.id;
    };

    const removeNotification = (id) => {
        const index = notifications.value.findIndex(n => n.id === id);
        if (index > -1) {
            notifications.value.splice(index, 1);
        }
    };

    const clearAll = () => {
        notifications.value = [];
    };

    const showSuccess = (message, duration = 4000) => {
        return addNotification(message, 'success', duration);
    };

    const showError = (message, duration = 6000) => {
        return addNotification(message, 'error', duration);
    };

    const showWarning = (message, duration = 5000) => {
        return addNotification(message, 'warning', duration);
    };

    const showInfo = (message, duration = 4000) => {
        return addNotification(message, 'info', duration);
    };

    return {
        notifications,
        addNotification,
        removeNotification,
        clearAll,
        showSuccess,
        showError,
        showWarning,
        showInfo
    };
}
