import { Transition } from '@headlessui/react';
import { Form, Head } from '@inertiajs/react';
import { useRef } from 'react';
import PasswordController from '@/actions/App/Http/Controllers/Settings/PasswordController';
import Heading from '@/components/heading';
import InputError from '@/components/input-error';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import AppLayout from '@/layouts/app-layout';
import SettingsLayout from '@/layouts/settings/layout';
import { edit } from '@/routes/user-password';
import type { BreadcrumbItem } from '@/types';

const breadcrumbs: BreadcrumbItem[] = [
    {
        title: 'Paramètres du mot de passe',
        href: edit().url,
    },
];

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

    // Couleurs du thème
    const theme = {
        primary: 'bg-red-600 hover:bg-red-700 text-white focus:ring-2 focus:ring-red-500 focus:ring-offset-2',
        secondary: 'bg-blue-600 hover:bg-blue-700 text-white',
        success: 'bg-green-600 hover:bg-green-700 text-white',
        dark: 'bg-black hover:bg-gray-900 text-white',
        inputFocus: 'focus:border-blue-500 focus:ring-blue-500',
        danger: 'text-red-600 dark:text-red-400',
        successText: 'text-green-600 dark:text-green-400',
        muted: 'text-gray-600 dark:text-gray-400',
        border: 'border-gray-200 dark:border-gray-700',
        card: 'bg-white dark:bg-gray-800 shadow-lg rounded-xl',
    };

    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title="Paramètres du mot de passe" />

            <h1 className="sr-only">Paramètres du Mot de Passe</h1>

            <SettingsLayout>
                <div className="space-y-8">
                    {/* En-tête avec icône */}
                    <div className="flex items-center gap-4 mb-2">
                        <div className={`p-3 rounded-lg ${theme.primary} bg-opacity-10`}>
                            <svg className="w-6 h-6 text-red-600 dark:text-red-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z" />
                            </svg>
                        </div>
                        <div>
                            <Heading
                                variant="small"
                                title="Modifier le mot de passe"
                                description="Assurez-vous que votre compte utilise un mot de passe long et aléatoire pour rester sécurisé"
                            />
                        </div>
                    </div>

                    {/* Carte principale */}
                    <div className={`p-8 ${theme.card} border ${theme.border}`}>
                        <Form
                            {...PasswordController.update.form()}
                            options={{
                                preserveScroll: true,
                            }}
                            resetOnError={[
                                'password',
                                'password_confirmation',
                                'current_password',
                            ]}
                            resetOnSuccess
                            onError={(errors) => {
                                if (errors.password) {
                                    passwordInput.current?.focus();
                                }

                                if (errors.current_password) {
                                    currentPasswordInput.current?.focus();
                                }
                            }}
                            className="space-y-8"
                        >
                            {({ errors, processing, recentlySuccessful }) => (
                                <>
                                    {/* Mot de passe actuel */}
                                    <div className="space-y-4">
                                        <div className="flex items-center justify-between">
                                            <Label 
                                                htmlFor="current_password"
                                                className="text-lg font-semibold text-gray-900 dark:text-white"
                                            >
                                                Mot de passe actuel
                                            </Label>
                                            <span className={`text-sm ${theme.muted}`}>
                                                Requis pour confirmer votre identité
                                            </span>
                                        </div>
                                        
                                        <div className="relative">
                                            <Input
                                                id="current_password"
                                                ref={currentPasswordInput}
                                                name="current_password"
                                                type="password"
                                                className={`w-full py-3 px-4 pr-12 border-2 ${theme.border} ${theme.inputFocus} rounded-lg`}
                                                autoComplete="current-password"
                                                placeholder="Entrez votre mot de passe actuel"
                                            />
                                            <div className="absolute right-3 top-1/2 transform -translate-y-1/2">
                                                <svg className="w-5 h-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                                                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
                                                </svg>
                                            </div>
                                        </div>
                                        
                                        <InputError 
                                            message={errors.current_password} 
                                            className={`p-3 rounded-lg bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800`}
                                        />
                                    </div>

                                    {/* Séparateur visuel */}
                                    <div className="relative">
                                        <div className="absolute inset-0 flex items-center">
                                            <div className="w-full border-t border-gray-300 dark:border-gray-600"></div>
                                        </div>
                                        <div className="relative flex justify-center">
                                            <span className={`px-4 ${theme.card} text-sm ${theme.muted}`}>
                                                Nouveau mot de passe
                                            </span>
                                        </div>
                                    </div>

                                    {/* Nouveau mot de passe */}
                                    <div className="space-y-6">
                                        <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
                                            <div className="space-y-4">
                                                <Label 
                                                    htmlFor="password"
                                                    className="text-lg font-semibold text-gray-900 dark:text-white flex items-center gap-2"
                                                >
                                                    <svg className="w-5 h-5 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
                                                    </svg>
                                                    Nouveau mot de passe
                                                </Label>
                                                
                                                <div className="relative">
                                                    <Input
                                                        id="password"
                                                        ref={passwordInput}
                                                        name="password"
                                                        type="password"
                                                        className={`w-full py-3 px-4 pr-12 border-2 ${theme.border} ${theme.inputFocus} rounded-lg`}
                                                        autoComplete="new-password"
                                                        placeholder="Choisissez un mot de passe fort"
                                                    />
                                                    <div className="absolute right-3 top-1/2 transform -translate-y-1/2">
                                                        <svg className="w-5 h-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z" />
                                                        </svg>
                                                    </div>
                                                </div>
                                                
                                                <InputError 
                                                    message={errors.password}
                                                    className={`p-3 rounded-lg bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800`}
                                                />
                                            </div>

                                            <div className="space-y-4">
                                                <Label 
                                                    htmlFor="password_confirmation"
                                                    className="text-lg font-semibold text-gray-900 dark:text-white flex items-center gap-2"
                                                >
                                                    <svg className="w-5 h-5 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 11V7a4 4 0 118 0m-4 8v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2z" />
                                                    </svg>
                                                    Confirmation
                                                </Label>
                                                
                                                <div className="relative">
                                                    <Input
                                                        id="password_confirmation"
                                                        name="password_confirmation"
                                                        type="password"
                                                        className={`w-full py-3 px-4 pr-12 border-2 ${theme.border} ${theme.inputFocus} rounded-lg`}
                                                        autoComplete="new-password"
                                                        placeholder="Retapez votre nouveau mot de passe"
                                                    />
                                                    <div className="absolute right-3 top-1/2 transform -translate-y-1/2">
                                                        <svg className="w-5 h-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z" />
                                                        </svg>
                                                    </div>
                                                </div>
                                                
                                                <InputError 
                                                    message={errors.password_confirmation}
                                                    className={`p-3 rounded-lg bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800`}
                                                />
                                            </div>
                                        </div>

                                        {/* Conseils de sécurité */}
                                        <div className={`p-4 rounded-lg bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800`}>
                                            <h4 className="font-semibold text-blue-800 dark:text-blue-300 mb-2 flex items-center gap-2">
                                                <svg className="w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
                                                    <path fillRule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clipRule="evenodd" />
                                                </svg>
                                                Conseils pour un mot de passe sécurisé
                                            </h4>
                                            <ul className={`text-sm ${theme.muted} space-y-1 ml-7`}>
                                                <li className="flex items-center gap-2">
                                                    <div className="w-1.5 h-1.5 rounded-full bg-red-500"></div>
                                                    Au moins 12 caractères
                                                </li>
                                                <li className="flex items-center gap-2">
                                                    <div className="w-1.5 h-1.5 rounded-full bg-blue-500"></div>
                                                    Mélange de lettres majuscules/minuscules
                                                </li>
                                                <li className="flex items-center gap-2">
                                                    <div className="w-1.5 h-1.5 rounded-full bg-green-500"></div>
                                                    Chiffres et caractères spéciaux
                                                </li>
                                                <li className="flex items-center gap-2">
                                                    <div className="w-1.5 h-1.5 rounded-full bg-black"></div>
                                                    Pas d'informations personnelles évidentes
                                                </li>
                                            </ul>
                                        </div>
                                    </div>

                                    {/* Bouton d'action et feedback */}
                                    <div className="pt-6 border-t border-gray-200 dark:border-gray-700">
                                        <div className="flex flex-col sm:flex-row items-center justify-between gap-4">
                                            <div className="flex items-center gap-4">
                                                <Button
                                                    disabled={processing}
                                                    data-test="update-password-button"
                                                    className={`px-8 py-3 rounded-lg font-semibold text-lg transition-all duration-300 ${theme.primary} shadow-md hover:shadow-lg transform hover:-translate-y-0.5`}
                                                >
                                                    {processing ? (
                                                        <span className="flex items-center gap-2">
                                                            <svg className="animate-spin h-5 w-5 text-white" fill="none" viewBox="0 0 24 24">
                                                                <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
                                                                <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                                                            </svg>
                                                            Modification en cours...
                                                        </span>
                                                    ) : (
                                                        <span className="flex items-center gap-2">
                                                            <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
                                                            </svg>
                                                            Mettre à jour le mot de passe
                                                        </span>
                                                    )}
                                                </Button>

                                                <Transition
                                                    show={recentlySuccessful}
                                                    enter="transition-all duration-300 ease-out"
                                                    enterFrom="opacity-0 translate-x-4"
                                                    enterTo="opacity-100 translate-x-0"
                                                    leave="transition-all duration-300 ease-in"
                                                    leaveTo="opacity-0 translate-x-4"
                                                >
                                                    <div className={`flex items-center gap-2 px-4 py-2 rounded-lg bg-green-50 dark:bg-green-900/30 border border-green-200 dark:border-green-800`}>
                                                        <svg className="w-5 h-5 text-green-600 dark:text-green-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
                                                        </svg>
                                                        <p className={`text-sm font-semibold ${theme.successText}`}>
                                                            Mot de passe mis à jour avec succès !
                                                        </p>
                                                    </div>
                                                </Transition>
                                            </div>

                                            <div className={`text-sm ${theme.muted} text-center sm:text-right`}>
                                                <p>Vous serez déconnecté après la modification</p>
                                                <p className="text-xs mt-1">Reconnectez-vous avec votre nouveau mot de passe</p>
                                            </div>
                                        </div>
                                    </div>
                                </>
                            )}
                        </Form>
                    </div>

                    {/* Note de sécurité */}
                    <div className={`p-4 rounded-lg border ${theme.border} bg-gray-50 dark:bg-gray-900/30`}>
                        <div className="flex items-start gap-3">
                            <div className={`p-2 rounded ${theme.primary} bg-opacity-10`}>
                                <svg className="w-5 h-5 text-red-600 dark:text-red-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M20.618 5.984A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016zM12 9v2m0 4h.01" />
                                </svg>
                            </div>
                            <div>
                                <h4 className="font-semibold text-gray-900 dark:text-white mb-1">
                                    Sécurité importante
                                </h4>
                                <p className={`text-sm ${theme.muted}`}>
                                    Après avoir changé votre mot de passe, toutes vos sessions actives seront fermées pour des raisons de sécurité. 
                                    Vous devrez vous reconnecter sur tous vos appareils.
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </SettingsLayout>
        </AppLayout>
    );
}