import React from 'react';
import GuestLayout from '@/Layouts/GuestLayout';
import { Head, useForm } from '@inertiajs/react';

interface ResetPasswordProps {
    token: string;
    email: string;
}

export default function ResetPassword({ token, email }: ResetPasswordProps) {
    const { data, setData, post, processing, errors, reset } = useForm({
        token,
        email,
        password: '',
        password_confirmation: '',
    });

    const submit = (e: React.FormEvent) => {
        e.preventDefault();
        post(route('password.store'), { onFinish: () => reset('password', 'password_confirmation') });
    };

    return (
        <GuestLayout>
            <Head title="Reset password" />

            <div className="mb-8 anim-down">
                <h1 className="text-2xl font-bold text-[#1A2421] tracking-tight">Set new password</h1>
                <p className="mt-1.5 text-sm text-gray-400 font-medium">
                    Choose a strong password for <span className="text-[#3D4E4B] font-semibold">{email}</span>.
                </p>
            </div>

            <form onSubmit={submit} className="space-y-4 anim-up" style={{ animationDelay: '0.1s' }}>
                {/* Email readonly — needed for form submission, not shown */}
                <input type="hidden" value={data.email} />
                <input type="hidden" value={data.token} />

                <div>
                    <label htmlFor="password" className="block text-sm font-semibold text-gray-600 mb-1.5">
                        New password
                    </label>
                    <input
                        id="password"
                        type="password"
                        autoComplete="new-password"
                        autoFocus
                        value={data.password}
                        onChange={e => setData('password', e.target.value)}
                        placeholder="Min. 8 characters"
                        className={`auth-input${errors.password ? ' !border-red-300 !bg-red-50/50' : ''}`}
                    />
                    {errors.password && <p className="mt-1.5 text-xs font-semibold text-red-500">{errors.password}</p>}
                </div>

                <div>
                    <label htmlFor="password_confirmation" className="block text-sm font-semibold text-gray-600 mb-1.5">
                        Confirm new password
                    </label>
                    <input
                        id="password_confirmation"
                        type="password"
                        autoComplete="new-password"
                        value={data.password_confirmation}
                        onChange={e => setData('password_confirmation', e.target.value)}
                        placeholder="••••••••"
                        className={`auth-input${errors.password_confirmation ? ' !border-red-300 !bg-red-50/50' : ''}`}
                    />
                    {errors.password_confirmation && <p className="mt-1.5 text-xs font-semibold text-red-500">{errors.password_confirmation}</p>}
                </div>

                <button
                    type="submit"
                    disabled={processing}
                    className="mt-2 w-full h-11 rounded-xl bg-[#3D4E4B] hover:bg-[#2D3A38] text-white text-sm font-bold tracking-tight transition-colors duration-200 flex items-center justify-center gap-2 disabled:opacity-60 disabled:cursor-not-allowed"
                >
                    {processing ? (
                        <>
                            <svg className="w-4 h-4 animate-spin text-white/60" fill="none" viewBox="0 0 24 24">
                                <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" />
                                <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" />
                            </svg>
                            Saving…
                        </>
                    ) : 'Reset password'}
                </button>
            </form>
        </GuestLayout>
    );
}
