"use client";

import { useState } from "react";

export default function ResetPage() {
  const [status, setStatus] = useState<string | null>(null);
  const [error, setError] = useState<string | null>(null);

  const onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    setStatus(null);
    setError(null);
    const formData = new FormData(event.currentTarget);
    const email = formData.get("email");

    const response = await fetch("/api/password-reset/request", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ email })
    });

    if (!response.ok) {
      const data = await response.json();
      setError(data.error || "Anfrage fehlgeschlagen.");
      return;
    }

    setStatus("Falls ein Konto existiert, wurde ein Link versendet.");
    event.currentTarget.reset();
  };

  return (
    <div className="mx-auto max-w-md card fade-up">
      <h1 className="text-2xl font-semibold">Passwort zurücksetzen</h1>
      <p className="mt-1 text-sm text-slate-600">
        Gib deine E-Mail an und wir senden dir einen Link zum Zurücksetzen.
      </p>
      <form onSubmit={onSubmit} className="mt-4 space-y-3">
        <input
          name="email"
          type="email"
          placeholder="E-Mail"
          required
          className="w-full rounded-xl border border-slate-300 px-3 py-2"
        />
        <button type="submit" className="btn-accent w-full">
          Link senden
        </button>
      </form>
      {status && <p className="mt-3 text-sm text-emerald-600">{status}</p>}
      {error && <p className="mt-3 text-sm text-red-600">{error}</p>}
    </div>
  );
}
