import { auth } from '@/auth';
import PageContainer from '@/components/layout/page-container';
import MasterOptionsListingPage from '@/features/foundation/master-options/components/master-options-listing';
import { PERMISSIONS } from '@/lib/auth/rbac';
import { searchParamsCache } from '@/lib/searchparams';
import type { SearchParams } from 'nuqs/server';

type PageProps = {
  searchParams: Promise<SearchParams>;
};

export default async function MasterOptionsRoute(props: PageProps) {
  const searchParams = await props.searchParams;
  const session = await auth();
  const canManageOptions =
    session?.user?.systemRole === 'super_admin' ||
    (!!session?.user?.activeOrganizationId &&
      (session.user.activeMembershipRole === 'admin' ||
        session.user.activePermissions.includes(PERMISSIONS.organizationManage)));

  searchParamsCache.parse(searchParams);

  return (
    <PageContainer
      pageTitle='Master Options'
      pageDescription='Organization-scoped CRM option registry for statuses, product types, payment terms, currency, and branch abstractions.'
      access={canManageOptions}
      accessFallback={
        <div className='text-muted-foreground rounded-lg border border-dashed p-8 text-center'>
          You do not have access to master option management.
        </div>
      }
    >
      <MasterOptionsListingPage />
    </PageContainer>
  );
}
