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

/**
 * 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<{ opportunityId: string }>;
}): Promise<Metadata> {
  const language = await metadataLanguage();
  const { opportunityId } = await params;
  const record = await fetchLearnServeOpportunityMeta(opportunityId);
  return localizedMetadata({
    title: metaTitle(record, "Learn and serve", language),
    description: metaDescription(
      record,
      "Workshop, course, internship or consultation details and how to join.",
      language,
    ),
  });
}

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