﻿import { Suspense, lazy } from 'react'
import { BrowserRouter, Routes, Route } from 'react-router-dom'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ConfigProvider } from 'antd'
import zhCN from 'antd/locale/zh_CN'
import { AuthProvider } from './contexts/AuthContext'
import AuthGuard from './components/AuthGuard'
import PermissionGuard from './components/PermissionGuard'
import PageLoading from './components/PageLoading'
import AdminLayout from './layouts/AdminLayout'

const Login = lazy(() => import('./pages/Login'))
const KnowledgeBasesPage = lazy(() => import('./pages/KnowledgeBases'))
const BillingPage = lazy(() => import('./pages/Billing'))
const GiteaEmbed = lazy(() => import('./pages/GiteaEmbed'))
const ServersPage = lazy(() => import("./pages/Servers"))
const AuditLogPage = lazy(() => import("./pages/AuditLog"))
const Dashboard = lazy(() => import('./pages/Dashboard'))
const UserManagement = lazy(() => import('./pages/UserManagement'))
const HermesSettings = lazy(() => import('./pages/HermesSettings'))
const TaskAssistant = lazy(() => import('./pages/TaskAssistant'))
const Placeholder = lazy(() => import('./pages/Placeholder'))
const ForbiddenPage = lazy(() => import('./pages/403'))
const NotFoundPage = lazy(() => import('./pages/404'))
const ServerErrorPage = lazy(() => import('./pages/500'))

const queryClient = new QueryClient()

function App() {
  return (
    <QueryClientProvider client={queryClient}>
      <ConfigProvider locale={zhCN}>
        <AuthProvider>
          <BrowserRouter>
            <Suspense fallback={<PageLoading />}>
              <Routes>
                <Route path="/login" element={<Login />} />
                <Route path="/403" element={<ForbiddenPage />} />
                <Route path="/500" element={<ServerErrorPage />} />
                <Route path="/404" element={<NotFoundPage />} />
                <Route
                  element={
                    <AuthGuard>
                      <AdminLayout />
                    </AuthGuard>
                  }
                >
                  <Route index element={<Dashboard />} />
                  <Route path="assistant" element={<TaskAssistant />} />
                  <Route
                    path="users"
                    element={
                      <PermissionGuard requiredRole="ADMIN">
                        <UserManagement />
                      </PermissionGuard>
                    }
                  />
                  <Route
                    path="users/admins"
                    element={
                      <PermissionGuard requiredRole="SUPER_ADMIN">
                        <UserManagement />
                      </PermissionGuard>
                    }
                  />
                  <Route path="users/members" element={<Placeholder title="普通用户" />} />
                  <Route
                    path="membership"
                    element={
                      <PermissionGuard requiredRole="ADMIN">
                        <Placeholder title="会员与额度" />
                      </PermissionGuard>
                    }
                  />
                  <Route path="knowledge/bases" element={<Suspense fallback={<PageLoading />}><KnowledgeBasesPage /></Suspense>} />
                  <Route path="knowledge/sources" element={<Placeholder title="知识源列表" />} />
                  <Route path="imports" element={<Placeholder title="文档导入" />} />
                  <Route path="files" element={<Placeholder title="文件与 COS" />} />
                  <Route
                    path="settings"
                    element={
                      <PermissionGuard requiredRole="ADMIN">
                        <Placeholder title="系统配置" />
                      </PermissionGuard>
                    }
                  />
                  <Route
                    path="knowledge/bases"
                    element={<Suspense fallback={<PageLoading />}><KnowledgeBasesPage /></Suspense>}
                  />
                  <Route
                    path="billing"
                    element={<PermissionGuard requiredRole="SUPER_ADMIN"><Suspense fallback={<PageLoading />}><BillingPage /></Suspense></PermissionGuard>}
                  />
                  <Route
                    path="git"
                    element={
                      <Suspense fallback={<PageLoading />}><GiteaEmbed /></Suspense>
                    }
                  />
                  <Route
                    path="servers"
                    element={
                      <PermissionGuard requiredRole="SUPER_ADMIN">
                        <Suspense fallback={<PageLoading />}><ServersPage /></Suspense>
                      </PermissionGuard>
                    }
                  />
                  <Route
                    path="audit"
                    element={
                      <PermissionGuard requiredRole="ADMIN">
                        <Suspense fallback={<PageLoading />}><AuditLogPage /></Suspense>
                      </PermissionGuard>
                    }
                  />
                  <Route path="*" element={<NotFoundPage />} />
                </Route>
              </Routes>
            </Suspense>
          </BrowserRouter>
        </AuthProvider>
      </ConfigProvider>
    </QueryClientProvider>
  )
}

export default App
