import Link from "next/link";
import { redirect } from "next/navigation";

import { auth } from "@/auth";
import { getPostLoginRedirect } from "@/lib/post-login-redirect";
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from "@/components/ui/card";

import { LoginForm } from "./login-form";

export const metadata = { title: "Anmelden · Kita-Planer" };

export default async function LoginPage() {
  const session = await auth();
  if (session?.user?.id) {
    redirect(getPostLoginRedirect(session.user));
  }

  return (
    <div className="flex min-h-screen items-center justify-center bg-muted/30 px-4 py-12">
      <Card className="w-full max-w-md">
        <CardHeader className="space-y-2">
          <CardTitle>Anmelden</CardTitle>
          <CardDescription>Willkommen zurück.</CardDescription>
        </CardHeader>
        <CardContent className="space-y-4">
          <LoginForm />
          <p className="text-center text-sm text-muted-foreground">
            Noch keinen Account?{" "}
            <Link href="/register" className="font-medium text-foreground underline">
              Kita registrieren
            </Link>
          </p>
        </CardContent>
      </Card>
    </div>
  );
}
