import { CustomerPageShell } from "@/app/customer/_components/customer-page-shell";
import { fetchAccessUsers } from "@/app/customer/_lib/admin/access-server-api";
import { loadAdminPageContext } from "@/app/customer/_lib/admin/load-admin-page-context";
import {
  canAssignRoles,
  managePageKeysForSection,
} from "@/app/customer/_lib/admin/admin-section-permissions";

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 ctx = await loadAdminPageContext(tenant, {
    page: managePageKeysForSection("users"),
    label: "Create User",
  });
  if (ctx.denied) {
    return ctx.deniedView;
  }

  const result = await fetchAccessUsers(tenant);

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