import { CustomerPageShell } from "@/app/customer/_components/customer-page-shell";
import { fetchAccessUsers } from "@/app/customer/_lib/admin/access-server-api";
import { getCustomerSessionContext } from "@/lib/frontend-auth/server";
import { redirect } from "next/navigation";

import { UserCreateForm } from "./_components/user-create-form";

type PageProps = {
  params: Promise<{ tenant: string }>;
};

export default async function CustomerAdminCreateUserPage({ params }: Readonly<PageProps>) {
  const { tenant } = await params;
  const [result, session] = await Promise.all([
    fetchAccessUsers(tenant),
    getCustomerSessionContext(tenant),
  ]);

  if (!session.permissions.capabilities?.admin_users_manage) {
    redirect(`/customer/${tenant}/admin/users`);
  }

  return (
    <CustomerPageShell>
      <UserCreateForm tenant={tenant} groups={result.data?.groups ?? []} />
    </CustomerPageShell>
  );
}
