import { HydrationBoundary, dehydrate } from '@tanstack/react-query';
import { getQueryClient } from '@/lib/query-client';
import { searchParamsCache } from '@/lib/searchparams';
import { employeesQueryOptions } from '../api/queries';
import { EmployeesTable } from './employee-tables';

export default async function EmployeeListingPage() {
  const page = searchParamsCache.get('page');
  const search = searchParamsCache.get('name');
  const pageLimit = searchParamsCache.get('perPage');
  const statuses = searchParamsCache.get('status');
  const sort = searchParamsCache.get('sort');

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

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

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