'use client';

import { cn } from '@/lib/utils';
import { ChatList } from '@/components/chat/ChatList';
import { ChatPanel } from '@/components/chat/ChatPanel';
import { ChatEntity } from '@/lib/types';
import Image from 'next/image';
import useVisionAgent from '@/lib/hooks/useVisionAgent';
import { useScrollAnchor } from '@/lib/hooks/useScrollAnchor';
import { ButtonScrollToBottom } from '../ui/ButtonScrollToBottom';

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

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

  const { messagesRef, scrollRef, visibilityRef, isAtBottom, scrollToBottom } =
    useScrollAnchor();
  console.log('[Ming] ~ Chat ~ isAtBottom:', isAtBottom);

  return (
    <>
      <div className="h-full overflow-auto" ref={scrollRef}>
        <div className="pb-[200px] pt-4 md:pt-10" ref={messagesRef}>
          <ChatList messages={messages} />
          <div className="h-px w-full" ref={visibilityRef} />
        </div>
      </div>
      <ChatPanel
        id={id}
        url={url}
        isLoading={isLoading}
        stop={stop}
        append={append}
        reload={reload}
        messages={messages}
        input={input}
        setInput={setInput}
      />
      <ButtonScrollToBottom
        isAtBottom={isAtBottom}
        scrollToBottom={scrollToBottom}
      />
    </>
  );
}
