import type { Metadata } from "next";
import { AuthLayout } from "@/components/auth/AuthLayout";
import {
  VerificationResult,
  type VerificationStatus,
} from "@/components/auth/VerificationResult";

export const metadata: Metadata = {
  title: "Email confirmation | ScanReceipts",
  robots: { index: false, follow: false },
};

const KNOWN_STATUSES: VerificationStatus[] = [
  "success",
  "already_verified",
  "expired",
  "invalid",
  "unavailable",
  "error",
];

function parseStatus(value: string | string[] | undefined): VerificationStatus {
  const candidate = Array.isArray(value) ? value[0] : value;
  return KNOWN_STATUSES.includes(candidate as VerificationStatus)
    ? (candidate as VerificationStatus)
    : "invalid";
}

/**
 * Where `/api/auth/verify` lands the user after consuming the token. Reading the
 * status on the server keeps the page free of a Suspense boundary.
 */
export default async function VerifiedPage({
  searchParams,
}: {
  searchParams: Promise<{ status?: string | string[] }>;
}) {
  const { status } = await searchParams;

  return (
    <AuthLayout>
      <VerificationResult status={parseStatus(status)} />
    </AuthLayout>
  );
}
