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 { searchParamsCache } from "@/lib/searchparams";
import type { SearchParams } from "nuqs/server";

export const metadata = {
  title: "Dashboard: CRM Enquiries",
};

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

export default async function EnquiriesRoute(props: PageProps) {
  const searchParams = 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,
        )));
  const businessRole = session?.user?.activeBusinessRole ?? null;

  searchParamsCache.parse(searchParams);

  const referenceData =
    canRead && session?.user?.activeOrganizationId
      ? await getEnquiryReferenceData(session.user.activeOrganizationId)
      : null;

  return (
    <PageContainer
      pageTitle="โอกาสขาย"
      pageDescription="พื้นที่ทำงานของฝ่ายขายสำหรับดูแลโอกาสขาย ติดตามความคืบหน้า และเตรียมใบเสนอราคา"
      access={canRead}
      accessFallback={
        <div className="text-muted-foreground rounded-lg border border-dashed p-8 text-center">
          You do not have access to CRM enquiries.
        </div>
      }
      pageHeaderAction={
        canCreate && referenceData ? (
          <EnquiryFormSheetTrigger referenceData={referenceData} workspace='enquiry' />
        ) : null
      }
    >
      {referenceData ? (
        <EnquiryListing
          referenceData={referenceData}
          workspace='enquiry'
          canUpdate={canUpdate}
          canDelete={canDelete}
          canAssign={canAssign}
          canReassign={canReassign}
        />
      ) : null}
    </PageContainer>
  );
}
