import { BrowserRouter, Routes, Route, Navigate, useParams } from 'react-router-dom'
import { ConfigProvider } from 'antd'
import zhCN from 'antd/locale/zh_CN'
import Login from '@/pages/Login/Login'
import ProjectList from '@/pages/ProjectList/ProjectList'
import DocumentPage from '@/pages/Document/DocumentPage'
import DocumentEditor from '@/pages/Document/DocumentEditor'
import Dashboard from '@/pages/Dashboard'
import Desktop from '@/pages/Desktop'
import Constructing from '@/pages/Constructing'
import PreviewPage from '@/pages/Preview/PreviewPage'
import ProfilePage from '@/pages/Profile/ProfilePage'
import Permissions from '@/pages/System/Permissions'
import Users from '@/pages/System/Users'
import Roles from '@/pages/System/Roles'
import ProtectedRoute from '@/components/ProtectedRoute'
import '@/App.css'

// 重定向到文档页面的组件
function RedirectToDocs() {
  const { projectId } = useParams()
  return <Navigate to={`/projects/${projectId}/docs`} replace />
}

function App() {
  return (
    <ConfigProvider locale={zhCN}>
      <BrowserRouter>
        <Routes>
          <Route path="/login" element={<Login />} />
          {/* 项目预览（公开访问，无需登录） */}
          <Route path="/preview/:projectId" element={<PreviewPage />} />
          <Route
            path="/dashboard"
            element={
              <ProtectedRoute>
                <Dashboard />
              </ProtectedRoute>
            }
          />
          <Route
            path="/desktop"
            element={
              <ProtectedRoute>
                <Desktop />
              </ProtectedRoute>
            }
          />
          <Route
            path="/projects"
            element={
              <ProtectedRoute>
                <ProjectList />
              </ProtectedRoute>
            }
          />
          {/* 文档阅读模式 */}
          <Route
            path="/projects/:projectId/docs"
            element={
              <ProtectedRoute>
                <DocumentPage />
              </ProtectedRoute>
            }
          />
          {/* 文档编辑模式 */}
          <Route
            path="/projects/:projectId/editor"
            element={
              <ProtectedRoute>
                <DocumentEditor />
              </ProtectedRoute>
            }
          />
          {/* 捕获所有 /projects/:projectId/* 路径（包括中文路径），重定向到文档页面 */}
          <Route path="/projects/:projectId/*" element={<RedirectToDocs />} />
          {/* 功能开发中页面 */}
          <Route
            path="/constructing"
            element={
              <ProtectedRoute>
                <Constructing />
              </ProtectedRoute>
            }
          />
          {/* 个人中心 */}
          <Route
            path="/profile"
            element={
              <ProtectedRoute>
                <ProfilePage />
              </ProtectedRoute>
            }
          />
          {/* 角色权限管理 */}
          <Route
            path="/system/permissions"
            element={
              <ProtectedRoute>
                <Permissions />
              </ProtectedRoute>
            }
          />
          {/* 用户管理 */}
          <Route
            path="/system/users"
            element={
              <ProtectedRoute>
                <Users />
              </ProtectedRoute>
            }
          />
          {/* 角色管理 */}
          <Route
            path="/system/roles"
            element={
              <ProtectedRoute>
                <Roles />
              </ProtectedRoute>
            }
          />
          <Route path="/" element={<Navigate to="/projects" replace />} />
        </Routes>
      </BrowserRouter>
    </ConfigProvider>
  )
}

export default App
