import { Form, Head } from '@inertiajs/react';
import AuthField, {
    AuthSubmitButton,
    authInputClasses,
    errorAttributes,
} from '@/components/auth/auth-field';
import PasswordInput from '@/components/password-input';
import { update } from '@/routes/password';

type Props = {
    token: string;
    email: string;
    passwordRules: string;
};

export default function ResetPassword({ token, email, passwordRules }: Props) {
    return (
        <>
            <Head title="Reset password" />

            <Form
                {...update.form()}
                transform={(data) => ({ ...data, token, email })}
                resetOnSuccess={['password', 'password_confirmation']}
                className="flex flex-col gap-5"
            >
                {({ processing, errors }) => (
                    <>
                        <AuthField
                            id="email"
                            label="Email address"
                            error={errors.email}
                        >
                            <input
                                id="email"
                                type="email"
                                name="email"
                                autoComplete="email"
                                value={email}
                                readOnly
                                className={authInputClasses}
                                {...errorAttributes('email', errors.email)}
                            />
                        </AuthField>

                        <AuthField
                            id="password"
                            required
                            label="New password"
                            error={errors.password}
                        >
                            <PasswordInput
                                id="password"
                                name="password"
                                required
                                autoFocus
                                autoComplete="new-password"
                                placeholder="Create a new password"
                                passwordrules={passwordRules}
                                className={`${authInputClasses} pr-11`}
                                {...errorAttributes(
                                    'password',
                                    errors.password,
                                )}
                            />
                        </AuthField>

                        <AuthField
                            id="password_confirmation"
                            required
                            label="Confirm new password"
                            error={errors.password_confirmation}
                        >
                            <PasswordInput
                                id="password_confirmation"
                                name="password_confirmation"
                                required
                                autoComplete="new-password"
                                placeholder="Re-enter your new password"
                                passwordrules={passwordRules}
                                className={`${authInputClasses} pr-11`}
                                {...errorAttributes(
                                    'password_confirmation',
                                    errors.password_confirmation,
                                )}
                            />
                        </AuthField>

                        <AuthSubmitButton
                            processing={processing}
                            className="mt-2"
                            data-test="reset-password-button"
                        >
                            Save new password
                        </AuthSubmitButton>
                    </>
                )}
            </Form>
        </>
    );
}

ResetPassword.layout = {
    title: 'Choose a new password',
    description: 'Pick a strong password you have not used here before.',
};
