import { publicApi } from '@/lib/api';
import { AuthShell, InvalidLink } from '@/components/AuthShell';
import { ResetPasswordForm } from '@/components/AccountForms';

export const metadata = { title: 'Choose a new password' };
export const dynamic = 'force-dynamic';

export default async function ResetPassword({ searchParams }: { searchParams: Promise<{ token?: string }> }) {
  const { token = '' } = await searchParams;
  const check = token ? (await publicApi<{ valid: boolean; otpRequired?: boolean }>('/auth/password/reset/check', { token })).data : { valid: false, otpRequired: false };
  const valid = check.valid;
  return (
    <AuthShell title="Choose a new password" description={valid ? 'After this, every device signed in to your account will be signed out.' : undefined}>
      {valid ? <ResetPasswordForm token={token} otpRequired={check.otpRequired} /> : <InvalidLink what="password reset" retry={{ href: '/forgot-password', label: 'Request a new link' }} />}
    </AuthShell>
  );
}
