import { BackendDownNotice } from "@/components/BackendDownNotice";
import { ToolboxPage } from "@/components/toolbox/ToolboxPage";
import { fetchProject, fetchToolbox } from "@/lib/api/server";

interface PageProps {
  params: Promise<{ id: string }>;
  searchParams: Promise<{ open?: string }>;
}

// 创作工具箱页。Server Component 取项目 + 工具描述符（GET /skills/toolbox，错误降级为空）。
// ?open=<tool_key> 由命令面板 action-gen-<key> 跳转，进页直开对应生成器（仅新工具）。
export default async function ToolboxRoutePage({
  params,
  searchParams,
}: PageProps) {
  const { id } = await params;
  const { open } = await searchParams;
  try {
    const [project, toolbox] = await Promise.all([
      fetchProject(id),
      fetchToolbox(),
    ]);
    return (
      <ToolboxPage
        project={project}
        tools={toolbox.tools ?? []}
        initialOpenKey={open}
      />
    );
  } catch {
    return <BackendDownNotice className="m-6" />;
  }
}
