import { Metadata, ResolvingMetadata } from "next";
import { notFound } from "next/navigation";
import { ContactIndexFragment } from "@/gql/graphql";
import { getClient } from "@/app/client";
import {
  ContactIndexView,
  contactQuery,
} from "@/components/contact/ContactIndexView";
import { getSeoMetadata } from "@/lib/seo";

export async function generateMetadata(
  { params }: { params: Promise<{}> },
  parent: ResolvingMetadata
): Promise<Metadata | null> {
  const { data, error } = await getClient().query(contactQuery, {});

  if (error) {
    throw new Error(error.message);
  }
  if (!data?.index) {
    return null;
  }

  const index = data.index as ContactIndexFragment;
  const metadata = await getSeoMetadata(index, parent);
  return metadata;
}

export default async function Page() {
  const { data, error } = await getClient().query(contactQuery, {});

  if (error) {
    throw new Error(error.message);
  }
  if (!data?.index) {
    return notFound();
  }

  const index = data.index as ContactIndexFragment;

  return <ContactIndexView index={index} />;
}
