'use client'

import { useActionState } from 'react'
import Link from 'next/link'
import { register, type AuthFormState } from '../actions'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'
import { AlertTriangle, Loader2 } from 'lucide-react'

export default function RegisterPage() {
  const [state, formAction, pending] = useActionState<AuthFormState, FormData>(
    register,
    {}
  )

  return (
    <div className="flex min-h-[calc(100vh-4rem)] items-center justify-center px-4 py-12">
      <Card className="w-full max-w-sm">
        <CardHeader className="text-center">
          <CardTitle className="text-2xl">Hesap Oluştur</CardTitle>
          <CardDescription>Bilgilerinizi girerek kayıt olun</CardDescription>
        </CardHeader>

        <CardContent>
          <form action={formAction} className="space-y-4">
            {state.error && (
              <div className="flex items-start gap-2 rounded-lg border border-destructive/30 bg-destructive/10 px-3 py-2.5 text-sm text-destructive">
                <AlertTriangle className="mt-0.5 size-4 shrink-0" />
                {state.error}
              </div>
            )}
            {state.success && (
              <div className="rounded-lg border border-green-500/30 bg-green-500/10 px-3 py-2.5 text-sm text-green-700 dark:text-green-400">
                {state.message ?? 'Kayıt başarılı. Lütfen giriş yapın.'}
              </div>
            )}

            <div className="grid grid-cols-2 gap-3">
              <div className="space-y-1.5">
                <Label htmlFor="first_name">Ad</Label>
                <Input id="first_name" name="first_name" type="text" required />
              </div>
              <div className="space-y-1.5">
                <Label htmlFor="last_name">Soyad</Label>
                <Input id="last_name" name="last_name" type="text" required />
              </div>
            </div>

            <div className="space-y-1.5">
              <Label htmlFor="username">Kullanıcı Adı</Label>
              <Input id="username" name="username" type="text" required autoComplete="username" />
            </div>

            <div className="space-y-1.5">
              <Label htmlFor="email">E-posta</Label>
              <Input id="email" name="email" type="email" required autoComplete="email" placeholder="ornek@mail.com" />
            </div>

            <div className="space-y-1.5">
              <Label htmlFor="password">Şifre</Label>
              <Input id="password" name="password" type="password" required autoComplete="new-password" placeholder="••••••••" />
            </div>

            <div className="space-y-1.5">
              <Label htmlFor="confirm_password">Şifre Tekrar</Label>
              <Input id="confirm_password" name="confirm_password" type="password" required autoComplete="new-password" placeholder="••••••••" />
            </div>

            <Button type="submit" disabled={pending} className="w-full">
              {pending && <Loader2 className="size-4 animate-spin" />}
              {pending ? 'Kayıt yapılıyor…' : 'Kayıt Ol'}
            </Button>
          </form>
        </CardContent>

        <CardFooter className="justify-center text-sm text-muted-foreground">
          Zaten hesabınız var mı?&nbsp;
          <Link href="/auth/login" className="font-medium text-primary underline-offset-4 hover:underline">
            Giriş Yap
          </Link>
        </CardFooter>
      </Card>
    </div>
  )
}
