/*
 * Copyright (c) 2021-2026 ahriman team.
 *
 * This file is part of ahriman
 * (see https://github.com/arcan1s/ahriman).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
import { type AlertColor, Box } from "@mui/material";
import type { Notification } from "contexts/Notification";
import { NotificationContext } from "contexts/NotificationContext";
import NotificationItem from "contexts/NotificationItem";
import React, { type ReactNode, useCallback, useMemo, useRef, useState } from "react";

export function NotificationProvider({ children }: { children: ReactNode }): React.JSX.Element {
    const nextId = useRef(0);
    const [notifications, setNotifications] = useState<Notification[]>([]);

    const addNotification = useCallback((title: string, message: string, severity: AlertColor) => {
        const id = nextId.current++;
        setNotifications(prev => [...prev, { id, title, message, severity }]);
    }, []);

    const removeNotification = useCallback((id: number) => {
        setNotifications(prev => prev.filter(notification => notification.id !== id));
    }, []);

    const showSuccess = useCallback(
        (title: string, message: string) => addNotification(title, message, "success"),
        [addNotification],
    );
    const showError = useCallback(
        (title: string, message: string) => addNotification(title, message, "error"),
        [addNotification],
    );

    const value = useMemo(() => ({ showSuccess, showError }), [showSuccess, showError]);

    return (
        <NotificationContext.Provider value={value}>
            {children}
            <Box
                sx={{
                    position: "fixed",
                    top: 16,
                    left: "50%",
                    transform: "translateX(-50%)",
                    zIndex: theme => theme.zIndex.snackbar,
                    display: "flex",
                    flexDirection: "column",
                    gap: 1,
                    maxWidth: 500,
                    width: "100%",
                    pointerEvents: "none",
                }}
            >
                {notifications.map(notification =>
                    <NotificationItem key={notification.id} notification={notification} onClose={removeNotification} />,
                )}
            </Box>
        </NotificationContext.Provider>
    );
}
