import { AppSidebar } from "@/modules/layout/components/app-sidebar"
import { SidebarProvider } from "@/modules/layout/components/sidebar-provider"
import { SiteHeader } from "@/modules/layout/components/site-header"
import {
  AiClientProvider,
} from "@/modules/ai/context/ai-client-provider"
import { AiAssistantWidget } from "@/modules/ai/components/ai-assistant-widget"
import { createFullAiClientService } from "@/modules/ai/context/create-ai-client-service"

export default function DashboardLayout({
  children,
}: {
  children: React.ReactNode
}): React.ReactNode {
  // 在 Server Component 函数体内创建 service，避免模块级单例在未来扩展请求级状态时泄露上下文。
  // service 对象本身仅是 Server Action 引用集合，无共享可变状态，Action 内部通过 requirePermission() 动态解析当前用户。
  const aiClientService = createFullAiClientService()

  return (
    <AiClientProvider service={aiClientService}>
      <SidebarProvider sidebar={<AppSidebar />}>
        <a href="#main-content" className="sr-only focus:not-sr-only focus:absolute focus:z-50 focus:p-4 focus:bg-background focus:text-foreground focus:border focus:border-border focus:rounded-md focus:m-2">
          Skip to main content
        </a>
        <SiteHeader />
        <main id="main-content" className="flex-1 overflow-auto p-6">
          {children}
        </main>
        <AiAssistantWidget />
      </SidebarProvider>
    </AiClientProvider>
  )
}
