import { HydrationBoundary, dehydrate } from '@tanstack/react-query';
import { getQueryClient } from '@/lib/query-client';
import { searchParamsCache } from '@/lib/searchparams';
import { masterOptionsQueryOptions } from '../api/queries';
import { MasterOptionsTable } from './master-options-table';

export default function MasterOptionsListingPage() {
  const page = searchParamsCache.get('page');
  const search = searchParamsCache.get('name');
  const pageLimit = searchParamsCache.get('perPage');
  const category = searchParamsCache.get('category');
  const sort = searchParamsCache.get('sort');

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

  const queryClient = getQueryClient();

  void queryClient.prefetchQuery(masterOptionsQueryOptions(filters));

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