import { localizedMetadata, metadataLanguage } from "@/lib/i18n/serverMetadata";
import type { Metadata } from "next";
import {
  fetchEventMeta,
  metaDescription,
  metaTitle,
} from "@/features/shared/services/detailMetadata";
import EventDetails from "@/features/events/components/EventDetails";

/**
 * Record-specific title and description.
 *
 * The listing pages carry static metadata; a detail route is the one place
 * where the record's own title is what belongs in the tab and in a shared
 * link. Falls back to the generic strings when the read fails — the endpoint
 * is public, but a deleted or draft record still 404s, and metadata must never
 * break the render.
 */
export async function generateMetadata({
  params,
}: {
  params: Promise<{ eventId: string }>;
}): Promise<Metadata> {
  const language = await metadataLanguage();
  const { eventId } = await params;
  const record = await fetchEventMeta(eventId);
  return localizedMetadata({
    title: metaTitle(record, "Event", language),
    description: metaDescription(
      record,
      "Event details, dates, location and how to take part.",
      language,
    ),
  });
}

export default async function Page({
  params,
}: {
  params: Promise<{ eventId: string }>;
}) {
  const { eventId } = await params;

  return <EventDetails eventId={eventId} />;
}
