import { createContext, useContext, useState } from 'react' import type { User, AuthSession } from '../types' import { MOCK_USERS } from '../data/mockData' interface AuthContextValue { session: AuthSession | null user: User | null login: (email: string, password: string) => Promise logout: () => void } const AuthContext = createContext(null) export function AuthProvider({ children }: { children: React.ReactNode }) { const [session, setSession] = useState(() => { const stored = sessionStorage.getItem('hotelsync-session') return stored ? JSON.parse(stored) : null }) const login = async (email: string, _password: string): Promise => { // Mock authentication — in production, call POST /auth/login await new Promise(r => setTimeout(r, 800)) const user = MOCK_USERS.find(u => u.email.toLowerCase() === email.toLowerCase()) if (!user) return null const s: AuthSession = { user, token: 'mock-jwt-token-' + user.id } setSession(s) sessionStorage.setItem('hotelsync-session', JSON.stringify(s)) return user } const logout = () => { setSession(null) sessionStorage.removeItem('hotelsync-session') } return ( {children} ) } export function useAuth() { const ctx = useContext(AuthContext) if (!ctx) throw new Error('useAuth must be used within AuthProvider') return ctx }