import { HydrationBoundary, dehydrate } from '@tanstack/react-query';
import { getQueryClient } from '@/lib/query-client';
import { searchParamsCache } from '@/lib/searchparams';
import { permissionAuditLogsQueryOptions } from '../api/queries';
import { PermissionAuditLogTable } from './permission-audit-log-table';

export default async function PermissionAuditLogListing() {
  const page = searchParamsCache.get('page');
  const perPage = searchParamsCache.get('perPage');
  const search = searchParamsCache.get('search');
  const user = searchParamsCache.get('user');
  const action = searchParamsCache.get('action');
  const dateFrom = searchParamsCache.get('dateFrom');
  const dateTo = searchParamsCache.get('dateTo');
  const sort = searchParamsCache.get('sort');

  const filters = {
    page,
    limit: perPage,
    ...(search && { search }),
    ...(user && { user }),
    ...(action && { action }),
    ...(dateFrom && { dateFrom }),
    ...(dateTo && { dateTo }),
    ...(sort && { sort })
  };

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

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