import { HydrationBoundary, dehydrate } from '@tanstack/react-query';
import { getQueryClient } from '@/lib/query-client';
import { searchParamsCache } from '@/lib/searchparams';
import { permissionTemplatesQueryOptions } from '../api/queries';
import { PermissionTemplateTable } from './permission-template-table';

export default async function PermissionTemplateListing() {
  const page = searchParamsCache.get('page');
  const perPage = searchParamsCache.get('perPage');
  const search = searchParamsCache.get('search');
  const status = searchParamsCache.get('status');
  const sort = searchParamsCache.get('sort');

  const filters = {
    page,
    limit: perPage,
    ...(search && { search }),
    ...(status && { status }),
    ...(sort && { sort })
  };

  const queryClient = getQueryClient();
  await queryClient.fetchQuery(permissionTemplatesQueryOptions(filters));

  return (
    <HydrationBoundary state={dehydrate(queryClient)}>
      <PermissionTemplateTable />
    </HydrationBoundary>
  );
}
