import { RefreshCcw } from "lucide-react";
import type { GetServerSideProps } from "next";
import Head from "next/head";
import { useState } from "react";
import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
import { Status500 } from "~/components/status-page";
import { UsersTable } from "~/components/tables/users-table";
import { Button } from "~/components/ui/button";
import {
	Dialog,
	DialogContent,
	DialogDescription,
	DialogHeader,
	DialogTitle,
	DialogTrigger,
} from "~/components/ui/dialog";
import { FormNewUser } from "~/forms/FormNewUser";

import type { NextPageWithLayout } from "~/pages/_app";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";

import { api } from "~/utils/api";

/**
 * Pagina di gestione utenti per admin: /area-riservata/admin/utenti
 */
const Admin_Utenti: NextPageWithLayout = () => {
	const { data, isLoading, refetch } = api.users.getUsersWChatInfo.useQuery(
		undefined,
		{
			refetchOnWindowFocus: true,
		},
	);

	if (isLoading) return <LoadingPage />;
	if (!data) return <Status500 />;
	return (
		<>
			<Head>
				<title>Utenti - Infoalloggi.it</title>
			</Head>
			<div className="mx-1 flex-1 overflow-auto">
				<div className="mx-auto pt-4">
					<div className="mx-3 items-start justify-between md:flex">
						<div className="flex max-w-lg items-center gap-3">
							<h3 className="font-bold text-2xl">Utenti</h3>
							<button
								className="cursor-pointer"
								onClick={async () => await refetch()}
								type="button"
							>
								<RefreshCcw className="size-6" />
							</button>
						</div>
						<div className="mt-3 md:mt-0">
							<NewUserModal />
						</div>
					</div>

					<UsersTable data={data} />
				</div>
			</div>
		</>
	);
};

export default Admin_Utenti;

export const getServerSideProps = (async (context) => {
	const access_token = context.req.cookies.access_token;

	const helpers = await TrpcAuthedFetchingIstance({ access_token });
	if (helpers) {
		await helpers.trpc.users.getUsersWChatInfo.prefetch();
		return {
			props: {
				trpcState: helpers.trpc.dehydrate(),
			},
		};
	}
	return {
		props: {},
	};
}) satisfies GetServerSideProps;

Admin_Utenti.getLayout = function getLayout(page) {
	return <AreaRiservataLayout>{page}</AreaRiservataLayout>;
};

const NewUserModal = () => {
	const [open, setOpen] = useState(false);
	const handleOpen = (status: boolean) => setOpen(status);
	return (
		<Dialog onOpenChange={setOpen} open={open}>
			<DialogTrigger asChild>
				<Button variant="outline">Aggiungi Utente</Button>
			</DialogTrigger>
			<DialogContent className="max-h-[80%] overflow-auto sm:max-w-106.25">
				<DialogHeader>
					<DialogTitle>Aggiungi Utente</DialogTitle>
					<DialogDescription className="sr-only">
						Aggiungi un nuovo utente al sistema.
					</DialogDescription>
				</DialogHeader>
				<div className="py-4">
					<FormNewUser setOpen={handleOpen} />
				</div>
			</DialogContent>
		</Dialog>
	);
};
