import { Metadata, ResolvingMetadata } from "next";
import { getClient } from "@/app/client";
import {
  EventIndexView,
  loadEventIndexProps,
} from "@/components/events/EventIndexView";
import { EventIndexFragment } from "@/gql/graphql";
import { eventIndexMetadataQuery } from "@/lib/event";
import { getSeoMetadata } from "@/lib/seo";

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

export default async function Page({
  searchParams,
}: {
  searchParams?: Promise<Record<string, string | string[] | undefined>>;
}) {
  await searchParams;
  const props = await loadEventIndexProps();
  return <EventIndexView {...props} />;
}
