import { redirect } from 'next/navigation';
import { auth } from '@/auth';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { loginAction } from './actions';

export default async function LoginPage({
  searchParams,
}: {
  searchParams: Promise<{ error?: string }>;
}) {
  const session = await auth();
  const params = await searchParams;

  if (session?.user?.id) {
    redirect('/');
  }

  return (
    <main className="grid min-h-screen place-items-center p-6">
      <Card className="w-full max-w-[420px]">
        <CardHeader>
          <p className="text-xs uppercase text-[hsl(var(--muted-foreground))]">EKB Auth</p>
          <CardTitle className="text-3xl">登录</CardTitle>
        </CardHeader>
        <CardContent>
          <form className="grid gap-4" action={loginAction}>
            <Label>
              邮箱
              <Input name="email" type="email" defaultValue="admin@ekb.com" required />
            </Label>
            <Label>
              密码
              <Input name="password" type="password" defaultValue="hashed_password_here" required />
            </Label>
            {params.error ? (
              <div className="rounded-md border border-red-300 bg-red-50 p-3 text-sm text-red-800">
                邮箱或密码不正确
              </div>
            ) : null}
            <Button type="submit">登录</Button>
          </form>
        </CardContent>
      </Card>
    </main>
  );
}
