import { ArrowLeft } from "lucide-react";
import type { GetServerSideProps } from "next";
import { useRouter } from "next/router";
import { DocNotFoundPage } from "~/components/doc_not_found";
import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
import { FileSection } from "~/components/servizio/conferma";
import { Status500 } from "~/components/status-page";
import { Button } from "~/components/ui/button";
import type { NextPageWithLayout } from "~/pages/_app";
import { useTranslation } from "~/providers/I18nProvider";
import type { AnnunciId } from "~/schemas/public/Annunci";
import type { ServizioServizioId } from "~/schemas/public/Servizio";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zAnnuncioId, zServizioId } from "~/server/utils/zod_types";
import { api } from "~/utils/api";

type RiapriConfermaProps = {
	servizioId: ServizioServizioId;
	annuncioId: AnnunciId;
};

const RiapriConferma: NextPageWithLayout<RiapriConfermaProps> = ({
	servizioId,
	annuncioId,
}: RiapriConfermaProps) => {
	const { data, isLoading } = api.servizio.getServizioAnnuncio.useQuery({
		servizioId,
		annuncioId,
	});
	const router = useRouter();
	const { t } = useTranslation();

	if (isLoading) return <LoadingPage />;
	if (!data) return <Status500 />;
	if (!data.doc_conferma_ref) return <DocNotFoundPage />;
	return (
		<div className="mx-2 flex flex-1 overflow-auto">
			<div className="mx-auto flex grow sm:grow-0">
				<div className="flex flex-col items-center space-y-4 py-4">
					<h1 className="text-xl font-bold">{t.servizio.conferma.titolo}</h1>
					<p className="text-center">{t.servizio.conferma.descrizione}</p>

					<FileSection storageId={data.doc_conferma_ref} />

					<div className="flex max-w-3xl flex-col gap-2 rounded-md bg-green-400 p-4 text-left">
						<p className="text-xl font-semibold">{t.servizio.conferma.cta}</p>
						<p>{t.servizio.conferma.coordinate}</p>
						<p>
							{t.servizio.conferma.intestazione}{" "}
							{data.caparra_intestazione || ""}
						</p>
						<p>
							{t.servizio.conferma.iban} {data.caparra_iban || ""}
						</p>
						<p>
							{t.servizio.conferma.importo} {data.caparra_importo || ""}
						</p>
						<p>
							{t.servizio.conferma.causale} {data.caparra_causale || ""}
						</p>
						<p className="text-xl font-semibold">
							{t.servizio.conferma.coord_footer}
						</p>
					</div>

					<Button
						onClick={() => router.back()}
						variant="secondary"
						className="mx-auto mb-4 flex items-center gap-2"
					>
						<ArrowLeft className="size-5" />
						{t.indietro}
					</Button>
				</div>
			</div>
		</div>
	);
};

RiapriConferma.getLayout = function getLayout(page) {
	return (
		<AreaRiservataLayout noSidebar noFooter>
			{page}
		</AreaRiservataLayout>
	);
};

export default RiapriConferma;

export const getServerSideProps = (async (context) => {
	const slug = context.params?.slug;
	if (!slug || slug.length !== 2) {
		return {
			notFound: true,
		};
	}
	const [servizioId, annuncioId] = slug;
	if (!servizioId || !annuncioId) {
		return {
			notFound: true,
		};
	}

	const parsedServizioId = zServizioId.safeParse(servizioId);
	const parsedAnnuncioId = zAnnuncioId.safeParse(parseInt(annuncioId));

	if (!parsedServizioId.success || !parsedAnnuncioId.success) {
		return {
			notFound: true,
		};
	}

	const access_token = context.req.cookies.access_token;

	const helper = await TrpcAuthedFetchingIstance({ access_token });
	if (helper) {
		await helper.trpc.servizio.getServizioAnnuncio.prefetch({
			servizioId: parsedServizioId.data,
			annuncioId: parsedAnnuncioId.data,
		});

		return {
			props: {
				servizioId: parsedServizioId.data,
				annuncioId: parsedAnnuncioId.data,
				trpcState: helper.trpc.dehydrate(),
			},
		};
	}
	return {
		redirect: {
			destination: "/500",
			permanent: false,
		},
	};
}) satisfies GetServerSideProps<RiapriConfermaProps>;
