import { useState } from 'react'
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import { UserProvider } from './context/UserContext'
import Dashboard from './pages/Dashboard'
import ServerDetail from './pages/ServerDetail'

export default function App() {
  const [token, setToken] = useState(localStorage.getItem('gsm_token'))

  const handleLogin = (newToken) => {
    localStorage.setItem('gsm_token', newToken)
    setToken(newToken)
  }

  const handleLogout = () => {
    localStorage.removeItem('gsm_token')
    setToken(null)
  }

  return (
    <UserProvider token={token}>
      <BrowserRouter>
        <Routes>
          <Route path="/" element={<Dashboard onLogin={handleLogin} onLogout={handleLogout} />} />
          <Route path="/server/:serverId" element={<ServerDetail onLogin={handleLogin} onLogout={handleLogout} />} />
          <Route path="*" element={<Navigate to="/" replace />} />
        </Routes>
      </BrowserRouter>
    </UserProvider>
  )
}
