import { useState } from 'react';
import { Link } from 'react-router-dom';
import { useAuthStore } from '../../store/authStore';
import { Blocks, Mail, ArrowLeft, CheckCircle } from 'lucide-react';

export default function ForgotPassword() {
  const [email, setEmail] = useState('');
  const [sent, setSent] = useState(false);
  const { forgotPassword, loading, error } = useAuthStore();

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    try {
      await forgotPassword(email);
      setSent(true);
    } catch {}
  };

  if (sent) {
    return (
      <div className="min-h-screen flex items-center justify-center bg-surface-950 p-4">
        <div className="w-full max-w-md text-center">
          <div className="flex items-center justify-center gap-2 mb-8">
            <Blocks className="w-10 h-10 text-primary-500" />
            <span className="text-3xl font-bold text-white">RealBlocks</span>
          </div>
          <div className="card">
            <CheckCircle className="w-16 h-16 text-green-400 mx-auto mb-4" />
            <h2 className="text-xl font-bold text-white mb-2">Check your email</h2>
            <p className="text-surface-400 mb-6">
              If an account exists for {email}, we've sent a password reset link.
            </p>
            <Link to="/login" className="btn-primary inline-flex">
              Back to Sign In
            </Link>
          </div>
        </div>
      </div>
    );
  }

  return (
    <div className="min-h-screen flex items-center justify-center bg-surface-950 p-4">
      <div className="w-full max-w-md">
        <div className="text-center mb-8">
          <div className="flex items-center justify-center gap-2 mb-4">
            <Blocks className="w-10 h-10 text-primary-500" />
            <span className="text-3xl font-bold text-white">RealBlocks</span>
          </div>
          <p className="text-surface-400">Reset your password</p>
        </div>

        <form onSubmit={handleSubmit} className="card space-y-4">
          {error && (
            <div className="bg-red-500/10 border border-red-500/30 text-red-400 px-4 py-2 rounded-lg text-sm">
              {error}
            </div>
          )}

          <p className="text-sm text-surface-400">
            Enter your email address and we'll send you a link to reset your password.
          </p>

          <div>
            <label className="block text-sm text-surface-300 mb-1">Email</label>
            <div className="relative">
              <Mail className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-surface-400" />
              <input
                type="email"
                value={email}
                onChange={(e) => setEmail(e.target.value)}
                className="input pl-10"
                placeholder="you@example.com"
                required
              />
            </div>
          </div>

          <button type="submit" disabled={loading} className="btn-primary w-full">
            {loading ? 'Sending...' : 'Send Reset Link'}
          </button>

          <Link to="/login" className="flex items-center justify-center gap-2 text-sm text-surface-400 hover:text-white">
            <ArrowLeft className="w-4 h-4" /> Back to Sign In
          </Link>
        </form>
      </div>
    </div>
  );
}
