import { auth } from "@/auth";
import PageContainer from "@/components/layout/page-container";
import OpportunityListing from "@/features/crm/opportunities/components/opportunity-listing";
import { OpportunityFormSheetTrigger } from "@/features/crm/opportunities/components/opportunity-form-sheet";
import { getOpportunityReferenceData } from "@/features/crm/opportunities/server/service";
import { PERMISSIONS } from "@/lib/auth/rbac";
import type { SearchParams } from "nuqs/server";
import { searchParamsCache } from "@/lib/searchparams";

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

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

export default async function OpportunitiesRoute(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.crmOpportunityRead)));

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

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

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

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

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

  if (!canRead || !session?.user?.activeOrganizationId) {
    return (
      <PageContainer access={false} pageTitle="CRM Opportunities">
        {null}
      </PageContainer>
    );
  }
  searchParamsCache.parse(searchParams);
  const referenceData = await getOpportunityReferenceData(
    session.user.activeOrganizationId,
  );

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

