'use client';

import { ChatList } from '@/components/chat/ChatList';
import { Composer } from '@/components/chat/Composer';
import { ChatEntity } from '@/lib/types';
import useVisionAgent from '@/lib/hooks/useVisionAgent';
import { useScrollAnchor } from '@/lib/hooks/useScrollAnchor';
import { Session } from 'next-auth';

export interface ChatProps extends React.ComponentProps<'div'> {
  chat: ChatEntity;
  session: Session | null;
}

export function Chat({ chat, session }: ChatProps) {
  const { url, id } = chat;
  const { messages, append, reload, stop, isLoading, input, setInput } =
    useVisionAgent(chat);

  const { messagesRef, scrollRef, visibilityRef, isAtBottom, scrollToBottom } =
    useScrollAnchor();

  return (
    <>
      <div className="h-full overflow-auto relative" ref={scrollRef}>
        <div className="pb-[200px] pt-4 md:pt-10" ref={messagesRef}>
          <ChatList
            messages={messages}
            session={session}
            isLoading={isLoading}
          />
          <div className="h-px w-full" ref={visibilityRef} />
        </div>
      </div>
      <div className="fixed inset-x-0 bottom-0 w-full animate-in duration-300 ease-in-out peer-[[data-state=open]]:group-[]:lg:pl-[250px] peer-[[data-state=open]]:group-[]:xl:pl-[300px] h-[178px]">
        <Composer
          id={id}
          url={url}
          isLoading={isLoading}
          stop={stop}
          append={append}
          reload={reload}
          messages={messages}
          input={input}
          setInput={setInput}
          isAtBottom={isAtBottom}
          scrollToBottom={scrollToBottom}
        />
      </div>
    </>
  );
}
