'use client';

import React, { createContext, useContext, useEffect, ReactNode } from 'react';
import { notifications as notificationService } from '../services/notification.service';
import { NotificationConfig } from '../types';

interface NotificationContextValue {
  service: typeof notificationService;
}

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

interface NotificationProviderProps {
  children: ReactNode;
  config?: NotificationConfig;
}

export function NotificationProvider({ children, config }: NotificationProviderProps) {
  useEffect(() => {
    if (config) {
      notificationService.configure(config);
    }
  }, [config]);

  const value: NotificationContextValue = {
    service: notificationService,
  };

  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 a NotificationProvider');
  }
  return context;
}