import { HydrationBoundary, dehydrate } from '@tanstack/react-query';
import { getQueryClient } from '@/lib/query-client';
import { searchParamsCache } from '@/lib/searchparams';
import { onlineLessonsQueryOptions } from '../api/queries';
import type { OnlineLessonCategory, OnlineLessonStatus } from '../api/types';
import OnlineLessonsManagePage from './online-lessons-manage-page';

export default async function OnlineLessonsManageListingPage({
  currentUserId,
  permissions
}: {
  currentUserId: string;
  permissions: readonly string[];
}) {
  const page = searchParamsCache.get('page');
  const perPage = searchParamsCache.get('perPage');
  const search = searchParamsCache.get('search');
  const category = searchParamsCache.get('category');
  const status = searchParamsCache.get('status');

  const filters = {
    page,
    limit: perPage,
    ...(search && { search }),
    ...(category ? { category: category as OnlineLessonCategory } : {}),
    ...(status && { status: status as OnlineLessonStatus })
  };

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

  return (
    <HydrationBoundary state={dehydrate(queryClient)}>
      <OnlineLessonsManagePage currentUserId={currentUserId} permissions={permissions} />
    </HydrationBoundary>
  );
}
