import { Metadata, ResolvingMetadata } from "next";
import { getClient } from "@/app/client";
import {
  NewsIndexView,
  loadNewsIndexProps,
} from "@/components/news/NewsIndexView";
import { NewsIndexFragment } from "@/gql/graphql";
import { newsIndexMetadataQuery } from "@/lib/news";
import { getSeoMetadata } from "@/lib/seo";

export async function generateMetadata(
  _: unknown,
  parent: ResolvingMetadata
): Promise<Metadata | null> {
  const { data, error } = await getClient().query(newsIndexMetadataQuery, {});
  if (error) throw new Error(error.message);
  if (!data?.index) return null;
  return getSeoMetadata(data.index as NewsIndexFragment, parent);
}

export default async function Page() {
  const props = await loadNewsIndexProps();
  return <NewsIndexView {...props} />;
}
