import { prisma } from "@streamclipper/db";
import { bootstrapAdmin, login } from "./actions";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card";

export const dynamic = "force-dynamic";

export default async function LoginPage({ searchParams }: { searchParams: Promise<{ error?: string }> }) {
  const { error } = await searchParams;
  const userCount = await prisma.user.count();
  const isBootstrap = userCount === 0;

  return (
    <div className="flex min-h-[60vh] items-center justify-center">
      <Card className="w-full max-w-sm">
        <CardHeader>
          <CardTitle className="text-xl">{isBootstrap ? "İlk Yönetici Hesabını Oluştur" : "Giriş Yap"}</CardTitle>
          {isBootstrap && (
            <CardDescription>Henüz kullanıcı yok — panele erişecek ilk admin hesabını burada oluştur.</CardDescription>
          )}
        </CardHeader>
        <CardContent>
          {!isBootstrap && error && <p className="mb-3 text-sm text-destructive">Kullanıcı adı veya şifre hatalı.</p>}
          <form action={isBootstrap ? bootstrapAdmin : login} className="flex flex-col gap-3">
            <Input name="username" placeholder="Kullanıcı adı" required />
            <Input name="password" type="password" placeholder="Şifre" required minLength={isBootstrap ? 8 : undefined} />
            <Button type="submit">{isBootstrap ? "Hesabı Oluştur" : "Giriş Yap"}</Button>
          </form>
        </CardContent>
      </Card>
    </div>
  );
}
