import { getTranslations } from "next-intl/server";
import { HydrationBoundary, dehydrate } from "@tanstack/react-query";
import { BlockList } from "@/components/dashboard/block/block-list";
import { DashboardLayout, DashboardBreadcrump } from "@/components/layout/dashboard";
import { BlockListQuerySchema } from "@/server/modules/block/block.validators";
import { createHelpers } from "@/lib/trpc/server";
import { nuqsTransformServerSchema } from "@/lib/utils/nuqs-transform-server-schema";

type Props = {
    searchParams: Promise<Record<string, string | string[] | undefined>>;
};

export default async function Page({ searchParams }: Props) {
    const search = nuqsTransformServerSchema(BlockListQuerySchema).parse(await searchParams);
    const helpers = await createHelpers();
    const t = await getTranslations("dashboard.block.list");

    await Promise.all([
        helpers.block.count.prefetch({
            search: search.search || undefined,
            scope: search.scope || undefined
        }),
        helpers.block.list.prefetch({
            search: search.search || undefined,
            scope: search.scope || undefined,
            offset: search.page * search.perPage,
            limit: search.perPage,
            orderBy: search.orderBy,
            orderDir: search.orderDir
        })
    ]);

    return (
        <HydrationBoundary state={dehydrate(helpers.queryClient)}>
            <DashboardLayout
              title={t("title")}
              description={t("description")}>
                <DashboardBreadcrump>
                    <DashboardBreadcrump.Item active>
                        {t("title")}
                    </DashboardBreadcrump.Item>
                </DashboardBreadcrump>

                <BlockList />
            </DashboardLayout>
        </HydrationBoundary>
    );
}
