import InputError from '@/components/input-error';
import SettingsLayout from '@/layouts/settings/layout';
import { Transition } from '@headlessui/react';
import { Head, useForm } from '@inertiajs/react';
import { Check, Lock, ShieldCheck } from 'lucide-react';
import { FormEventHandler, useRef } from 'react';

export default 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: (errors) => {
                if (errors.password) {
                    reset('password', 'password_confirmation');
                    passwordInput.current?.focus();
                }

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

    return (
        <SettingsLayout>
            <Head title="Passwort Einstellungen - Rose Hosting" />

            <div className="space-y-8">
                <div>
                    <h2 className="text-lg font-bold text-white flex items-center gap-2">
                        <Lock className="h-5 w-5 text-rose-400" /> Passwort ändern
                    </h2>
                    <p className="text-xs text-slate-400 mt-1">
                        Verwende ein sicheres, zufälliges Passwort, um deinen Server-Account optimal zu schützen.
                    </p>
                </div>

                <form onSubmit={updatePassword} className="space-y-5">
                    {/* Current Password */}
                    <div className="space-y-2">
                        <label htmlFor="current_password" className="block text-xs font-semibold text-slate-200">
                            Aktuelles Passwort
                        </label>
                        <input
                            id="current_password"
                            ref={currentPasswordInput}
                            value={data.current_password}
                            onChange={(e) => setData('current_password', e.target.value)}
                            type="password"
                            autoComplete="current-password"
                            placeholder="Dein aktuelles Passwort"
                            className="w-full bg-[#050203] border border-[#36131b] rounded-xl px-4 py-2.5 text-xs text-white outline-none focus:border-red-500 font-medium"
                        />
                        <InputError className="mt-1 text-red-400 text-xs" message={errors.current_password} />
                    </div>

                    {/* New Password */}
                    <div className="space-y-2">
                        <label htmlFor="password" className="block text-xs font-semibold text-slate-200">
                            Neues Passwort
                        </label>
                        <input
                            id="password"
                            ref={passwordInput}
                            value={data.password}
                            onChange={(e) => setData('password', e.target.value)}
                            type="password"
                            autoComplete="new-password"
                            placeholder="Mindestens 8 Zeichen"
                            className="w-full bg-[#050203] border border-[#36131b] rounded-xl px-4 py-2.5 text-xs text-white outline-none focus:border-red-500 font-medium"
                        />
                        <InputError className="mt-1 text-red-400 text-xs" message={errors.password} />
                    </div>

                    {/* Confirm Password */}
                    <div className="space-y-2">
                        <label htmlFor="password_confirmation" className="block text-xs font-semibold text-slate-200">
                            Neues Passwort bestätigen
                        </label>
                        <input
                            id="password_confirmation"
                            value={data.password_confirmation}
                            onChange={(e) => setData('password_confirmation', e.target.value)}
                            type="password"
                            autoComplete="new-password"
                            placeholder="Passwort wiederholen"
                            className="w-full bg-[#050203] border border-[#36131b] rounded-xl px-4 py-2.5 text-xs text-white outline-none focus:border-red-500 font-medium"
                        />
                        <InputError className="mt-1 text-red-400 text-xs" message={errors.password_confirmation} />
                    </div>

                    <div className="flex items-center gap-4 pt-2">
                        <button
                            type="submit"
                            disabled={processing}
                            className="px-6 py-2.5 rounded-xl bg-red-600 hover:bg-red-700 text-white font-bold text-xs shadow-lg shadow-red-600/25 active:scale-95 transition-all disabled:opacity-50"
                        >
                            {processing ? 'Speichern...' : 'Passwort Aktualisieren'}
                        </button>

                        <Transition
                            show={recentlySuccessful}
                            enter="transition ease-in-out"
                            enterFrom="opacity-0"
                            leave="transition ease-in-out"
                            leaveTo="opacity-0"
                        >
                            <span className="text-xs text-emerald-400 font-semibold flex items-center gap-1 font-mono">
                                <Check className="h-4 w-4" /> Passwort aktualisiert!
                            </span>
                        </Transition>
                    </div>
                </form>
            </div>
        </SettingsLayout>
    );
}
