"use client";

import { useActionState } from "react";
import { AlertCircle, Mail, CheckCircle2, Loader2 } from "lucide-react";
import { requestPasswordReset, type ForgotPasswordState } from "./actions";
import { Button } from "@/components/Button";

const initial: ForgotPasswordState = { ok: false };

export function ForgotPasswordForm() {
  const [state, action, pending] = useActionState(requestPasswordReset, initial);
  return (
    <form action={action} className="space-y-5">
      {state.message && (
        <div
          role="alert"
          className={
            state.ok
              ? "flex items-start gap-2 rounded-md border border-emerald-200 bg-emerald-50 p-3 text-sm text-emerald-900"
              : "flex items-start gap-2 rounded-md border border-red-200 bg-red-50 p-3 text-sm text-red-800"
          }
        >
          {state.ok ? (
            <CheckCircle2 className="mt-0.5 h-4 w-4 shrink-0" aria-hidden />
          ) : (
            <AlertCircle className="mt-0.5 h-4 w-4 shrink-0" aria-hidden />
          )}
          <span>{state.message}</span>
        </div>
      )}
      <label className="block">
        <span className="mb-1.5 block text-sm font-semibold text-[var(--color-ink)]">
          Email
        </span>
        <div className="relative">
          <Mail
            className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-[var(--color-slate)]"
            aria-hidden
          />
          <input
            name="email"
            type="email"
            required
            autoComplete="email"
            placeholder="you@hospital.org"
            className="w-full rounded-md border border-[var(--color-line)] bg-white py-2.5 pl-9 pr-3 text-[var(--color-ink)] placeholder:text-[var(--color-slate)]/60 focus:border-[var(--color-blue)] focus:outline-none focus:ring-2 focus:ring-[var(--color-blue)]/20"
          />
        </div>
      </label>
      <Button type="submit" disabled={pending} className="w-full">
        {pending ? (
          <>
            <Loader2 className="h-4 w-4 animate-spin" aria-hidden />
            Sending reset link…
          </>
        ) : (
          "Send reset link"
        )}
      </Button>
    </form>
  );
}
