import Head from "next/head";
import { Sidebar } from "~/components/area-riservata/sidebar";
import { ChatSidebar } from "~/components/chat/chat-sidebar";
import { LoadingPage } from "~/components/loading";
import { SiteHeader } from "~/components/navbar/site-header";
import { Status500 } from "~/components/status-page";
import type { NextPageWithLayout } from "~/pages/_app";
import { useTranslation } from "~/providers/I18nProvider";
import { useSession } from "~/providers/SessionProvider";
import { api } from "~/utils/api";

const AdminChats: NextPageWithLayout = () => {
	const { t } = useTranslation();
	const session = useSession();

	const { data: activeChats, isLoading: isLoadingActiveChats } =
		api.chat.getActiveChats.useQuery();

	const { data: inactiveUsers, isLoading: isLoadingInactiveUsers } =
		api.users.getActiveUsersWithoutChat.useQuery();

	if (
		session.status === "pending" ||
		isLoadingActiveChats ||
		isLoadingInactiveUsers
	)
		return <LoadingPage />;

	if (session.status !== "success" || !session.user) {
		window.location.reload();
		return <LoadingPage />;
	}
	if (!activeChats || !inactiveUsers) return <Status500 />;

	return (
		<>
			<Head>
				<title>{t.heads.area_riservata_titolo}</title>
				<meta content={t.heads.area_riservata_description} name="description" />
			</Head>
			<div className="flex h-full w-full flex-col md:flex-row">
				<Sidebar isAdmin />

				<div className="h-full flex-1 grow">
					<div className="flex h-full w-full flex-row">
						<div className="h-full border-r">
							<ChatSidebar chatId={null} />
						</div>
						<div className="w-full" />
					</div>
				</div>
			</div>
		</>
	);
};
export default AdminChats;

AdminChats.getLayout = function getLayout(page) {
	return (
		<div className="h-screen">
			<div className="flex h-full flex-col">
				<SiteHeader />
				<div className="flex min-h-0 grow">{page}</div>
			</div>
		</div>
	);
};
