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

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

export default async function Page({ searchParams }: Props) {
    const params = nuqsTransformServerSchema(ProfileListQuerySchema).parse(await searchParams);
    const helpers = await createHelpers();
    const t = await getTranslations();

    await Promise.all([
        helpers.profile.count.prefetch({
            search: params.search || undefined,
            isPersonal: params.isPersonal || false
        }),
        helpers.profile.list.prefetch({
            with: ["user"],
            search: params.search || undefined,
            isPersonal: params.isPersonal || false,
            offset: params.page * params.perPage,
            limit: params.perPage,
            orderBy: params.orderBy,
            orderDir: params.orderDir
        })
    ]);

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

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