import React from "react";
import { HydrationBoundary, dehydrate } from "@tanstack/react-query";
import { DashboardLayout } from "@/components/layout/dashboard";
import { PlanList } from "@/components/dashboard/payment/plan-list";
import { createHelpers } from "@/lib/trpc/server";
import { nuqsTransformServerSchema } from "@/lib/utils/nuqs-transform-server-schema";
import { PlanListQuerySchema } from "@/server/modules/payment/payment.validators";

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

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

    await Promise.all([
        helpers.payment.plan.count.prefetch({
            access: "dashboard",
            type: search.type || undefined
        }),
        helpers.payment.plan.list.prefetch({
            access: "dashboard",
            type: search.type || undefined,
            offset: search.page * search.perPage,
            limit: search.perPage,
            orderBy: search.orderBy,
            orderDir: search.orderDir
        })
    ]);

    return (
        <HydrationBoundary state={dehydrate(helpers.queryClient)}>
            <DashboardLayout
              title="Payment plans"
              description="You can currently edit only the plan description here. Pricing is managed separately in the Stripe dashboard.">
                <PlanList />
            </DashboardLayout>
        </HydrationBoundary>
    );
}
