import { i18n } from '@/lib/i18n';
import { docsRoute } from '@/lib/shared';
import { LoginForm } from './login-form';

export default async function LoginPage(props: PageProps<'/[lang]/login'>) {
  const { lang } = await props.params;
  const { next } = await props.searchParams;

  const prefix = lang === i18n.defaultLanguage ? '' : `/${lang}`;

  /*
    `next.startsWith('/')` ferme une redirection ouverte : sans ce test,
    `?next=https://ailleurs.example` enverrait l'utilisateur hors du site une
    fois connecté, avec l'autorité d'un lien que nous avons nous-mêmes envoyé.
  */
  const callbackURL =
    typeof next === 'string' && next.startsWith('/') ? next : `${prefix}${docsRoute}`;

  return (
    <main className="flex flex-1 flex-col justify-center px-4">
      <div className="mx-auto w-full max-w-sm">
        <h1 className="mb-2 text-xl font-bold">ADVMFR Knowledge Base</h1>
        <p className="mb-6 text-sm text-fd-muted-foreground">
          Access is restricted. Enter your address to receive a sign-in link.
        </p>
        <LoginForm callbackURL={callbackURL} />
      </div>
    </main>
  );
}
