import React, { useRef } from 'react';
import { useForm } from '@inertiajs/react';
import { Transition } from '@headlessui/react';
import { useTranslation } from '@/hooks/useTranslation';

// Shadcn UI Components
import { Input } from "@/Components/ui/input";
import { Label } from "@/Components/ui/label";
import { Button } from "@/Components/ui/button";

// Icons & Helpers
import { Loader2, CheckCircle2, Lock, KeyRound, ShieldCheck } from 'lucide-react';
import InputError from '@/Components/InputError';

export default function UpdatePasswordForm({ className = '' }) {
    const { t } = useTranslation();
    const passwordInput = useRef();
    const currentPasswordInput = useRef();

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

    const updatePassword = (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 (
        <section className={className}>
            <header className="mb-6">
                <h2 className="text-lg font-bold flex items-center gap-2">
                    <ShieldCheck className="w-5 h-5 text-primary" /> 
                    {t('Update Password')}
                </h2>
                <p className="mt-1 text-sm text-muted-foreground">
                    {t('Ensure your account is using a long, random password to stay secure.')}
                </p>
            </header>

            <form onSubmit={updatePassword} className="space-y-6">
                {/* Current Password Field */}
                <div className="space-y-2 max-w-md">
                    <Label htmlFor="current_password" xxl="8" className="text-sm font-bold flex items-center gap-2">
                        <KeyRound className="w-3.5 h-3.5 text-muted-foreground" /> 
                        {t('Current Password')}
                    </Label>
                    <Input
                        id="current_password"
                        ref={currentPasswordInput}
                        value={data.current_password}
                        onChange={(e) => setData('current_password', e.target.value)}
                        type="password"
                        className="bg-background border-input"
                        autoComplete="current-password"
                        placeholder="••••••••"
                    />
                    <InputError message={errors.current_password} />
                </div>

                <div className="grid grid-cols-1 md:grid-cols-2 gap-6 pt-2">
                    {/* New Password Field */}
                    <div className="space-y-2">
                        <Label htmlFor="password" xxl="8" className="text-sm font-bold flex items-center gap-2">
                            <Lock className="w-3.5 h-3.5 text-muted-foreground" /> 
                            {t('New Password')}
                        </Label>
                        <Input
                            id="password"
                            ref={passwordInput}
                            value={data.password}
                            onChange={(e) => setData('password', e.target.value)}
                            type="password"
                            className="bg-background border-input"
                            autoComplete="new-password"
                            placeholder={t('New secure password')}
                        />
                        <InputError message={errors.password} />
                    </div>

                    {/* Confirm New Password Field */}
                    <div className="space-y-2">
                        <Label htmlFor="password_confirmation" className="text-sm font-bold flex items-center gap-2">
                            <ShieldCheck className="w-3.5 h-3.5 text-muted-foreground" /> 
                            {t('Confirm Password')}
                        </Label>
                        <Input
                            id="password_confirmation"
                            value={data.password_confirmation}
                            onChange={(e) => setData('password_confirmation', e.target.value)}
                            type="password"
                            className="bg-background border-input"
                            autoComplete="new-password"
                            placeholder={t('Repeat new password')}
                        />
                        <InputError message={errors.password_confirmation} />
                    </div>
                </div>

                {/* Footer Actions */}
                <div className="flex items-center gap-4 pt-4 border-t border-border/50">
                    <Button 
                        type="submit" 
                        disabled={processing}
                        className="px-8 font-bold shadow-md active:scale-95 transition-all"
                    >
                        {processing ? <Loader2 className="w-4 h-4 mr-2 animate-spin" /> : null}
                        {t('Update Password')}
                    </Button>

                    <Transition
                        show={recentlySuccessful}
                        enter="transition ease-in-out duration-300"
                        enterFrom="opacity-0 translate-x-4"
                        leave="transition ease-in-out duration-300"
                        leaveTo="opacity-0"
                    >
                        <p className="text-sm text-green-600 dark:text-green-500 flex items-center gap-1.5 font-bold">
                            <CheckCircle2 className="w-4 h-4" /> 
                            {t('Password Updated.')}
                        </p>
                    </Transition>
                </div>
            </form>
        </section>
    );
}