import { ArrowRight } from "lucide-react";
import type { GetServerSideProps } from "next";
import Link from "next/link";
import { UserDashboardFaq } from "~/components/area-riservata/dashboard";
import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
import { Servizio } from "~/components/servizio/servizio";
import { Status500 } from "~/components/status-page";
import { Button } from "~/components/ui/button";
import { Separator } from "~/components/ui/separator";
import type { NextPageWithLayout } from "~/pages/_app";
import { useTranslation } from "~/providers/I18nProvider";
import { useEnforcedSession } from "~/providers/SessionProvider";
import type { ServizioServizioId } from "~/schemas/public/Servizio";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zServizioId } from "~/server/utils/zod_types";
import { api } from "~/utils/api";

type ServizioPageProps = {
	servizioId: ServizioServizioId;
};
const ServizioPage: NextPageWithLayout<ServizioPageProps> = ({
	servizioId,
}: ServizioPageProps) => {
	const { user } = useEnforcedSession();
	const { t } = useTranslation();
	const { data, isLoading } = api.servizio.getServizioData.useQuery({
		servizioId,
	});

	if (isLoading) {
		return <LoadingPage />;
	}
	if (!data) {
		return <Status500 />;
	}
	return (
		<div className="mt-2 flex w-full flex-1 flex-col items-start justify-center gap-4 overflow-auto p-2 md:gap-6">
			<div className="flex w-full flex-col gap-4">
				<div className="flex w-full items-center justify-between">
					<h3 className="font-semibold text-2xl">
						{t.servizio.dettaglio_servizio}
					</h3>

					<Link href="/area-riservata/dashboard">
						<Button size="sm" variant="outline">
							<ArrowRight className="size-4" />
							{t.servizio.torna_alla_dashboard}
						</Button>
					</Link>
				</div>
				<Separator />
			</div>
			<div className="flex w-full flex-1 grow flex-col space-y-5">
				<Servizio
					isAdmin={false}
					key={data.servizio_id}
					servizio={data}
					userId={user.id}
				/>
			</div>
			<div className="mx-auto">
				<UserDashboardFaq />
			</div>
		</div>
	);
};
export default ServizioPage;

export const getServerSideProps = (async (context) => {
	const servizio_id = context.params?.servizioId;

	if (typeof servizio_id !== "string") {
		console.error("Error: servizioId is not a string");
		return {
			redirect: {
				destination: "/500",
				permanent: false,
			},
		};
	}

	const parsedServizioId = zServizioId.safeParse(servizio_id);
	if (!parsedServizioId.success) {
		console.error("Error parsing servizioId", parsedServizioId.error);
		return {
			redirect: {
				destination: "/500",
				permanent: false,
			},
		};
	}
	const servizioId = parsedServizioId.data;

	const access_token = context.req.cookies.access_token;

	const helper = await TrpcAuthedFetchingIstance({ access_token });
	if (helper) {
		await helper.trpc.servizio.getServizio.prefetch({
			servizioId,
		});

		return {
			props: {
				servizioId,
				trpcState: helper.trpc.dehydrate(),
			},
		};
	}

	return {
		redirect: {
			destination: `/area-riservata/admin/utenti`,
			permanent: false,
		},
	};
}) satisfies GetServerSideProps<ServizioPageProps>;

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