import { dehydrate, HydrationBoundary } from "@tanstack/react-query";
import { getQueryClient } from "@/lib/query-client";
import { searchParamsCache } from "@/lib/searchparams";
import { trainingRecordsQueryOptions } from "../api/queries";
import { PendingReviewTable } from "./pending-review-table";

export default async function PendingReviewListingPage() {
  const page = searchParamsCache.get("page");
  const pageLimit = searchParamsCache.get("perPage");
  const search = searchParamsCache.get("search");
  const year = searchParamsCache.get("year");
  const company = searchParamsCache.get("company");
  const department = searchParamsCache.get("department");
  const sort = searchParamsCache.get("sort");

  const filters = {
    page,
    limit: pageLimit,
    ...(search && { search }),
    statuses: "pending",
    ...(year && { year }),
    ...(company && { company }),
    ...(department && { department }),
    ...(sort && { sort }),
  };

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

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