import { HydrationBoundary, dehydrate } from '@tanstack/react-query';
import { getQueryClient } from '@/lib/query-client';
import { searchParamsCache } from '@/lib/searchparams';
import {
  trainingMatricesQueryOptions,
  trainingMatrixComplianceQueryOptions
} from '../api/queries';
import { TrainingMatrixPageContent } from './training-matrix-page-content';

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

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

  const queryClient = getQueryClient();
  await Promise.all([
    queryClient.fetchQuery(trainingMatricesQueryOptions(filters)),
    queryClient.fetchQuery(trainingMatrixComplianceQueryOptions())
  ]);

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