import { redirect } from "next/navigation";
import { AuthPageShell } from "@/components/auth-page-shell";
import { AuthTransitionLink } from "@/components/auth-transition";
import { FindIdForm } from "@/components/find-id-form";
import {
  getMailEntryUrlForCurrentRequest,
  getSessionUser,
  redirectToCanonicalMailAuthHostIfNeeded,
} from "@/lib/auth";
import { translate } from "@/lib/i18n-dictionaries";
import { resolveRequestLocale } from "@/lib/i18n-server";
import { toSearchString } from "@/lib/mail-urls";

type FindIdPageProps = {
  searchParams?: Promise<Record<string, string | string[] | undefined>>;
};

export default async function FindIdPage({ searchParams }: FindIdPageProps) {
  const params = searchParams ? await searchParams : {};
  await redirectToCanonicalMailAuthHostIfNeeded(
    "/find-id",
    toSearchString(params),
  );
  const user = await getSessionUser();
  const locale = await resolveRequestLocale();
  if (user) {
    redirect(await getMailEntryUrlForCurrentRequest(user));
  }

  return (
    <AuthPageShell
      title="auth.findId.title"
      description="auth.findId.description"
    >
      <FindIdForm />

      <div className="mt-6 text-center text-[13px] text-[#7f8791]">
        {translate(locale, "auth.findId.resetPrompt")}{" "}
        <AuthTransitionLink
          href="/reset-password"
          className="font-semibold text-[#2f6df6]"
        >
          {translate(locale, "auth.forgotPassword")}
        </AuthTransitionLink>
      </div>
    </AuthPageShell>
  );
}
