import { auth } from '@/auth';
import PageContainer from '@/components/layout/page-container';
import EnquiryListing from '@/features/crm/enquiries/components/enquiry-listing';
import { EnquiryFormSheetTrigger } from '@/features/crm/enquiries/components/enquiry-form-sheet';
import { getEnquiryReferenceData } from '@/features/crm/enquiries/server/service';
import { PERMISSIONS } from '@/lib/auth/rbac';
import type { SearchParams } from 'nuqs/server';

export const metadata = {
  title: 'Dashboard: CRM Opportunities'
};

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

export default async function EnquiriesRoute(props: PageProps) {
  await props.searchParams;
  const session = await auth();

  const canRead =
    session?.user?.systemRole === 'super_admin' ||
    (!!session?.user?.activeOrganizationId &&
      (session.user.activeMembershipRole === 'admin' ||
        session.user.activePermissions.includes(PERMISSIONS.crmEnquiryRead)));

  const canCreate =
    session?.user?.systemRole === 'super_admin' ||
    (!!session?.user?.activeOrganizationId &&
      (session.user.activeMembershipRole === 'admin' ||
        session.user.activePermissions.includes(PERMISSIONS.crmEnquiryCreate)));

  const canUpdate =
    session?.user?.systemRole === 'super_admin' ||
    (!!session?.user?.activeOrganizationId &&
      (session.user.activeMembershipRole === 'admin' ||
        session.user.activePermissions.includes(PERMISSIONS.crmEnquiryUpdate)));

  const canDelete =
    session?.user?.systemRole === 'super_admin' ||
    (!!session?.user?.activeOrganizationId &&
      (session.user.activeMembershipRole === 'admin' ||
        session.user.activePermissions.includes(PERMISSIONS.crmEnquiryDelete)));

  const canAssign =
    session?.user?.systemRole === 'super_admin' ||
    (!!session?.user?.activeOrganizationId &&
      (session.user.activeMembershipRole === 'admin' ||
        session.user.activePermissions.includes(PERMISSIONS.crmEnquiryAssign)));

  const canReassign =
    session?.user?.systemRole === 'super_admin' ||
    (!!session?.user?.activeOrganizationId &&
      (session.user.activeMembershipRole === 'admin' ||
        session.user.activePermissions.includes(PERMISSIONS.crmEnquiryReassign)));

  if (!canRead || !session?.user?.activeOrganizationId) {
    return (
      <PageContainer access={false} pageTitle='CRM Opportunities'>
        {null}
      </PageContainer>
    );
  }

  const referenceData = await getEnquiryReferenceData(session.user.activeOrganizationId);

  return (
    <PageContainer
      pageTitle='CRM Opportunities'
      pageDescription='Sales execution workspace for opportunities created directly or handed off from marketing leads.'
      pageHeaderAction={canCreate ? <EnquiryFormSheetTrigger referenceData={referenceData} /> : null}
    >
      <EnquiryListing
        referenceData={referenceData}
        workspace='enquiry'
        canUpdate={canUpdate}
        canDelete={canDelete}
        canAssign={canAssign}
        canReassign={canReassign}
      />
    </PageContainer>
  );
}
