import { Routes, Route, Navigate, useLocation } from 'react-router-dom'
import MainLayout from './layouts/MainLayout.jsx'
import AllSkills from './pages/AllSkills.jsx'
import MySkills from './pages/MySkills.jsx'
import UploadSkill from './pages/UploadSkill.jsx'
import CreateSkill from './pages/CreateSkill.jsx'
import KnowledgeFiles from './pages/KnowledgeFiles.jsx'
import KnowledgeQA from './pages/KnowledgeQA.jsx'
import ProcessFileDetail from './pages/ProcessFileDetail.jsx'
import SkillOps from './pages/SkillOps.jsx'
import SkillEditPage from './pages/SkillEditPage.jsx'
import AuditLog from './pages/AuditLog.jsx'
import UserManagement from './pages/UserManagement.jsx'
import Architecture from './pages/Architecture.jsx'
import PlatformGovernance from './pages/PlatformGovernance.jsx'
import LlmSettings from './pages/LlmSettings.jsx'
import ApiAccess from './pages/ApiAccess.jsx'
import AiFlowDrawing from './pages/AiFlowDrawing.jsx'
import SkillSandboxPage from './pages/SkillSandboxPage.jsx'
import SkillEvalPage from './pages/SkillEvalPage.jsx'
import CreateSkillChat from './pages/CreateSkillChat.jsx'
import Login from './pages/Login.jsx'
import ComingSoon from './components/ComingSoon.jsx'
import CreateChatOverlay from './components/CreateChatOverlay.jsx'
import EvalOverlay from './components/EvalOverlay.jsx'
import SandboxOverlay from './components/SandboxOverlay.jsx'
import { useAuth } from './contexts/AuthContext.jsx'
import { ChatOverlayProvider, useChatOverlay } from './contexts/ChatOverlayContext.jsx'
import { FEATURES } from './featureFlags.js'
import { PRODUCT } from './productConfig.js'

function RequireAuth({ children }) {
  const { isAuthed } = useAuth()
  const location = useLocation()
  if (!isAuthed) {
    return <Navigate to="/login" replace state={{ from: location.pathname + location.search }} />
  }
  return children
}

// 服务降级网关：开关关闭时展示「暂未开放」占位页，原组件代码保留
function Gated({ enabled, title, children }) {
  return enabled ? children : <ComingSoon title={title} />
}

function OverlayRenderer() {
  const { activeOverlay } = useChatOverlay()
  if (!activeOverlay) return null
  switch (activeOverlay) {
    case 'create': return <CreateChatOverlay />
    case 'eval': return <EvalOverlay />
    case 'sandbox': return <SandboxOverlay />
    default: return null
  }
}

export default function App() {
  return (
    <ChatOverlayProvider>
      <Routes>
      <Route path="/login" element={<Login />} />
      <Route path="/skills/:id/sandbox" element={<RequireAuth><SkillSandboxPage /></RequireAuth>} />
      <Route element={<RequireAuth><MainLayout /></RequireAuth>}>
        <Route path="/" element={<Navigate to="/all-skills" replace />} />
        <Route path="/all-skills" element={<AllSkills />} />
        <Route path="/my-skills" element={<MySkills />} />
        <Route path="/upload" element={<UploadSkill />} />
        <Route path="/create" element={PRODUCT.modules.legacyFormCreate ? <Gated enabled={FEATURES.createSkill} title="创建 Skill"><CreateSkill /></Gated> : <Navigate to="/create-skill-chat" replace />} />
        <Route path="/process-files" element={PRODUCT.modules.knowledge ? <KnowledgeFiles /> : <Navigate to="/all-skills" replace />} />
        <Route path="/knowledge/files" element={PRODUCT.modules.knowledge ? <KnowledgeFiles /> : <Navigate to="/all-skills" replace />} />
        <Route path="/knowledge/my-files" element={PRODUCT.modules.knowledge ? <KnowledgeFiles mode="mine" /> : <Navigate to="/all-skills" replace />} />
        <Route path="/knowledge/qa" element={PRODUCT.modules.knowledge ? <KnowledgeQA /> : <Navigate to="/all-skills" replace />} />
        <Route path="/knowledge/files/:id" element={PRODUCT.modules.knowledge ? <ProcessFileDetail /> : <Navigate to="/all-skills" replace />} />
        <Route path="/governance" element={<PlatformGovernance />} />
        <Route path="/skill-ops" element={<SkillOps />} />
        <Route path="/audit-log" element={<AuditLog />} />
        <Route path="/user-management" element={<UserManagement />} />
        <Route path="/architecture" element={<Architecture />} />
        <Route path="/llm-settings" element={<Gated enabled={FEATURES.llmSettings} title="智能体配置"><LlmSettings /></Gated>} />
        <Route path="/api-access" element={<ApiAccess />} />
        <Route path="/ai-flow-drawing" element={PRODUCT.modules.aiFlowDrawing ? <AiFlowDrawing /> : <Navigate to="/all-skills" replace />} />
        <Route path="/skills/:id/edit" element={<SkillEditPage />} />
        <Route path="/skill-eval" element={<Gated enabled={FEATURES.skillEval} title="Skill 评测"><SkillEvalPage /></Gated>} />
        <Route path="/create-skill-chat" element={<CreateSkillChat />} />
        <Route path="*" element={<Navigate to="/all-skills" replace />} />
      </Route>
      </Routes>
      <OverlayRenderer />
    </ChatOverlayProvider>
  )
}
