import { cookies } from "next/headers";
import {
  dehydrate,
  HydrationBoundary,
  QueryClient,
} from "@tanstack/react-query";
import { isAuthenticatedToken } from "@/lib/entry-route-cache";
import { fetchProfileSSR } from "@/lib/ssr-fetch";
import { marriageQueryKeys } from "@/hooks/marriage/query-keys";
import NewMatchClient from "./new-match-client";

export const dynamic = "force-dynamic";

export default async function NewMatchPage() {
  const cookieStore = await cookies();

  const token =
    cookieStore.get("HABIB_TOKEN")?.value ??
    cookieStore.get("habib_token")?.value;

  const queryClient = new QueryClient({
    defaultOptions: {
      queries: { staleTime: 10 * 1000 },
    },
  });

  if (isAuthenticatedToken(token)) {
    await queryClient.prefetchQuery({
      queryKey: marriageQueryKeys.profile(),
      queryFn: () => fetchProfileSSR(token!),
    });
  }

  return (
    <HydrationBoundary state={dehydrate(queryClient)}>
      <NewMatchClient />
    </HydrationBoundary>
  );
}
