import Field from '@/components/Field';
import GuestLayout from '@/Layouts/GuestLayout';
import { Head, useForm } from '@inertiajs/react';
import { ArrowRight, Lock, Mail } from 'lucide-react';
import { FormEventHandler } from 'react';

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

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

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

            <div className="mb-6">
                <h1 className="pf-display text-foreground text-2xl font-bold tracking-tight">
                    Set a new password
                </h1>
                <p className="text-muted-foreground mt-1.5 text-sm">
                    Choose a strong password you haven't used before.
                </p>
            </div>

            <form onSubmit={submit} className="space-y-4" noValidate>
                <Field
                    label="Email Address"
                    id="email"
                    name="email"
                    type="email"
                    placeholder="your@email.com"
                    icon={Mail}
                    value={data.email}
                    onChange={(e) => setData('email', e.target.value)}
                    error={errors.email}
                    autoComplete="username"
                />
                <Field
                    label="New Password"
                    id="password"
                    name="password"
                    type="password"
                    placeholder="Create a strong password"
                    icon={Lock}
                    value={data.password}
                    onChange={(e) => setData('password', e.target.value)}
                    error={errors.password}
                    autoComplete="new-password"
                    autoFocus
                />
                <Field
                    label="Confirm Password"
                    id="password_confirmation"
                    name="password_confirmation"
                    type="password"
                    placeholder="Repeat your new password"
                    icon={Lock}
                    value={data.password_confirmation}
                    onChange={(e) =>
                        setData('password_confirmation', e.target.value)
                    }
                    error={errors.password_confirmation}
                    autoComplete="new-password"
                />

                <button
                    type="submit"
                    disabled={processing}
                    className="pf-btn mt-2 w-full py-3 text-sm disabled:cursor-not-allowed disabled:opacity-60"
                >
                    {processing ? (
                        'Resetting…'
                    ) : (
                        <>
                            <span>Reset Password</span>
                            <ArrowRight
                                className="h-4 w-4"
                                aria-hidden="true"
                            />
                        </>
                    )}
                </button>
            </form>
        </GuestLayout>
    );
}
