function Forgot() {
  const [email, setEmail] = React.useState('');
  const [sent, setSent] = React.useState(false);
  const [loading, setLoading] = React.useState(false);
  const toast = window.Toast.useToast();

  async function onSubmit(e) {
    e.preventDefault();
    setLoading(true);
    try {
      await window.AuthAPI.requestPasswordReset(email);
      setSent(true);
      toast.success('Si un compte existe pour cet email, un lien a été envoyé.');
    } catch (err) {
      const m = err?.error?.message || 'Erreur lors de la demande.';
      toast.error(m);
    } finally { setLoading(false); }
  }

  return (
    <div className="container">
      <h1>Mot de passe oublié</h1>
      {sent ? (
        <p>Vérifiez votre boîte mail et suivez le lien de réinitialisation.</p>
      ) : (
        <form onSubmit={onSubmit} style={{ maxWidth: 420 }}>
          <label>Email<br/>
            <input type="email" value={email} onChange={(e)=>setEmail(e.target.value)} placeholder="Votre email" />
          </label><br/>
          <button type="submit" disabled={loading || !email}>{loading ? 'Envoi...' : 'Envoyer le lien'}</button>
        </form>
      )}
    </div>
  );
}

window.Pages = window.Pages || {};
window.Pages.Forgot = Forgot;
