// SPEC: PJ2026-010404 项目管理; PJ2026-010405 云端控制台。
// Implementation reference: draft-2026-07-13-p0-cloud-console.
// Responsibility: 维护 Cloud Web 稳定实体深链；HWPOD 实体身份进入 path，query 只保存集合视图状态。

import { createRouter, createWebHistory, type RouteRecordRaw } from "vue-router";
import { installRouterGuards } from "./guards";

const CodeWorkbenchView = () => import("@/views/workbench/CodeWorkbenchView.vue");
const WorkbenchDebugView = () => import("@/views/workbench/WorkbenchDebugView.vue");
const OpenCodeFrameView = () => import("@/views/opencode/OpenCodeFrameView.vue");
const ProjectsView = () => import("@/views/projects/ProjectsView.vue");
const ProjectMdtodoView = () => import("@/views/projects/MdtodoView.vue");
const TaskTreeView = () => import("@/views/projects/TaskTreeView.vue");
const AgentRunsView = () => import("@/views/agents/AgentRunsView.vue");
const CaseRunView = () => import("@/views/caserun/CaseRunView.vue");

const routes: RouteRecordRaw[] = [
  { path: "/", redirect: "/workbench" },
  { path: "/login", name: "Login", component: () => import("@/views/LoginView.vue"), meta: { requiresAuth: false, title: "登录" } },
  { path: "/register", name: "Register", component: () => import("@/views/RegisterView.vue"), meta: { requiresAuth: false, title: "注册" } },
  { path: "/dashboard", name: "Dashboard", component: () => import("@/views/user/DashboardView.vue"), meta: { requiresAuth: true, navId: "user.dashboard", title: "平台概览", section: "user" } },
  { path: "/workbench/debug", name: "WorkbenchDebug", component: WorkbenchDebugView, meta: { requiresAuth: true, navId: "workbench.debug", title: "Workbench 调试", section: "workbench-debug" } },
  { path: "/workbench/sessions/:sessionId", alias: ["/workspace/sessions/:sessionId"], name: "CodeWorkbenchSession", component: CodeWorkbenchView, meta: { requiresAuth: true, navId: "workbench.code", title: "Code 工作台", section: "workbench" } },
  { path: "/workbench", alias: ["/workspace"], name: "CodeWorkbench", component: CodeWorkbenchView, meta: { requiresAuth: true, navId: "workbench.code", title: "Code 工作台", section: "workbench" } },
  { path: "/caserun", name: "CaseRun", component: CaseRunView, meta: { requiresAuth: true, navId: "workbench.code", title: "CaseRun 执行台", section: "caserun", boundedLayout: true } },
  { path: "/caserun/runs/:runId", name: "CaseRunDetail", component: CaseRunView, meta: { requiresAuth: true, navId: "workbench.code", navParent: "CaseRun", title: "CaseRun 详情", section: "caserun", boundedLayout: true } },
  { path: "/agents/runs", name: "AgentRuns", component: AgentRunsView, meta: { requiresAuth: true, navId: "workbench.code", title: "AgentRun 运行观察", section: "agents" } },
  { path: "/agents/runs/:runId", name: "AgentRunDetail", component: AgentRunsView, meta: { requiresAuth: true, navId: "workbench.code", navParent: "AgentRuns", title: "AgentRun 详情", section: "agents" } },
  { path: "/opencode", name: "OpenCode", component: OpenCodeFrameView, meta: { requiresAuth: true, navId: "opencode.root", title: "OpenCode", section: "opencode" } },
  { path: "/projects", name: "Projects", component: ProjectsView, meta: { requiresAuth: true, navId: "project.overview", title: "项目", section: "project" } },
  { path: "/projects/mdtodo", name: "ProjectMdtodo", component: ProjectMdtodoView, meta: { requiresAuth: true, navId: "project.mdtodo", title: "MDTODO", section: "project" } },
  { path: "/projects/tasktree", name: "TaskTree", component: TaskTreeView, meta: { requiresAuth: true, navId: "project.tasktree", title: "TaskTree", section: "project" } },
  { path: "/projects/tasktree/:groupId", name: "TaskTreeGroup", component: TaskTreeView, meta: { requiresAuth: true, navId: "project.tasktree", navParent: "TaskTree", title: "TaskTree", section: "project" } },
  { path: "/projects/mdtodo/sources/:sourceId/files/:fileRef", name: "ProjectMdtodoFile", component: ProjectMdtodoView, meta: { requiresAuth: true, navId: "project.mdtodo", title: "MDTODO", section: "project" } },
  { path: "/projects/mdtodo/sources/:sourceId/files/:fileRef/tasks/:taskId", name: "ProjectMdtodoTask", component: ProjectMdtodoView, meta: { requiresAuth: true, navId: "project.mdtodo", title: "MDTODO", section: "project" } },
  { path: "/projects/mdtodo/sources/:sourceId/files/:fileRef/tasks/:taskId/reports/:linkId", name: "ProjectMdtodoReport", component: ProjectMdtodoView, meta: { requiresAuth: true, navId: "project.mdtodo", title: "MDTODO", section: "project" } },
  { path: "/projects/:projectId", name: "ProjectDetail", component: ProjectsView, meta: { requiresAuth: true, navId: "project.overview", navParent: "Projects", title: "项目详情", section: "project" } },
  { path: "/api-keys", name: "ApiKeys", component: () => import("@/views/user/ApiKeysView.vue"), meta: { requiresAuth: true, navId: "user.apiKeys", title: "API Keys", section: "user" } },
  { path: "/api-keys/:keyId", name: "ApiKeyDetail", component: () => import("@/views/user/ApiKeysView.vue"), meta: { requiresAuth: true, navId: "user.apiKeys", navParent: "ApiKeys", title: "API Key 详情", section: "user" } },
  { path: "/usage", name: "Usage", component: () => import("@/views/user/UsageView.vue"), meta: { requiresAuth: true, navId: "user.usage", title: "用量与性能", section: "user" } },
  { path: "/billing", name: "Billing", component: () => import("@/views/user/BillingView.vue"), meta: { requiresAuth: true, navId: "user.billing", title: "充值与订阅", section: "user" } },
  { path: "/performance", name: "Performance", component: () => import("@/views/PerformanceView.vue"), meta: { requiresAuth: true, navId: "system.performance", title: "性能监控", section: "system" } },
  { path: "/admin/access", alias: ["/access"], name: "Access", component: () => import("@/views/admin/AccessView.vue"), meta: { requiresAuth: true, requiresAdmin: true, navId: "admin.access", title: "授权管理", section: "admin" } },
  { path: "/admin/access/users/:userId", name: "AccessUserDetail", component: () => import("@/views/admin/AccessView.vue"), meta: { requiresAuth: true, requiresAdmin: true, navId: "admin.access", navParent: "Access", title: "授权用户详情", section: "admin" } },
  { path: "/admin/secrets", name: "AdminSecrets", component: () => import("@/views/admin/SecretsView.vue"), meta: { requiresAuth: true, requiresAdmin: true, navId: "admin.secrets", title: "密钥管理", section: "admin" } },
  { path: "/admin/secrets/external-secrets/:namespace/:name", name: "AdminExternalSecretDetail", component: () => import("@/views/admin/ExternalSecretDetailView.vue"), meta: { requiresAuth: true, requiresAdmin: true, navId: "admin.secrets", title: "ExternalSecret 详情", section: "admin" } },
  { path: "/admin/users", name: "Users", component: () => import("@/views/admin/UsersView.vue"), meta: { requiresAuth: true, requiresAdmin: true, navId: "admin.users", title: "用户管理", section: "admin" } },
  { path: "/admin/users/:userId", name: "UserDetail", component: () => import("@/views/admin/UsersView.vue"), meta: { requiresAuth: true, requiresAdmin: true, navId: "admin.users", navParent: "Users", title: "用户详情", section: "admin" } },
  { path: "/admin/billing", name: "AdminBilling", component: () => import("@/views/admin/BillingView.vue"), meta: { requiresAuth: true, requiresAdmin: true, navId: "admin.billing", title: "兑换与订阅", section: "admin" } },
  { path: "/admin/hwpod-groups", redirect: "/hwpods/devices" },
  { path: "/hwpods", redirect: "/hwpods/devices" },
  { path: "/hwpods/devices/:hwpodId?", name: "HwpodDevices", component: () => import("@/views/admin/HwpodGroupsView.vue"), meta: { requiresAuth: true, requiresAdmin: true, navId: "admin.hwpodGroups", navParent: "HwpodGroups", title: "HWPOD 设备资源", section: "admin", boundedLayout: true } },
  { path: "/hwpods/nodes/:nodeId?", name: "HwpodNodes", component: () => import("@/views/admin/HwpodGroupsView.vue"), meta: { requiresAuth: true, requiresAdmin: true, navId: "admin.hwpodGroups", navParent: "HwpodGroups", title: "HWPOD Node", section: "admin", boundedLayout: true } },
  { path: "/hwpods/onboarding/:nodeId?", name: "HwpodOnboarding", component: () => import("@/views/admin/HwpodGroupsView.vue"), meta: { requiresAuth: true, requiresAdmin: true, navId: "admin.hwpodGroups", navParent: "HwpodGroups", title: "HWPOD 节点接入", section: "admin", boundedLayout: true } },
  { path: "/admin/provider-profiles", name: "ProviderProfiles", component: () => import("@/views/admin/ProviderProfilesView.vue"), meta: { requiresAuth: true, requiresAdmin: true, navId: "admin.providerProfiles", title: "Provider Profiles", section: "admin" } },
  { path: "/admin/provider-profiles/:profile", name: "ProviderProfileDetail", component: () => import("@/views/admin/ProviderProfilesView.vue"), meta: { requiresAuth: true, requiresAdmin: true, navId: "admin.providerProfiles", navParent: "ProviderProfiles", title: "Provider Profile 详情", section: "admin" } },
  { path: "/skills", name: "Skills", component: () => import("@/views/SkillsView.vue"), meta: { requiresAuth: true, navId: "system.skills", title: "技能包", section: "system" } },
  { path: "/skills/:skillId", name: "SkillDetail", component: () => import("@/views/SkillsView.vue"), meta: { requiresAuth: true, navId: "system.skills", navParent: "Skills", title: "技能包详情", section: "system" } },
  { path: "/skills/:skillId/files/:filePath(.*)*", name: "SkillFile", component: () => import("@/views/SkillsView.vue"), meta: { requiresAuth: true, navId: "system.skills", navParent: "Skills", title: "技能文件", section: "system" } },
  { path: "/settings", name: "Settings", component: () => import("@/views/SettingsView.vue"), meta: { requiresAuth: true, navId: "system.settings", title: "设置", section: "system" } },
  { path: "/gate", alias: ["/diagnostics/gate"], name: "Gate", component: () => import("@/views/GateView.vue"), meta: { requiresAuth: true, navId: "system.gate", title: "内部复核", section: "system" } },
  { path: "/help", name: "Help", component: () => import("@/views/HelpView.vue"), meta: { requiresAuth: true, navId: "system.help", title: "帮助", section: "system" } },
  { path: "/:pathMatch(.*)*", name: "NotFound", component: () => import("@/views/NotFoundView.vue"), meta: { requiresAuth: false, title: "未找到" } }
];

const router = createRouter({ history: createWebHistory(), routes });
installRouterGuards(router);

export default router;
