import { BrowserRouter, Navigate, Route, Routes } from "react-router-dom";
import { AuthProvider } from "./auth/AuthContext";
import { ThemeProvider } from "./theme/ThemeContext";
import { ProtectedRoute, AccountRoute } from "./components/ProtectedRoute";
import { LoginPage } from "./pages/LoginPage";
import { RegisterPage } from "./pages/RegisterPage";
import { AnonymousEntryPage } from "./pages/AnonymousEntryPage";
import { HomePage } from "./pages/HomePage";
import { TestListPage } from "./pages/TestListPage";
import { PollPage } from "./pages/PollPage";
import { MyTestsPage } from "./pages/MyTestsPage";
import { ResponsePage } from "./pages/ResponsePage";
import { ProfilePage } from "./pages/ProfilePage";

// Доступно и по токену, и анонимно (школа+класс без аккаунта).
function protect(el: React.ReactNode) {
  return <ProtectedRoute>{el}</ProtectedRoute>;
}

// Требует настоящий аккаунт — история и профиль анонимусам недоступны.
function account(el: React.ReactNode) {
  return <AccountRoute>{el}</AccountRoute>;
}

export default function App() {
  return (
    <ThemeProvider>
      <AuthProvider>
        <BrowserRouter>
          <Routes>
            <Route path="/login" element={<LoginPage />} />
            <Route path="/register" element={<RegisterPage />} />
            <Route path="/anonymous" element={<AnonymousEntryPage />} />
            <Route path="/" element={protect(<HomePage />)} />
            <Route path="/tests" element={protect(<TestListPage />)} />
            <Route path="/poll/:pollId" element={protect(<PollPage />)} />
            <Route path="/my-tests" element={account(<MyTestsPage />)} />
            <Route path="/response/:responseId" element={account(<ResponsePage />)} />
            <Route path="/profile" element={account(<ProfilePage />)} />
            <Route path="*" element={<Navigate to="/" replace />} />
          </Routes>
        </BrowserRouter>
      </AuthProvider>
    </ThemeProvider>
  );
}
