import { CustomerPageShell } from "@/app/customer/_components/customer-page-shell";

import { fetchAccessGroups, fetchAccessRouteCatalog } from "@/app/customer/_lib/admin/access-server-api";
import { getCustomerSessionContext } from "@/lib/frontend-auth/server";
import { GroupsPageClient } from "./_components/groups-page-client";

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

export default async function CustomerAdminGroupsPage({ params }: Readonly<PageProps>) {
  const { tenant } = await params;
  const [groupsResult, catalogResult, session] = await Promise.all([
    fetchAccessGroups(tenant),
    fetchAccessRouteCatalog(tenant),
    getCustomerSessionContext(tenant),
  ]);

  return (
    <CustomerPageShell>
      <GroupsPageClient
        tenant={tenant}
        initialGroups={groupsResult.items}
        appCatalog={catalogResult.apps}
        initialErrorMessage={groupsResult.errorMessage ?? catalogResult.errorMessage}
        canManageRoles={Boolean(session.permissions.capabilities?.admin_roles_manage)}
      />
    </CustomerPageShell>
  );
}
