import { useState } from 'react';
import { Form, Head } from '@inertiajs/react';
import InputError from '@/components/input-error';
import TextLink from '@/components/text-link';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Spinner } from '@/components/ui/spinner';
import AuthLayout from '@/layouts/auth-layout';
import { home, register } from '@/routes';
import { store } from '@/routes/login';
import { request } from '@/routes/password';
import { Link } from '@inertiajs/react';

type Props = {
    status?: string;
    canResetPassword: boolean;
    canRegister: boolean;
};

export default function Login({
    status,
    canResetPassword,
    canRegister,
}: Props) {
    const [showPassword, setShowPassword] = useState(false);
    const [remember, setRemember] = useState(false);
    return (
        <AuthLayout
            title="Connectez-vous en tant qu'administrateur"
            description="Accédez à votre tableau de bord et gérez vos Entreprises"
            showLogo={true}
        >
            <Head title="admin-login" />

            <div className="w-full max-w-md mx-auto">
                {/* En-tête avec logo et message de bienvenue */}
                <div className="text-center mb-8">
                                        
                </div>

                {/* Bannière de status */}
                {status && (
                    <div className="mb-6 rounded-xl bg-gradient-to-r from-green-50 to-emerald-50 border border-green-200 p-4 text-center dark:from-green-900/20 dark:to-emerald-900/20 dark:border-green-700">
                        <div className="flex items-center justify-center gap-2">
                            <svg className="h-5 w-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="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
                            </svg>
                            <span className="text-sm font-medium text-green-700 dark:text-green-300">
                                {status}
                            </span>
                        </div>
                    </div>
                )}

                {/* Formulaire de connexion */}
                <div className="relative overflow-hidden rounded-2xl border border-gray-200 bg-white p-8 shadow-2xl dark:border-gray-700 dark:bg-gray-800">
                    {/* Effets décoratifs */}
                    <div className="absolute -top-20 -right-20 h-40 w-40 rounded-full bg-gradient-to-br from-red-500/10 to-transparent blur-3xl"></div>
                    <div className="absolute -bottom-20 -left-20 h-40 w-40 rounded-full bg-gradient-to-tr from-blue-500/10 to-transparent blur-3xl"></div>
                    
                    <Form
                        {...store.form()}
                        resetOnSuccess={['password']}
                        className="relative z-10 flex flex-col gap-6"
                    >
                        {({ processing, errors }) => (
                            <>
                                {/* Champ email/téléphone */}
                                <div className="space-y-3">
                                    <div className="flex items-center justify-between">
                                        <Label htmlFor="email" className="text-sm font-medium text-gray-700 dark:text-gray-300">
                                            <span className="flex items-center gap-2">
                                                <svg className="h-4 w-4 text-blue-600 dark:text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M3 8l7.89 4.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
                                                </svg>
                                                Email ou numéro de téléphone
                                            </span>
                                        </Label>
                                        {errors.email && (
                                            <div className="flex items-center gap-1">
                                                <svg className="h-4 w-4 text-red-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.698-.833-2.464 0L4.732 16.5c-.77.833.192 2.5 1.732 2.5h13.856" />
                                                </svg>
                                            </div>
                                        )}
                                    </div>
                                    <div className="relative">
                                        <Input
                                            id="email"
                                            type="text"
                                            name="email"
                                            required
                                            autoFocus
                                            tabIndex={1}
                                            autoComplete="username"
                                            placeholder="email@example.com ou 6XXXXXXXX"
                                            className={`h-12 rounded-lg border-2 bg-gray-50 px-4 pl-12 text-gray-900 transition-all duration-300 focus:border-blue-500 focus:bg-white focus:ring-2 focus:ring-blue-200 dark:bg-gray-700 dark:text-white dark:focus:border-blue-400 dark:focus:ring-blue-500/20 ${
                                                errors.email 
                                                    ? 'border-red-300 dark:border-red-500' 
                                                    : 'border-gray-300 dark:border-gray-600'
                                            }`}
                                        />
                                        <div className="absolute left-4 top-1/2 -translate-y-1/2">
                                            <svg className="h-5 w-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
                                            </svg>
                                        </div>
                                    </div>
                                    <InputError message={errors.email} className="text-sm" />
                                </div>

                                {/* Champ mot de passe */}
                                <div className="space-y-3">
                                    <div className="flex items-center justify-between">
                                        <Label htmlFor="password" className="text-sm font-medium text-gray-700 dark:text-gray-300">
                                            <span className="flex items-center gap-2">
                                                <svg className="h-4 w-4 text-blue-600 dark:text-blue-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>
                                                Mot de passe
                                            </span>
                                        </Label>
                                        {canResetPassword && (
                                            <TextLink
                                                href={request()}
                                                className="text-sm font-medium text-blue-600 hover:text-blue-700 dark:text-blue-400 dark:hover:text-blue-300"
                                                tabIndex={5}
                                            >
                                                Mot de passe oublié ?
                                            </TextLink>
                                        )}
                                    </div>
                                    <div className="relative">
                                        <Input
                                            id="password"
                                            type={showPassword ? 'text' : 'password'}
                                            name="password"
                                            required
                                            tabIndex={2}
                                            autoComplete="current-password"
                                            placeholder="Votre mot de passe"
                                            className={`h-12 rounded-lg border-2 bg-gray-50 px-4 pl-12 pr-12 text-gray-900 transition-all duration-300 focus:border-blue-500 focus:bg-white focus:ring-2 focus:ring-blue-200 dark:bg-gray-700 dark:text-white dark:focus:border-blue-400 dark:focus:ring-blue-500/20 ${
                                                errors.password 
                                                    ? 'border-red-300 dark:border-red-500' 
                                                    : 'border-gray-300 dark:border-gray-600'
                                            }`}
                                        />
                                        <div className="absolute left-4 top-1/2 -translate-y-1/2">
                                            <svg className="h-5 w-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z" />
                                            </svg>
                                        </div>
                                        <button
                                            type="button"
                                            onClick={() => setShowPassword((v) => !v)}
                                            className="absolute right-4 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 focus:outline-none"
                                            tabIndex={-1}
                                            aria-label={showPassword ? 'Masquer le mot de passe' : 'Afficher le mot de passe'}
                                        >
                                            {showPassword ? (
                                                <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878a4.5 4.5 0 106.262 6.262M4.031 11.117A8.959 8.959 0 003 12c0 4.478 2.943 8.268 7 9.543 3.947-1.239 7.5-4.08 9.542-9.542" />
                                                </svg>
                                            ) : (
                                                <svg className="h-5 w-5" 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>
                                            )}
                                        </button>
                                    </div>
                                    <InputError message={errors.password} className="text-sm" />
                                </div>

                                {/* Case à cocher "Se souvenir de moi" */}
                                <div className="flex items-center space-x-3">
                                    <input
                                        type="checkbox"
                                        id="remember"
                                        name="remember"
                                        value="1"
                                        tabIndex={3}
                                        checked={remember}
                                        onChange={(e) => setRemember(e.target.checked)}
                                        className="h-5 w-5 rounded border-2 border-gray-300 accent-blue-600 focus:ring-2 focus:ring-blue-200 dark:border-gray-600 dark:accent-blue-500"
                                    />
                                    <Label htmlFor="remember" className="text-gray-700 dark:text-gray-300 cursor-pointer">
                                        Se souvenir de moi
                                    </Label>
                                </div>

                                {/* Bouton de connexion */}
                                <Button
                                    type="submit"
                                    className="group relative h-12 w-full rounded-lg bg-gradient-to-r from-red-600 to-red-500 text-white shadow-lg transition-all duration-300 hover:from-red-700 hover:to-red-600 hover:shadow-xl hover:scale-[1.02] active:scale-[0.98]"
                                    tabIndex={4}
                                    disabled={processing}
                                    data-test="login-button"
                                >
                                    {processing ? (
                                        <div className="flex items-center justify-center gap-2">
                                            <Spinner className="h-5 w-5" />
                                            <span>Connexion en cours...</span>
                                        </div>
                                    ) : (
                                        <div className="flex items-center justify-center gap-2">
                                            <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M11 16l-4-4m0 0l4-4m-4 4h14m-5 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h7a3 3 0 013 3v1" />
                                            </svg>
                                            <span className="font-semibold">Se connecter</span>
                                        </div>
                                    )}
                                </Button>

                                {/* Bouton aller à l'accueil */}
                                <Link
                                    href={home.url()}
                                    className="flex items-center justify-center gap-2 h-11 w-full rounded-lg border-2 border-gray-300 bg-white text-gray-700 transition-all duration-200 hover:border-gray-400 hover:bg-gray-50 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:hover:border-gray-500 dark:hover:bg-gray-600"
                                    tabIndex={6}
                                >
                                    <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6" />
                                    </svg>
                                    <span className="font-medium">Aller à l'accueil</span>
                                </Link>

                                {/* Lien d'inscription */}
                                
                            </>
                        )}
                    </Form>
                </div>

                {/* Section d'informations supplémentaires */}
                <div className="mt-8 space-y-4">
                    {/* Bandeau de sécurité */}
                    <div className="rounded-xl border border-blue-200 bg-blue-50 p-4 dark:border-blue-700 dark:bg-blue-900/20">
                        <div className="flex items-center gap-3">
                            <div className="flex h-10 w-10 items-center justify-center rounded-lg bg-blue-100 dark:bg-blue-800">
                                <svg className="h-5 w-5 text-blue-600 dark:text-blue-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>
                                <p className="text-sm font-medium text-blue-800 dark:text-blue-300">
                                    Connexion sécurisée
                                </p>
                                <p className="text-xs text-blue-600 dark:text-blue-400">
                                    Vos informations sont protégées par chiffrement SSL
                                </p>
                            </div>
                        </div>
                    </div>

                    {/* Options de connexion alternatives */}
                    
                </div>
            </div>
        </AuthLayout>
    );
}