import { useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { Lock, KeyRound, ArrowLeft, CheckCircle } from 'lucide-react';
import Header from '../components/Header';
import Footer from '../components/Footer';
import { requestResetPassword } from '../config/UserRequest';
import { toast } from 'react-toastify';

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

function ResetPassword() {
    const navigate = useNavigate();
    const { dataWebsite } = useStore();
    const [loading, setLoading] = useState(false);
    const [formData, setFormData] = useState({
        otp: '',
        newPassword: '',
        confirmPassword: '',
    });

    const handleChange = (e) => {
        setFormData({
            ...formData,
            [e.target.name]: e.target.value,
        });
    };

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

        if (!formData.otp || !formData.newPassword || !formData.confirmPassword) {
            toast.error('Vui lòng nhập đầy đủ thông tin!');
            return;
        }

        if (formData.newPassword !== formData.confirmPassword) {
            toast.error('Mật khẩu nhập lại không khớp!');
            return;
        }

        if (formData.newPassword.length < 6) {
            toast.error('Mật khẩu phải có ít nhất 6 ký tự!');
            return;
        }

        setLoading(true);
        try {
            await requestResetPassword({
                otp: formData.otp,
                newPassword: formData.newPassword,
            });
            toast.success('Đổi mật khẩu thành công! Vui lòng đăng nhập lại.');
            navigate('/login');
        } catch (error) {
            toast.error(error.response?.data?.message || 'Đổi mật khẩ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>Đặt lại 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="/forgot-password"
                            className="absolute top-4 left-4 p-2 bg-white/20 hover:bg-white/30 rounded-full transition-colors"
                            title="Quay lại"
                        >
                            <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">
                            <KeyRound size={32} />
                        </div>
                        <h2 className="text-3xl font-bold mb-2">Đặt Lại Mật Khẩu</h2>
                        <p className="text-orange-100 text-sm">Nhập mã OTP và mật khẩu mới</p>
                    </div>

                    {/* Form Section */}
                    <div className="p-8 space-y-6">
                        <form onSubmit={handleSubmit} className="space-y-5">
                            {/* OTP Input */}
                            <div className="space-y-1">
                                <label className="text-sm font-semibold text-gray-700 ml-1">Mã OTP</label>
                                <div className="relative">
                                    <input
                                        type="text"
                                        name="otp"
                                        value={formData.otp}
                                        onChange={handleChange}
                                        placeholder="Nhập mã OTP (6 số)"
                                        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 tracking-widest font-medium"
                                        required
                                    />
                                    <KeyRound
                                        className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400"
                                        size={18}
                                    />
                                </div>
                            </div>

                            {/* New Password Input */}
                            <div className="space-y-1">
                                <label className="text-sm font-semibold text-gray-700 ml-1">Mật khẩu mới</label>
                                <div className="relative">
                                    <input
                                        type="password"
                                        name="newPassword"
                                        value={formData.newPassword}
                                        onChange={handleChange}
                                        placeholder="Nhập mật khẩu mới"
                                        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
                                    />
                                    <Lock
                                        className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400"
                                        size={18}
                                    />
                                </div>
                            </div>

                            {/* Confirm Password Input */}
                            <div className="space-y-1">
                                <label className="text-sm font-semibold text-gray-700 ml-1">Xác nhận mật khẩu</label>
                                <div className="relative">
                                    <input
                                        type="password"
                                        name="confirmPassword"
                                        value={formData.confirmPassword}
                                        onChange={handleChange}
                                        placeholder="Nhập lại mật khẩu mới"
                                        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
                                    />
                                    <Lock
                                        className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400"
                                        size={18}
                                    />
                                </div>
                            </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 xử lý...</span>
                                    </>
                                ) : (
                                    <>
                                        <span>Xác Nhận Đổi Mật Khẩu</span>
                                        <CheckCircle size={20} />
                                    </>
                                )}
                            </button>
                        </form>
                    </div>
                </div>
            </main>

            <footer>
                <Footer />
            </footer>
        </div>
    );
}

export default ResetPassword;
