import React from "react";
import { HydrationBoundary, dehydrate } from "@tanstack/react-query";
import { DashboardLayout } from "@/components/layout/dashboard";
import { PaywallBypassTokenList } from "@/components/dashboard/paywall-bypass-token/paywall-bypass-token-list";
import { createHelpers } from "@/lib/trpc/server";
import { nuqsTransformServerSchema } from "@/lib/utils/nuqs-transform-server-schema";
import { PaywallBypassTokenListQuerySchema } from "@/server/modules/paywall-bypass/paywall-bypass-token.validators";

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

export default async function Page({ searchParams }: Props) {
    const search = nuqsTransformServerSchema(PaywallBypassTokenListQuerySchema).parse(await searchParams);
    const helpers = await createHelpers();

    await Promise.all([
        helpers.paywallBypassToken.count.prefetch({
            search: search.search || undefined,
            postId: search.postId || undefined
        }),
        helpers.paywallBypassToken.list.prefetch({
            with: ["post"],
            search: search.search || undefined,
            postId: search.postId || undefined,
            offset: search.page * search.perPage,
            limit: search.perPage,
            orderBy: search.orderBy,
            orderDir: search.orderDir
        })
    ]);

    return (
        <HydrationBoundary state={dehydrate(helpers.queryClient)}>
            <DashboardLayout title="Paywall Bypass Post Tokens">
                <PaywallBypassTokenList />
            </DashboardLayout>
        </HydrationBoundary>
    );
}
