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

export const metadata = {
  title: 'Dashboard: New Asset'
};

export default async function NewAssetPage() {
  const session = await auth();
  const hasActiveOrganization = !!session?.user?.activeOrganizationId;
  const canCreate =
    session?.user?.systemRole === 'super_admin' ||
    session?.user?.activePermissions.includes('asset:create');
  const queryClient = getQueryClient();

  if (canCreate && hasActiveOrganization) {
    void queryClient.prefetchQuery(assetOptionsQueryOptions());
  }

  return (
    <PageContainer
      pageTitle='Create Asset'
      pageDescription='Register a new IT asset.'
      access={!!canCreate && hasActiveOrganization}
      accessFallback={
        <div className='text-muted-foreground rounded-lg border border-dashed p-8 text-center'>
          Select an active workspace with asset-create access before creating assets.
        </div>
      }
    >
      <HydrationBoundary state={dehydrate(queryClient)}>
        <AssetForm initialData={null} pageTitle='Create Asset' />
      </HydrationBoundary>
    </PageContainer>
  );
}
