"use client";

import { useActionState } from "react";
import { requestPasswordReset } from "@/lib/actions/auth";

export function ForgotPasswordForm() {
  const [state, formAction, isPending] = useActionState(
    requestPasswordReset,
    undefined,
  );

  if (state?.success) {
    return (
      <p className="mt-8 text-charcoal">
        If an account exists for that email, a password reset link is on its
        way.
      </p>
    );
  }

  return (
    <form action={formAction} className="mt-8 flex flex-col gap-4">
      {state?.error && (
        <p className="rounded bg-terracotta/10 px-4 py-2 text-sm text-terracotta">
          {state.error}
        </p>
      )}

      <label className="flex flex-col gap-1">
        <span className="text-sm font-medium text-charcoal">Email</span>
        <input
          type="email"
          name="email"
          required
          className="rounded border border-charcoal/20 bg-warm-white px-4 py-2 text-charcoal"
        />
      </label>

      <button
        type="submit"
        disabled={isPending}
        className="mt-2 rounded-full bg-terracotta px-6 py-2 font-medium text-white hover:bg-terracotta/90 disabled:opacity-60"
      >
        {isPending ? "Sending..." : "Send Reset Link"}
      </button>
    </form>
  );
}
