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

/**
 * 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 fetchVolunteerOpportunityMeta(opportunityId);
  return localizedMetadata({
    title: metaTitle(record, "Volunteer opportunity", language),
    description: metaDescription(
      record,
      "Volunteer opportunity details, requirements and how to register.",
      language,
    ),
  });
}

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