"use client";

import * as React from "react";
import { useActionState } from "react";
import Link from "next/link";
import { Eye, EyeOff, UserPlus } from "lucide-react";
import { DocField, DocInput, DocNotice, DocSelect } from "@/components/auth/doc-field";
import { registerAction } from "@/actions/auth";
import { IDLE_ACTION } from "@/lib/action-state";

export function RegisterForm() {
  const [state, formAction, pending] = useActionState(registerAction, IDLE_ACTION);
  const [showPassword, setShowPassword] = React.useState(false);
  const [role, setRole] = React.useState("STUDENT");

  const errors = state.status === "error" ? state.errors ?? {} : {};

  return (
    <form action={formAction} className="space-y-5">
      {state.status === "error" && state.message ? (
        <DocNotice>{state.message}</DocNotice>
      ) : null}

      <DocField
        label="Nama lengkap"
        htmlFor="fullName"
        required
        error={errors.fullName}
      >
        <DocInput
          id="fullName"
          name="fullName"
          required
          autoComplete="name"
          placeholder="mis. Andi Pratama"
        />
      </DocField>

      <DocField
        label="Username"
        htmlFor="username"
        required
        error={errors.username}
        hint="Huruf, angka, titik, garis bawah, atau tanda hubung."
      >
        <DocInput
          id="username"
          name="username"
          required
          autoComplete="username"
          autoCapitalize="none"
          autoCorrect="off"
          spellCheck={false}
          placeholder="mis. andi.pratama"
        />
      </DocField>

      <DocField
        label="Email"
        htmlFor="email"
        counter="Opsional"
        error={errors.email}
        hint="Dipakai untuk notifikasi, bukan untuk masuk."
      >
        <DocInput
          id="email"
          name="email"
          type="email"
          autoComplete="email"
          placeholder="nama@sekolah.sch.id"
        />
      </DocField>

      <DocField
        label="Password"
        htmlFor="password"
        required
        error={errors.password}
        hint="Minimal 8 karakter, mengandung huruf dan angka."
      >
        <div className="relative">
          <DocInput
            id="password"
            name="password"
            type={showPassword ? "text" : "password"}
            required
            autoComplete="new-password"
            placeholder="••••••••"
            className="doc-field-toggle"
          />
          <button
            type="button"
            onClick={() => setShowPassword((value) => !value)}
            className="absolute right-2 top-1/2 grid size-8 -translate-y-1/2 place-items-center text-doc-ink-3 transition-colors hover:text-doc-ink"
            aria-label={showPassword ? "Sembunyikan password" : "Tampilkan password"}
          >
            {showPassword ? <EyeOff className="size-4" /> : <Eye className="size-4" />}
          </button>
        </div>
      </DocField>

      <DocField label="Daftar sebagai" htmlFor="role" required>
        <DocSelect
          id="role"
          name="role"
          value={role}
          onChange={(event) => setRole(event.target.value)}
        >
          <option value="STUDENT">Siswa</option>
          <option value="TEACHER">Guru</option>
        </DocSelect>
      </DocField>

      {role === "STUDENT" ? (
        <DocField
          label="Kode kelas"
          htmlFor="joinCode"
          counter="Opsional"
          error={errors.joinCode}
          hint="Masukkan kode dari gurumu untuk langsung bergabung ke kelas."
        >
          <DocInput
            id="joinCode"
            name="joinCode"
            placeholder="mis. RBT7A"
            autoCapitalize="characters"
            className="uppercase"
          />
        </DocField>
      ) : null}

      <button type="submit" disabled={pending} className="btn w-full disabled:opacity-60">
        {pending ? "Membuat akun…" : "Daftar"}
        {pending ? null : <UserPlus className="size-3.5" />}
      </button>

      <p className="text-center text-[0.8125rem] text-doc-ink-2">
        Sudah punya akun?{" "}
        <Link href="/login" className="link-draw font-medium text-doc-ink">
          Masuk di sini
        </Link>
      </p>
    </form>
  );
}
