import { Suspense } from "react";
import { redirect } from "next/navigation";

import { CustomerPageShell } from "@/app/customer/_components/customer-page-shell";
import { hasAnySettingsAccess } from "@/app/customer/[tenant]/settings/_lib/settings-section-permissions";
import { getCustomerSessionContext } from "@/lib/frontend-auth/server";
import { customerUrl } from "@/lib/tenant";

import { SettingsView } from "./_components/settings-view";

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

function SettingsFallback() {
  return (
    <div className="flex min-h-48 items-center justify-center text-muted-foreground text-sm">Loading settings…</div>
  );
}

export default async function SettingsPage({ params }: Readonly<PageProps>) {
  const { tenant } = await params;
  const session = await getCustomerSessionContext(tenant);

  if (!hasAnySettingsAccess(session.permissions.routes)) {
    redirect(customerUrl(tenant, "/dashboard"));
  }

  return (
    <CustomerPageShell>
      <Suspense fallback={<SettingsFallback />}>
        <SettingsView allowedRoutes={session.permissions.routes} />
      </Suspense>
    </CustomerPageShell>
  );
}
