import { Routes, Route, Navigate } from 'react-router-dom';
import MainLayout from '@/components/Layout/MainLayout';
import SettingLayout from '@/components/Layout/SettingLayout';
import Login from '@/pages/login/Login';
import PipelineConfig from '@/pages/PipelineConfig';
import Dashboard from '@/pages/Dashboard';
import ModelsResources from '@/pages/ModelsResources';
import AgentList from '@/pages/agent-mui/list';
import {
  KnowledgeBaseList,
  KnowledgeBaseCreate,
  KnowledgeBaseDetail,
  KnowledgeBaseSetting,
  KnowledgeBaseTesting,
  KnowledgeLogsPage
} from '@/pages/knowledge';
import {
  ModelsSetting,
  SystemSetting,
  TeamsSetting,
  ProfileSetting,
  MCPSetting,
} from '@/pages/setting';
import MCP from '@/pages/MCP';
import FormFieldTest from '@/pages/FormFieldTest';
import ChunkParsedResult from '@/pages/chunk/parsed-result';
import DocumentPreview from '@/pages/chunk/document-preview';
import AgentDetailPage from '@/pages/agent-mui/detail';
// import AgentDetailPage from '@/pages/agent';
import RagflowLayout from '@/pages/ragflow/layout';
import RagflowIframePage from '@/pages/ragflow/iframe';
import RagflowAgentPage from '@/pages/ragflow/agent';

const AppRoutes = () => {
  return (
    <Routes>
      <Route path="/login" element={<Login />} />

      {/* 使用MainLayout作为受保护路由的布局 */}
      <Route path="/" element={<MainLayout />}>
        <Route index element={<KnowledgeBaseList />} />
        <Route path="knowledge">
          <Route index element={<KnowledgeBaseList />} />
          <Route path="create" element={<KnowledgeBaseCreate />} />
          {/* 详情通用一个Layout */}
          <Route path=':id'>
            <Route path="testing" element={<KnowledgeBaseTesting />} />
            <Route index element={<KnowledgeBaseDetail />} />
            <Route path="setting" element={<KnowledgeBaseSetting />} />
            <Route path="overview" element={<KnowledgeLogsPage />} />
          </Route>
        </Route>
        <Route path="agents">
          <Route index element={<AgentList />} />
        </Route>
      </Route>
      <Route path="agent">
        <Route path=":id" element={<AgentDetailPage />} />
      </Route>
      {/* 处理chunk相关路由 需要传入 kb_id doc_id */}
      <Route path="chunk">
        <Route path="parsed-result" element={<ChunkParsedResult />} />
        {/* 文档预览页面路由 */}
        <Route path="document-preview/:kb_id/:doc_id" element={<DocumentPreview />} />
      </Route>
      {/* setting 相关路由 */}
      <Route path="setting" element={<SettingLayout />}>
        <Route index element={<ModelsSetting />} />
        <Route path="models" element={<ModelsSetting />} />
        <Route path="system" element={<SystemSetting />} />
        <Route path="teams" element={<TeamsSetting />} />
        <Route path="profile" element={<ProfileSetting />} />
        <Route path="mcp" element={<MCPSetting />} />
      </Route>

      {/* 通过 iframe 承载 ragflow 应用，避免路由冲突并保持同源 */}
      {/* ragflow 路由分组：layout 承载，agent 为特定页面，其余走通用 iframe */}
      <Route path="route-ragflow" element={<RagflowLayout />}>
        <Route path="agent/:id" element={<RagflowAgentPage />} />
        <Route path="*" element={<RagflowIframePage />} />
      </Route>

      {/* 处理未匹配的路由 */}
      <Route path="*" element={<Navigate to="/knowledge" replace />} />
    </Routes>
  );
};

export default AppRoutes;