// App Component
// Main application component - handles authentication state and routing

function App() {
    const [user, setUser] = useState(null);
    const [token, setToken] = useState(localStorage.getItem('token'));

    useEffect(() => {
        if (token) {
            const userData = JSON.parse(localStorage.getItem('user') || '{}');
            setUser(userData);
        }
    }, []);

    const handleLogin = (userData, userToken) => {
        setUser(userData);
        setToken(userToken);
        localStorage.setItem('token', userToken);
        localStorage.setItem('user', JSON.stringify(userData));
    };

    const handleLogout = () => {
        setUser(null);
        setToken(null);
        localStorage.removeItem('token');
        localStorage.removeItem('user');
    };

    return (
        <I18nProvider>
            {user ? <Dashboard user={user} token={token} onLogout={handleLogout} /> : <Auth onLogin={handleLogin} />}
        </I18nProvider>
    );
}

// Export to global scope
window.App = App;
