import React from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import { useAdmin } from './contexts';

// 用户端页面
import HomePage from './pages/HomePage';
import QuizPage from './pages/QuizPage';
import ResultPage from './pages/ResultPage';
import { SubjectSelectionPage } from './pages/SubjectSelectionPage';
import { UserTaskPage } from './pages/UserTaskPage';
import BankingLandingPage from './pages/BankingLandingPage';

// 管理端页面
import AdminLoginPage from './pages/admin/AdminLoginPage';
import AdminDashboardPage from './pages/admin/AdminDashboardPage';
import QuestionManagePage from './pages/admin/QuestionManagePage';
import QuestionTextImportPage from './pages/admin/QuestionTextImportPage';
import QuizConfigPage from './pages/admin/QuizConfigPage';
import StatisticsPage from './pages/admin/StatisticsPage';
import BackupRestorePage from './pages/admin/BackupRestorePage';
import QuestionCategoryPage from './pages/admin/QuestionCategoryPage';
import ExamSubjectPage from './pages/admin/ExamSubjectPage';
import ExamTaskPage from './pages/admin/ExamTaskPage';
import UserManagePage from './pages/admin/UserManagePage';

// 布局组件
import AdminLayout from './layouts/AdminLayout';

// 管理员路由守卫
const AdminRoute = ({ children }: { children: React.ReactNode }) => {
  const { isAuthenticated } = useAdmin();
  return isAuthenticated ? <>{children}</> : <Navigate to="/admin/login" />;
};

function App() {
  return (
    <div className="min-h-screen bg-gray-50">
      <Routes>
        {/* 用户端路由 */}
        <Route path="/" element={<HomePage />} />
        <Route path="/subjects" element={<SubjectSelectionPage />} />
        <Route path="/tasks" element={<UserTaskPage />} />
        <Route path="/quiz" element={<QuizPage />} />
        <Route path="/result/:id" element={<ResultPage />} />
        <Route path="/banking-demo" element={<BankingLandingPage />} />
        
        {/* 管理端路由 */}
        <Route path="/admin/login" element={<AdminLoginPage />} />
        <Route
          path="/admin/*"
          element={
            <AdminRoute>
              <AdminLayout>
                <Routes>
                  <Route path="dashboard" element={<AdminDashboardPage />} />
                  <Route path="questions" element={<QuestionManagePage />} />
                  <Route path="question-bank" element={<QuestionManagePage />} />
                  <Route path="questions/text-import" element={<QuestionTextImportPage />} />
                  <Route path="categories" element={<QuestionCategoryPage />} />
                  <Route path="subjects" element={<ExamSubjectPage />} />
                  <Route path="tasks" element={<ExamTaskPage />} />
                  <Route path="exam-tasks" element={<ExamTaskPage />} />
                  <Route path="users" element={<UserManagePage />} />
                  <Route path="config" element={<QuizConfigPage />} />
                  <Route path="statistics" element={<StatisticsPage />} />
                  <Route path="backup" element={<BackupRestorePage />} />
                  <Route path="*" element={<Navigate to="/admin/dashboard" />} />
                </Routes>
              </AdminLayout>
            </AdminRoute>
          }
        />
        
        {/* 默认重定向 */}
        <Route path="*" element={<Navigate to="/" />} />
      </Routes>
    </div>
  );
}

export default App;
