import { createChat, getChatMessages } from "@/entities/chats/api";
import type { IChatMessage } from "@/entities/chats/types";
import { ChatProvider } from "@/providers/chat-provider";

export default async function ChatLayout({
  children,
  params,
}: Readonly<{
  children: React.ReactNode;
  params: Promise<{ assistantId: string }>;
}>) {
  const { assistantId } = await params;

  const { chatId } = await createChat(assistantId);

  const { messages: initialMessages, totalCount } = await getChatMessages(
    chatId,
    { limit: 50, page: 1 }
  );

  return (
    <ChatProvider
      chatId={chatId}
      initialMessages={initialMessages as IChatMessage[]}
      initialTotal={totalCount}
    >
      {children}
    </ChatProvider>
  );
}
