import { createFileRoute, notFound } from "@tanstack/react-router";
import LegalPage from "@/components/LegalPage";
import RichText from "@/components/RichText";
import { findPageBySlug, type RichTextBlock } from "@/lib/cms";

export const Route = createFileRoute("/legal/imprint")({
	loader: async () => {
		const page = await findPageBySlug("legal/imprint");
		if (!page) throw notFound();
		return { page };
	},
	component: ImprintPage,
	head: ({ loaderData }) => ({
		meta: [
			{
				title: `unom - ${loaderData?.page?.meta?.title ?? loaderData?.page?.title ?? "Impressum"}`,
			},
			...(loaderData?.page?.description
				? [{ name: "description", content: loaderData.page.description }]
				: []),
		],
	}),
});

function ImprintPage() {
	const { page } = Route.useLoaderData();
	return (
		<LegalPage>
			{page.blocks?.map((block, i) => {
				if (block.blockType === "RichText") {
					return <RichText key={i} data={(block as RichTextBlock).body} />;
				}
				return null;
			})}
		</LegalPage>
	);
}
