import { CustomerPageShell } from "@/app/customer/_components/customer-page-shell";
import { fetchPortalParentBanks } from "@/app/customer/_lib/admin/parent-banks-server-api";
import {
  EMPTY_CUSTOMER_PORTAL_CAPABILITIES,
  getCustomerSessionContext,
} from "@/lib/frontend-auth/server";

import { ParentBanksPageClient } from "./_components/parent-banks-page-client";

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

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

  const capabilities = session.permissions.capabilities ?? EMPTY_CUSTOMER_PORTAL_CAPABILITIES;
  const canManage =
    capabilities.admin_link_bank_manage ||
    session.permissions.routes.includes("access/link_bank_accounts_manage") ||
    session.permissions.routes.includes("access/parent_banks_manage");

  return (
    <CustomerPageShell>
      <ParentBanksPageClient
        tenant={tenant}
        initialItems={result.items}
        initialStatusOptions={result.statusOptions}
        initialErrorMessage={result.errorMessage}
        catalogAvailable={result.available}
        canManage={canManage}
      />
    </CustomerPageShell>
  );
}
