import { HydrationBoundary, dehydrate } from '@tanstack/react-query';
import { auth } from '@/auth';
import PageContainer from '@/components/layout/page-container';
import { assetByIdQueryOptions, assetOptionsQueryOptions } from '@/features/assets/api/queries';
import { AssetActionForm } from '@/features/assets/components/asset-action-form';
import { getQueryClient } from '@/lib/query-client';

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

export default async function AssetAssignPage({ params }: PageProps) {
  const { assetId } = await params;
  const session = await auth();
  const canAssign =
    session?.user?.systemRole === 'super_admin' ||
    session?.user?.activePermissions.includes('asset:assign');

  const queryClient = getQueryClient();
  void queryClient.prefetchQuery(assetByIdQueryOptions(assetId));
  void queryClient.prefetchQuery(assetOptionsQueryOptions());

  return (
    <PageContainer pageTitle='Asset Assignment' pageDescription='Assign this asset to an employee.' access={!!canAssign}>
      <HydrationBoundary state={dehydrate(queryClient)}>
        <AssetActionForm action='assign' assetId={assetId} />
      </HydrationBoundary>
    </PageContainer>
  );
}
