"use client";

import Link from "next/link";
import { useActionState } from "react";
import { Button } from "@/components/ui/button";
import { register } from "@/actions/auth";

export default function RegistracePage() {
  const [state, formAction, pending] = useActionState(register, {});

  return (
    <div className="min-h-[60vh] flex items-center justify-center px-4 py-16">
      <div className="w-full max-w-sm space-y-6">
        <div className="text-center space-y-1">
          <h1 className="text-2xl font-bold text-primary">Registrace</h1>
          <p className="text-sm text-muted-foreground">
            Už máš účet?{" "}
            <Link href="/prihlaseni" className="text-primary underline underline-offset-4">
              Přihlás se
            </Link>
          </p>
        </div>

        <form action={formAction} className="space-y-4">
          {state.error && (
            <div className="p-3 rounded-lg bg-destructive/10 text-destructive text-sm text-center">
              {state.error}
            </div>
          )}

          <div className="space-y-1">
            <label className="text-sm font-medium" htmlFor="name">Jméno</label>
            <input
              id="name"
              name="name"
              type="text"
              autoComplete="name"
              required
              className="w-full rounded-md border border-input bg-background px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-ring"
              placeholder="Jan Novák"
            />
            {state.fieldErrors?.name && (
              <p className="text-xs text-destructive">{state.fieldErrors.name[0]}</p>
            )}
          </div>

          <div className="space-y-1">
            <label className="text-sm font-medium" htmlFor="email">E-mail</label>
            <input
              id="email"
              name="email"
              type="email"
              autoComplete="email"
              required
              className="w-full rounded-md border border-input bg-background px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-ring"
              placeholder="jan@example.cz"
            />
            {state.fieldErrors?.email && (
              <p className="text-xs text-destructive">{state.fieldErrors.email[0]}</p>
            )}
          </div>

          <div className="space-y-1">
            <label className="text-sm font-medium" htmlFor="password">Heslo</label>
            <input
              id="password"
              name="password"
              type="password"
              autoComplete="new-password"
              required
              className="w-full rounded-md border border-input bg-background px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-ring"
              placeholder="Alespoň 8 znaků"
            />
            {state.fieldErrors?.password && (
              <p className="text-xs text-destructive">{state.fieldErrors.password[0]}</p>
            )}
          </div>

          <Button type="submit" className="w-full" disabled={pending}>
            {pending ? "Registruji..." : "Vytvořit účet"}
          </Button>
        </form>
      </div>
    </div>
  );
}
