<!-- SPEC: PJ2026-010405 云端控制台. -->
<script setup lang="ts">
import { ChartBar, FolderKanban, ListChecks, Monitor } from "lucide-vue-next";
import { computed, onMounted, ref } from "vue";
import { systemAPI } from "@/api";
import AsyncBoundary from "@/components/common/AsyncBoundary.vue";
import DashboardWorkspace, { type DashboardEntry } from "@/components/dashboard/DashboardWorkspace.vue";
import PageCommandBar from "@/components/layout/PageCommandBar.vue";
import { useAuthStore } from "@/stores/auth";
import type { DashboardSummaryResponse } from "@/types";

const auth = useAuthStore();
const summary = ref<DashboardSummaryResponse | null>(null);
const loading = ref(true);
const error = ref<string | null>(null);
const actorName = computed(() => auth.user?.displayName || auth.user?.username || auth.user?.name || "HWLAB 用户");
const entries = computed<DashboardEntry[]>(() => [
  { id: "workbench.code", title: "Code Workbench", description: "继续会话、提交任务并查看可追溯运行结果。", path: "/workbench", icon: Monitor },
  { id: "project.overview", title: "Projects", description: "查看项目、source readiness、任务与公共 Workbench 关联。", path: "/projects", icon: FolderKanban },
  { id: "project.mdtodo", title: "MDTODO", description: "进入有界三栏任务工作区并恢复 source / file / task 深链。", path: "/projects/mdtodo", icon: ListChecks },
  { id: "user.usage", title: "Usage", description: "查看账本 authority 返回的用量、额度和 active reservation。", path: "/usage", icon: ChartBar }
].filter((entry) => auth.canAccessNav(entry.id)));
const asyncState = computed(() => loading.value ? summary.value ? "refreshing" : "initial-loading" : error.value ? summary.value ? "partial" : "error" : summary.value ? "ready" : "empty");

onMounted(refresh);
async function refresh(): Promise<void> {
  loading.value = true;
  const response = await systemAPI.dashboardSummary();
  loading.value = false;
  if (!response.ok) { error.value = response.error ?? `HTTP ${response.status}`; return; }
  summary.value = response.data;
  error.value = null;
}
</script>

<template>
  <section class="route-stack dashboard-page">
    <PageCommandBar eyebrow="Cloud Console" title="平台概览" description="从当前身份可访问的工作入口继续，不用装饰指标代替业务事实。" />
    <AsyncBoundary :state="asyncState" :has-content="Boolean(summary)" label="加载控制台聚合摘要" error-title="控制台摘要不可用" :error="error || '请求未完成'" @retry="refresh">
      <DashboardWorkspace v-if="summary" :summary="summary" :entries="entries" :actor-name="actorName" :error="error" />
    </AsyncBoundary>
  </section>
</template>
