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/privacy")({
	loader: async () => {
		const page = await findPageBySlug("legal/privacy");
		if (!page) throw notFound();
		return { page };
	},
	component: PrivacyPage,
	head: ({ loaderData }) => ({
		meta: [
			{
				title: `unom - ${loaderData?.page?.meta?.title ?? loaderData?.page?.title ?? "Datenschutzerklärung"}`,
			},
			...(loaderData?.page?.description
				? [{ name: "description", content: loaderData.page.description }]
				: []),
		],
	}),
});

function PrivacyPage() {
	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>
	);
}
