/**
 * Unsubscribe landing page, `/u/{token}` (task 3.6).
 *
 * The GET renders a confirmation and changes nothing; the opt-out itself is a POST carrying
 * `confirm: true`. Same reasoning as `/i/{token}/cancel`: a mail client or chat app that
 * prefetches the link must not be able to act on the guest's behalf.
 */
import type { Metadata } from "next";
import { notFound } from "next/navigation";

import { UnsubscribeConfirm } from "@/components/UnsubscribeConfirm";
import { fetchUnsubscribe } from "@/lib/api/server";
import { getDictionary } from "@/lib/i18n";
import type { Locale } from "@/lib/view-models";

export const dynamic = "force-dynamic";
export const revalidate = 0;

export const metadata: Metadata = {
  robots: { index: false, follow: false },
  referrer: "no-referrer",
};

export default async function UnsubscribePage({
  params,
  searchParams,
}: {
  params: Promise<{ token: string }>;
  searchParams: Promise<{ lang?: string }>;
}) {
  const { token } = await params;
  const { lang } = await searchParams;

  const data = await fetchUnsubscribe(token);
  if (!data) notFound();

  const locale: Locale = lang === "bn" ? "bn" : "en";
  const dict = getDictionary(locale);

  return (
    <main className="mx-auto flex min-h-dvh max-w-md flex-col justify-center gap-6 px-5 py-10">
      <UnsubscribeConfirm
        // An explicit view model, not the API object (design D8): only these four fields
        // are serialized into the page, and the guest's email is not among them.
        view={{
          token,
          guestName: data.guest_name,
          eventTitle: locale === "bn" ? data.event_title_bn : data.event_title_en,
          alreadyUnsubscribed: data.already_unsubscribed,
        }}
        dict={dict}
      />
    </main>
  );
}
