"use client";

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

export function ResetPasswordForm({ token }: { token: string }) {
  const [state, formAction, isPending] = useActionState(
    resetPassword,
    undefined,
  );

  if (state?.success) {
    return (
      <p className="mt-8 text-charcoal">
        Your password has been reset. You can now log in with your new
        password.
      </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>
      )}

      <input type="hidden" name="token" value={token} />

      <label className="flex flex-col gap-1">
        <span className="text-sm font-medium text-charcoal">
          New password
        </span>
        <input
          type="password"
          name="password"
          required
          minLength={8}
          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 ? "Resetting..." : "Reset Password"}
      </button>
    </form>
  );
}
