'use client';

import { Notification, useNotificationActions, useNotifications, useUnreadCount } from '@/hooks/use-notifications';
import { createContext, ReactNode, useCallback, useContext } from 'react';

interface NotificationContextType {
    notifications: Notification[];
    unreadCount: number;
    isLoading: boolean;
    error: string | null;
    refreshNotifications: () => Promise<void>;
    markAsRead: (id: string) => Promise<Notification | null>;
    markAsUnread: (id: string) => Promise<Notification | null>;
    markAllAsRead: () => Promise<boolean>;
    deleteNotification: (id: string) => Promise<boolean>;
    deleteAllNotifications: () => Promise<boolean>;
    loadMore: () => void;
    hasMore: boolean;
}

const NotificationContext = createContext<NotificationContextType | undefined>(undefined);

export function NotificationProvider({ children }: { children: ReactNode }) {
    const { 
        notifications, 
        isLoading, 
        error, 
        hasMore, 
        loadMore,
        refresh: refreshNotifications 
    } = useNotifications(true, 30000);
    
    const { unreadCount, refresh: refreshUnreadCount } = useUnreadCount(true, 30000);
    
    const {
        markAsRead,
        markAsUnread,
        markAllAsRead,
        deleteNotification,
        deleteAllNotifications
    } = useNotificationActions();

    const handleMarkAsRead = useCallback(async (id: string) => {
        const result = await markAsRead(id);
        if (result) {
            refreshUnreadCount();
        }
        return result;
    }, [markAsRead, refreshUnreadCount]);

    const handleMarkAsUnread = useCallback(async (id: string) => {
        const result = await markAsUnread(id);
        if (result) {
            refreshUnreadCount();
        }
        return result;
    }, [markAsUnread, refreshUnreadCount]);

    const handleMarkAllAsRead = useCallback(async () => {
        const result = await markAllAsRead();
        if (result) {
            refreshUnreadCount();
            refreshNotifications();
        }
        return result;
    }, [markAllAsRead, refreshUnreadCount, refreshNotifications]);

    const handleDeleteNotification = useCallback(async (id: string) => {
        const result = await deleteNotification(id);
        if (result) {
            refreshUnreadCount();
            refreshNotifications();
        }
        return result;
    }, [deleteNotification, refreshUnreadCount, refreshNotifications]);

    const handleDeleteAllNotifications = useCallback(async () => {
        const result = await deleteAllNotifications();
        if (result) {
            refreshUnreadCount();
            refreshNotifications();
        }
        return result;
    }, [deleteAllNotifications, refreshUnreadCount, refreshNotifications]);

    const value: NotificationContextType = {
        notifications,
        unreadCount,
        isLoading,
        error,
        refreshNotifications,
        markAsRead: handleMarkAsRead,
        markAsUnread: handleMarkAsUnread,
        markAllAsRead: handleMarkAllAsRead,
        deleteNotification: handleDeleteNotification,
        deleteAllNotifications: handleDeleteAllNotifications,
        loadMore,
        hasMore
    };

    return (
        <NotificationContext.Provider value={value}>
            {children}
        </NotificationContext.Provider>
    );
}

export function useNotificationContext() {
    const context = useContext(NotificationContext);
    if (context === undefined) {
        throw new Error('useNotificationContext must be used within NotificationProvider');
    }
    return context;
}
