import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom';

import { AppShell } from './components/AppShell';
import { RequireAdmin, RequireAuth, RequireDevice } from './components/guards';
import { AuthProvider } from './context/AuthContext';
import { CallProvider } from './context/CallContext';
import { ConversationsProvider } from './context/ConversationsContext';
import { FriendshipsProvider } from './context/FriendshipsContext';
import { AdminPage } from './pages/AdminPage';
import { AuthCallbackPage } from './pages/AuthCallbackPage';
import { AuthPage } from './pages/AuthPage';
import { ChatsEmptyState, ChatsPage } from './pages/ChatsPage';
import { ConversationPage } from './pages/ConversationPage';
import { DevicePage } from './pages/DevicePage';
import { FriendsPage } from './pages/FriendsPage';
import { SettingsPage } from './pages/SettingsPage';

export function App() {
  return (
    <AuthProvider>
      <FriendshipsProvider>
        <ConversationsProvider>
          <CallProvider>
            <BrowserRouter>
            <Routes>
              <Route path="/auth" element={<AuthPage />} />
              <Route path="/auth/callback" element={<AuthCallbackPage />} />
              <Route element={<RequireAuth />}>
                <Route path="/device" element={<DevicePage />} />
                <Route element={<RequireDevice />}>
                  <Route element={<AppShell />}>
                    <Route index element={<Navigate to="/chats" replace />} />
                    <Route path="/chats" element={<ChatsPage />}>
                      <Route index element={<ChatsEmptyState />} />
                      <Route path=":id" element={<ConversationPage />} />
                    </Route>
                    <Route path="/friends" element={<FriendsPage />} />
                    <Route path="/settings" element={<SettingsPage />} />
                    <Route element={<RequireAdmin />}>
                      <Route path="/admin" element={<AdminPage />} />
                    </Route>
                  </Route>
                </Route>
              </Route>
              <Route path="*" element={<Navigate to="/chats" replace />} />
            </Routes>
            </BrowserRouter>
          </CallProvider>
        </ConversationsProvider>
      </FriendshipsProvider>
    </AuthProvider>
  );
}
