import Link from 'next/link';
import { redirect } from 'next/navigation';
import { currentClient } from '@/lib/session';
import { AuthCard } from '@/components/AuthCard';
import { LoginForm } from '@/components/AccountForms';

export const metadata = { title: 'Sign in' };

export default async function Login({ searchParams }: { searchParams: Promise<{ next?: string; expired?: string }> }) {
  const { next: raw, expired } = await searchParams;
  const next = raw && raw.startsWith('/') && !raw.startsWith('//') ? raw : '/account/';
  if (await currentClient()) redirect(next);
  return (
    <AuthCard
      title="Sign in"
      intro={expired ? 'Your session ended — sign in again.' : 'Follow your enquiries, keep a shortlist of lawyers and get alerts for new matches.'}
      footer={
        <>
          <Link href="/account/forgot-password/" className="link">
            Forgot your password?
          </Link>{' '}
          · No account?{' '}
          <Link href={`/account/register/`} className="link">
            Create one free
          </Link>
        </>
      }
    >
      <LoginForm next={next} />
    </AuthCard>
  );
}
