import { createContext, useContext, useState } from 'react' import type { ModuleStatus } from '../data/modulesData' const DEFAULT_STATUSES: Record = { 'housekeeping': 'active', 'channel-manager': 'active', 'wifi-auth': 'active', 'payments': 'active', 'tv-welcome': 'active', 'smart-locks': 'active', 'olap-reports': 'active', 'website-builder': 'active', 'booking-widget': 'active', 'pos': 'active', 'reviews': 'active', 'room-service': 'active', 'rental': 'active', 'migration': 'active', } interface ModulesContextValue { statuses: Record setStatus: (id: string, status: ModuleStatus) => void isActive: (id: string) => boolean } const ModulesContext = createContext(null) const MODULES_VERSION = '2' export function ModulesProvider({ children }: { children: React.ReactNode }) { const [statuses, setStatuses] = useState>(() => { try { // Reset stored statuses when version changes so new defaults apply const storedVersion = localStorage.getItem('hotelsync-modules-ver') if (storedVersion !== MODULES_VERSION) { localStorage.removeItem('hotelsync-modules') localStorage.setItem('hotelsync-modules-ver', MODULES_VERSION) return DEFAULT_STATUSES } const stored = localStorage.getItem('hotelsync-modules') return stored ? { ...DEFAULT_STATUSES, ...JSON.parse(stored) } : DEFAULT_STATUSES } catch { return DEFAULT_STATUSES } }) const setStatus = (id: string, status: ModuleStatus) => { setStatuses(prev => { const next = { ...prev, [id]: status } localStorage.setItem('hotelsync-modules', JSON.stringify(next)) return next }) } const isActive = (id: string) => statuses[id] === 'active' || statuses[id] === 'trial' return ( {children} ) } export function useModules() { const ctx = useContext(ModulesContext) if (!ctx) throw new Error('useModules must be used within ModulesProvider') return ctx }