import { auth } from '@/auth';
import { HydrationBoundary, dehydrate } from '@tanstack/react-query';
import PageContainer from '@/components/layout/page-container';
import { ApprovalDetail } from '@/features/foundation/approval/components/approval-detail';
import { approvalByIdOptions } from '@/features/foundation/approval/queries';
import { PERMISSIONS } from '@/lib/auth/rbac';
import { getQueryClient } from '@/lib/query-client';

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

export default async function ApprovalDetailRoute({ params }: PageProps) {
  const { id } = await params;
  const session = await auth();
  const canRead =
    session?.user?.systemRole === 'super_admin' ||
    (!!session?.user?.activeOrganizationId &&
      (session.user.activeMembershipRole === 'admin' ||
        session.user.activePermissions.includes(PERMISSIONS.crmApprovalRead)));
  const canApprove =
    session?.user?.systemRole === 'super_admin' ||
    (!!session?.user?.activeOrganizationId &&
      (session.user.activeMembershipRole === 'admin' ||
        session.user.activePermissions.includes(PERMISSIONS.crmApprovalApprove)));
  const canReject =
    session?.user?.systemRole === 'super_admin' ||
    (!!session?.user?.activeOrganizationId &&
      (session.user.activeMembershipRole === 'admin' ||
        session.user.activePermissions.includes(PERMISSIONS.crmApprovalReject)));
  const canReturn =
    session?.user?.systemRole === 'super_admin' ||
    (!!session?.user?.activeOrganizationId &&
      (session.user.activeMembershipRole === 'admin' ||
        session.user.activePermissions.includes(PERMISSIONS.crmApprovalReturn)));
  const canCancel =
    session?.user?.systemRole === 'super_admin' ||
    (!!session?.user?.activeOrganizationId &&
      (session.user.activeMembershipRole === 'admin' ||
        session.user.activePermissions.includes(PERMISSIONS.crmApprovalSubmit)));
  const isOrgAdmin =
    session?.user?.systemRole === 'super_admin' || session?.user?.activeMembershipRole === 'admin';
  const queryClient = getQueryClient();

  if (canRead) {
    void queryClient.prefetchQuery(approvalByIdOptions(id));
  }

  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 this approval request.
        </div>
      }
    >
      {canRead && session?.user ? (
        <HydrationBoundary state={dehydrate(queryClient)}>
          <ApprovalDetail
            approvalId={id}
            canApprove={canApprove}
            canReject={canReject}
            canReturn={canReturn}
            canCancel={canCancel}
            activeBusinessRole={session.user.activeBusinessRole}
            isOrgAdmin={isOrgAdmin}
            currentUserId={session.user.id}
          />
        </HydrationBoundary>
      ) : null}
    </PageContainer>
  );
}
