import ChatInterface from '../../../components/ChatInterface';
import { auth, sessionUser } from '@/auth';
import { dbGetChat } from '@/lib/db/functions';
import { redirect } from 'next/navigation';
import { revalidatePath } from 'next/cache';
import TopPrompt from '@/components/chat/TopPrompt';
import { Provider } from 'jotai';

interface ChatServerProps {
  chatId: string;
}

export default async function ChatServer({ chatId }: ChatServerProps) {
  const chat = await dbGetChat(chatId);
  const { id: userId } = await sessionUser();

  if (!chat) {
    revalidatePath('/');
    redirect('/');
  }
  return (
    <Provider>
      <div className="w-[1600px] max-w-full mx-auto flex flex-col space-y-4 items-center">
        <TopPrompt chat={chat} userId={userId} />
        <ChatInterface chat={chat} userId={userId} />
      </div>
    </Provider>
  );
}
