"use client";

import { memo } from "react";
import ChatBottombar from "~/components/chat/chat-bottombar";
import { ChatList } from "~/components/chat/chat-list";
import ChatTopbar from "~/components/chat/chat-topbar";
import { useLiveChat } from "~/hooks/chatHooks";
import type { ChatsChatid } from "~/schemas/public/Chats";
import type { Session } from "~/server/api/trpc";
import { api } from "~/utils/api";

const Chat = memo(
	({ chatId, userData }: { chatId: ChatsChatid; userData: Session }) => {
		const liveChat = useLiveChat(chatId, userData);
		const [userinfo] = api.chat.getUserInfosByChat.useSuspenseQuery({
			chatId,
		});

		return (
			<div className="flex h-full w-full flex-col justify-between">
				<div className="shrink-0">
					<ChatTopbar
						chatId={chatId}
						chatUserData={userinfo}
						onlineStatus={liveChat.onlineUsers}
						userData={userData}
					/>
				</div>
				<ChatList
					chatId={chatId}
					messages={liveChat.messages}
					query={liveChat.query}
					typingStatus={liveChat.typingStatus}
					userData={userData}
				/>

				<div className="shrink-0 border-t">
					<ChatBottombar
						chatId={chatId}
						chatUserData={userinfo}
						userData={userData}
					/>
				</div>
			</div>
		);
	},
	(prevProps, nextProps) =>
		prevProps.chatId === nextProps.chatId &&
		prevProps.userData.id === nextProps.userData.id,
);
Chat.displayName = "Chat";

export default Chat;
