
import React, { createContext, useContext, useState, useEffect } from 'react';

const BannerContext = createContext();

export const useBanners = () => useContext(BannerContext);

export const BannerProvider = ({ children }) => {
  const [priorityBanner, setPriorityBanner] = useState({
    visible: true,
    message: "Plateforme agréée facture électronique – 2026",
    type: "info", // info, warning
    dismissible: true,
    id: "priority-2026-mandate"
  });

  const [secondaryBanner, setSecondaryBanner] = useState({
    visible: true,
    message: "Bienvenue sur la nouvelle version de Dataven. Découvrez les nouvelles fonctionnalités.",
    dismissible: true,
    id: "secondary-welcome-v3"
  });

  // Load dismissed state from localStorage
  useEffect(() => {
    const dismissedPriority = localStorage.getItem(`banner-dismissed-${priorityBanner.id}`);
    const dismissedSecondary = localStorage.getItem(`banner-dismissed-${secondaryBanner.id}`);

    if (dismissedPriority) {
      setPriorityBanner(prev => ({ ...prev, visible: false }));
    }
    if (dismissedSecondary) {
      setSecondaryBanner(prev => ({ ...prev, visible: false }));
    }
  }, []);

  const dismissPriority = () => {
    if (priorityBanner.dismissible) {
      setPriorityBanner(prev => ({ ...prev, visible: false }));
      localStorage.setItem(`banner-dismissed-${priorityBanner.id}`, 'true');
    }
  };

  const dismissSecondary = () => {
    if (secondaryBanner.dismissible) {
      setSecondaryBanner(prev => ({ ...prev, visible: false }));
      localStorage.setItem(`banner-dismissed-${secondaryBanner.id}`, 'true');
    }
  };

  return (
    <BannerContext.Provider value={{ 
      priorityBanner, 
      secondaryBanner, 
      dismissPriority, 
      dismissSecondary,
      setPriorityBanner,
      setSecondaryBanner
    }}>
      {children}
    </BannerContext.Provider>
  );
};
