import React, { createContext, useState, useContext } from 'react';
import FlashNotification from '@/components/FlashNotification';

const NotificationContext = createContext();

export const NotificationProvider = ({ children }) => {
  const [notification, setNotification] = useState({
    message: '',
    type: '',
    title: '',
    errorCode: '',
  });

  const showNotification = (
    message,
    type = 'info',
    title = '',
    errorCode = ''
  ) => {
    setNotification({ message, type, title, errorCode });
  };

  const clearNotification = () => {
    setNotification({ message: '', type: '', title: '', errorCode: '' });
  };

  return (
    <NotificationContext.Provider value={{ showNotification }}>
      {notification.message && (
        <FlashNotification
          title={notification.title}
          message={notification.message}
          type={notification.type}
          errorCode={notification.errorCode}
          onClose={clearNotification}
        />
      )}
      {children}
    </NotificationContext.Provider>
  );
};

export const useNotification = () => useContext(NotificationContext);
