// src/App.tsx
import { useState, useEffect } from 'react';
import AdminPanel from './components/AdminPanel'; 
import Login from './components/Login';
import { CssBaseline, ThemeProvider, createTheme } from '@mui/material';

const darkTheme = createTheme({
  palette: {
    mode: 'dark',
  },
});

function App() {
  const [token, setToken] = useState<string | null>(localStorage.getItem('jwt_token'));

  useEffect(() => {
    if (token) {
      localStorage.setItem('jwt_token', token);
    } else {
      localStorage.removeItem('jwt_token');
    }
  }, [token]);

  const handleLogout = () => {
    setToken(null);
  };

  return (
    <ThemeProvider theme={darkTheme}>
      <CssBaseline />
      {token ? (
        <AdminPanel onLogout={handleLogout} /> 
      ) : (
        <Login onLoginSuccess={setToken} />
      )}
    </ThemeProvider>
  );
}

export default App;