import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';
import { LanguageSwitcher } from '../../../components/language-switcher';
import { TwoFactorLoginCard } from '../../../components/two-factor-login-card';
import { twoFactorEmailCookieName } from '../../../lib/auth';
import { getDictionary, getLocale } from '../../../lib/i18n';

export default async function TwoFactorPage() {
  const locale = await getLocale();
  const dict = await getDictionary();
  const email = (await cookies()).get(twoFactorEmailCookieName)?.value;

  if (!email) {
    redirect('/login');
  }

  return (
    <TwoFactorLoginCard
      email={email}
      languageSwitcher={
        <LanguageSwitcher
          currentLocale={locale}
          label={dict.common.language}
          englishLabel={dict.common.english}
          indonesianLabel={dict.common.indonesian}
          submitLabel={dict.common.save}
          variant="compact"
        />
      }
    />
  );
}
