import { SettingsLayout } from '@admin/components/settings/settings-layout';
import { Button } from '@admin/components/ui/button';
import { PasswordStrengthIndicator } from '@admin/components/ui/forms/password-strength-indicator';
import { FormError } from '@admin/components/ui/form-error';
import { Input } from '@admin/components/ui/input';
import { Label } from '@admin/components/ui/label';
import { dashboardPersistentLayout } from '@admin/layouts/dashboard-page-layout';
import { Head, useForm } from '@inertiajs/react';
import { FormEventHandler, useRef } from 'react';

function Password() {
    const passwordInput = useRef<HTMLInputElement>(null);
    const currentPasswordInput = useRef<HTMLInputElement>(null);

    const { data, setData, errors, put, reset, processing, recentlySuccessful } = useForm({
        current_password: '',
        password: '',
        password_confirmation: '',
    });

    const updatePassword: FormEventHandler = (e) => {
        e.preventDefault();

        put(route('password.update'), {
            preserveScroll: true,
            onSuccess: () => reset(),
            onError: (formErrors) => {
                if (formErrors.password) {
                    reset('password', 'password_confirmation');
                    passwordInput.current?.focus();
                }

                if (formErrors.current_password) {
                    reset('current_password');
                    currentPasswordInput.current?.focus();
                }
            },
        });
    };

    return (
        <>
            <Head title="Password settings" />

            <SettingsLayout>
                <div className="space-y-6">
                    <div>
                        <h4 className="text-lg font-semibold text-gray-800 dark:text-white">Update password</h4>
                        <p className="text-sm text-gray-500 dark:text-white/70">
                            Use a strong password with uppercase, lowercase, and a number or symbol.
                        </p>
                    </div>

                    <form onSubmit={updatePassword} className="space-y-4">
                        <div>
                            <Label htmlFor="current_password">Current password</Label>
                            <Input
                                id="current_password"
                                ref={currentPasswordInput}
                                value={data.current_password}
                                onChange={(e) => setData('current_password', e.target.value)}
                                type="password"
                                autoComplete="current-password"
                            />
                            <FormError message={errors.current_password} />
                        </div>

                        <div>
                            <Label htmlFor="password">New password</Label>
                            <Input
                                id="password"
                                ref={passwordInput}
                                value={data.password}
                                onChange={(e) => setData('password', e.target.value)}
                                type="password"
                                autoComplete="new-password"
                            />
                            <PasswordStrengthIndicator password={data.password} />
                            <FormError message={errors.password} />
                        </div>

                        <div>
                            <Label htmlFor="password_confirmation">Confirm password</Label>
                            <Input
                                id="password_confirmation"
                                value={data.password_confirmation}
                                onChange={(e) => setData('password_confirmation', e.target.value)}
                                type="password"
                                autoComplete="new-password"
                            />
                            <FormError message={errors.password_confirmation} />
                        </div>

                        <div className="flex items-center gap-3">
                            <Button type="submit" disabled={processing}>
                                Save password
                            </Button>
                            {recentlySuccessful && <span className="text-sm text-success">Saved.</span>}
                        </div>
                    </form>
                </div>
            </SettingsLayout>
        </>
    );
}

export default Object.assign(Password, {
    layout: dashboardPersistentLayout('Password', 'Settings'),
});
