import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import { ConfigProvider } from 'antd'
import zhCN from 'antd/locale/zh_CN'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { AuthProvider } from './framework/auth'
import { PermissionProvider } from './framework/permission'
import { RouteGuard } from './framework/router'
import MainLayout from './framework/layout/MainLayout'
import AdminLayout from './framework/layout/AdminLayout'
import OrgLayout from './framework/layout/OrgLayout'
import HomePage from './pages/HomePage'
import LoginPage from './pages/LoginPage'
import AdminDashboard from './pages/admin/AdminDashboard'
import OrgDashboard from './pages/org/OrgDashboard'
import PromptListPage from './pages/admin/PromptListPage'
import PromptEditorPage from './pages/admin/PromptEditorPage'
import TenantListPage from './pages/admin/tenants/TenantListPage'
import TenantDetailPage from './pages/admin/tenants/TenantDetailPage'
import { MODULES } from './framework/modules/moduleRegistry'
// 用户管理页面
import UserListPage from './modules/admin/pages/UserListPage'
import UserFormPage from './modules/admin/pages/UserFormPage'
import UserDetailPage from './modules/admin/pages/UserDetailPage'
// 系统知识库管理
import SystemKbListPage from './modules/admin/pages/SystemKbListPage'
import SystemKbDetailPage from './modules/admin/pages/SystemKbDetailPage'
// IIT 项目管理
import IitProjectListPage from './modules/admin/pages/IitProjectListPage'
import IitProjectDetailPage from './modules/admin/pages/IitProjectDetailPage'
import IitQcCockpitPage from './modules/admin/pages/IitQcCockpitPage'
// 运营日志
import ActivityLogsPage from './pages/admin/ActivityLogsPage'
// 个人中心页面
import ProfilePage from './pages/user/ProfilePage'

/**
 * 应用根组件
 * 
 * @description 
 * - ConfigProvider: Ant Design国际化配置
 * - QueryClientProvider: React Query状态管理
 * - AuthProvider: JWT认证管理 🆕
 * - PermissionProvider: 权限管理系统
 * - RouteGuard: 路由守卫保护
 * - BrowserRouter: 前端路由
 * 
 * 路由结构：
 * - /login - 通用登录页（个人用户）
 * - /t/{tenantCode}/login - 租户专属登录页
 * - / - 首页（需要认证）
 * - /{module}/* - 业务模块（需要认证+权限）
 */

// 创建React Query客户端
const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: 1000 * 60 * 5, // 5分钟
      gcTime: 1000 * 60 * 10, // 10分钟
      retry: 1,
      refetchOnWindowFocus: false,
    },
  },
})

function App() {
  return (
    <ConfigProvider locale={zhCN}>
      <QueryClientProvider client={queryClient}>
        {/* 认证提供者：JWT Token管理 */}
        <AuthProvider>
          {/* 权限提供者：模块级权限管理 */}
          <PermissionProvider>
            <BrowserRouter>
              <Routes>
                {/* 登录页面（无需认证） */}
                <Route path="/login" element={<LoginPage />} />
                <Route path="/t/:tenantCode/login" element={<LoginPage />} />
                
                {/* 业务应用端 /app/* */}
                <Route path="/" element={<MainLayout />}>
                  {/* 首页 */}
                  <Route index element={<HomePage />} />
                  
                  {/* 动态加载模块路由 - 基于模块权限系统 ⭐ 2026-01-16 */}
                  {MODULES.map(module => (
                    <Route
                      key={module.id}
                      path={`${module.path}/*`}
                      element={
                        <RouteGuard
                          requiredModule={module.moduleCode}
                          moduleName={module.name}
                        >
                          <module.component />
                        </RouteGuard>
                      }
                    />
                  ))}
                  
                  {/* 个人中心路由 - 2026-01-28 新增 */}
                  <Route path="/user/profile" element={<ProfilePage />} />
                  <Route path="/user/settings" element={<ProfilePage />} />
                </Route>

                {/* 运营管理端 /admin/* */}
                <Route path="/admin" element={<AdminLayout />}>
                  <Route index element={<Navigate to="/admin/dashboard" replace />} />
                  <Route path="dashboard" element={<AdminDashboard />} />
                  {/* Prompt 管理 */}
                  <Route path="prompts" element={<PromptListPage />} />
                  <Route path="prompts/:code" element={<PromptEditorPage />} />
                  {/* 租户管理 */}
                  <Route path="tenants" element={<TenantListPage />} />
                  <Route path="tenants/:id" element={<TenantDetailPage />} />
                  {/* 用户管理 */}
                  <Route path="users" element={<UserListPage />} />
                  <Route path="users/create" element={<UserFormPage mode="create" />} />
                  <Route path="users/:id" element={<UserDetailPage />} />
                  <Route path="users/:id/edit" element={<UserFormPage mode="edit" />} />
                  {/* 系统知识库 */}
                  <Route path="system-kb" element={<SystemKbListPage />} />
                  <Route path="system-kb/:id" element={<SystemKbDetailPage />} />
                  {/* IIT 项目管理 */}
                  <Route path="iit-projects" element={<IitProjectListPage />} />
                  <Route path="iit-projects/:id" element={<IitProjectDetailPage />} />
                  <Route path="iit-projects/:id/cockpit" element={<IitQcCockpitPage />} />
                  {/* 运营日志 */}
                  <Route path="activity-logs" element={<ActivityLogsPage />} />
                  {/* 系统配置 */}
                  <Route path="system" element={<div className="text-center py-20">🚧 系统配置页面开发中...</div>} />
                </Route>

                {/* 机构管理端 /org/* */}
                <Route path="/org" element={<OrgLayout />}>
                  <Route index element={<Navigate to="/org/dashboard" replace />} />
                  <Route path="dashboard" element={<OrgDashboard />} />
                  <Route path="users" element={<div className="text-center py-20">🚧 用户管理页面开发中...</div>} />
                  <Route path="departments" element={<div className="text-center py-20">🚧 科室/部门管理页面开发中...</div>} />
                  <Route path="usage" element={<div className="text-center py-20">🚧 使用统计页面开发中...</div>} />
                  <Route path="audit" element={<div className="text-center py-20">🚧 审计日志页面开发中...</div>} />
                </Route>

                {/* 404重定向 */}
                <Route path="*" element={<Navigate to="/" replace />} />
              </Routes>
            </BrowserRouter>
          </PermissionProvider>
        </AuthProvider>
      </QueryClientProvider>
    </ConfigProvider>
  )
}

export default App
