import { useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { Mail, ArrowLeft, Send } from 'lucide-react';
import Header from '../components/Header';
import { requestForgotPassword } from '../config/UserRequest';
import { toast } from 'react-toastify';
import Footer from '../components/Footer';

import { Helmet } from 'react-helmet';
import { useStore } from '../hooks/useStore';

function ForgotPassword() {
    const navigate = useNavigate();
    const { dataWebsite } = useStore();
    const [loading, setLoading] = useState(false);
    const [email, setEmail] = useState('');

    const handleSubmit = async (e) => {
        e.preventDefault();

        if (!email) {
            toast.error('Vui lòng nhập email!');
            return;
        }

        setLoading(true);
        try {
            await requestForgotPassword({ email });
            toast.success('Mã xác nhận đã được gửi đến email của bạn!');
            // Chuyển hướng đến trang nhập OTP/Reset password
            navigate('/reset-password');
        } catch (error) {
            toast.error(error.response?.data?.message || 'Gửi yêu cầu thất bại!');
        } finally {
            setLoading(false);
        }
    };

    return (
        <div className="min-h-screen bg-gradient-to-br from-slate-50 via-white to-orange-50">
            <Helmet>
                <title>Quên mật khẩu | {dataWebsite?.nameWebsite || 'Website'}</title>
            </Helmet>
            <header className="pb-16">
                <Header />
            </header>

            <main className="container mx-auto px-4 py-12 flex items-center justify-center min-h-[calc(100vh-64px)]">
                <div className="w-full max-w-md bg-white rounded-2xl shadow-xl border border-gray-100 overflow-hidden transform transition-all hover:shadow-2xl">
                    {/* Header Section */}
                    <div className="bg-gradient-to-r from-orange-500 to-red-500 p-8 text-center text-white relative">
                        <Link
                            to="/login"
                            className="absolute top-4 left-4 p-2 bg-white/20 hover:bg-white/30 rounded-full transition-colors"
                            title="Quay lại đăng nhập"
                        >
                            <ArrowLeft size={20} />
                        </Link>
                        <div className="w-16 h-16 bg-white/20 rounded-full flex items-center justify-center mx-auto mb-4 backdrop-blur-sm">
                            <Mail size={32} />
                        </div>
                        <h2 className="text-3xl font-bold mb-2">Quên Mật Khẩu</h2>
                        <p className="text-orange-100 text-sm">Nhập email để nhận mã xác nhận</p>
                    </div>

                    {/* Form Section */}
                    <div className="p-8 space-y-6">
                        <form onSubmit={handleSubmit} className="space-y-5">
                            {/* Email Input */}
                            <div className="space-y-1">
                                <label className="text-sm font-semibold text-gray-700 ml-1">Email đăng ký</label>
                                <div className="relative">
                                    <input
                                        type="email"
                                        value={email}
                                        onChange={(e) => setEmail(e.target.value)}
                                        placeholder="Nhập địa chỉ email của bạn"
                                        className="w-full pl-10 pr-4 py-3 bg-gray-50 border border-gray-200 rounded-xl focus:outline-none focus:ring-2 focus:ring-orange-400 focus:border-transparent transition-all"
                                        required
                                    />
                                    <Mail
                                        className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400"
                                        size={18}
                                    />
                                </div>
                                <p className="text-xs text-gray-500 ml-1">
                                    Chúng tôi sẽ gửi mã OTP gồm 6 chữ số đến email này.
                                </p>
                            </div>

                            {/* Submit Button */}
                            <button
                                type="submit"
                                disabled={loading}
                                className="w-full bg-gradient-to-r from-orange-500 to-red-500 text-white font-bold py-3.5 rounded-xl shadow-lg hover:shadow-xl hover:from-orange-600 hover:to-red-600 transform transition-all active:scale-[0.98] disabled:opacity-70 disabled:cursor-not-allowed flex items-center justify-center gap-2"
                            >
                                {loading ? (
                                    <>
                                        <div className="w-5 h-5 border-2 border-white/30 border-t-white rounded-full animate-spin"></div>
                                        <span>Đang gửi...</span>
                                    </>
                                ) : (
                                    <>
                                        <span>Gửi Mã Xác Nhận</span>
                                        <Send size={20} />
                                    </>
                                )}
                            </button>
                        </form>

                        {/* Login Link */}
                        <div className="text-center pt-4 border-t border-gray-100">
                            <p className="text-gray-600 text-sm">
                                Nhớ mật khẩu?{' '}
                                <Link
                                    to="/login"
                                    className="text-orange-600 font-bold hover:text-orange-700 hover:underline transition-colors"
                                >
                                    Đăng nhập
                                </Link>
                            </p>
                        </div>
                    </div>
                </div>
            </main>
            <footer>
                <Footer />
            </footer>
        </div>
    );
}

export default ForgotPassword;
