import { createFileRoute, redirect } from "@tanstack/react-router";
import { CreateSsoProviderPage } from "~/client/modules/sso/routes/create-sso-provider";
import { getOrganizationContext } from "~/server/lib/functions/organization-context";

export const Route = createFileRoute("/(dashboard)/settings/sso/new")({
	component: RouteComponent,
	errorComponent: () => <div>Failed to load SSO registration</div>,
	loader: async () => {
		const orgContext = await getOrganizationContext();
		const role = orgContext.activeMember?.role;

		if (role !== "owner") {
			throw redirect({ to: "/settings" });
		}
	},
	staticData: {
		breadcrumb: () => [{ label: "Settings", href: "/settings" }, { label: "Register SSO Provider" }],
	},
	head: () => ({
		meta: [
			{ title: "Zerobyte - Register SSO Provider" },
			{
				name: "description",
				content: "Register a new OIDC identity provider for organization sign-in.",
			},
		],
	}),
});

function RouteComponent() {
	return <CreateSsoProviderPage />;
}
