import { Form, Head } from '@inertiajs/react';
import { motion } from 'framer-motion';
import InputError from '@/components/input-error';
import PasswordInput from '@/components/password-input';
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 { update } from '@/routes/password';

type Props = {
    token: string;
    email: string;
    passwordRules: string;
};

const itemVariants = {
    hidden: { opacity: 0, y: 15 },
    visible: { 
        opacity: 1, 
        y: 0, 
        transition: { duration: 0.5, ease: [0.22, 1, 0.36, 1] } 
    }
};

export default function ResetPassword({ token, email, passwordRules }: Props) {
    return (
        <>
            <Head title="Réinitialiser le mot de passe" />

            <Form
                {...update.form()}
                transform={(data) => ({ ...data, token, email })}
                resetOnSuccess={['password', 'password_confirmation']}
            >
                {({ processing, errors }) => (
                    <motion.div 
                        initial="hidden"
                        animate="visible"
                        variants={{
                            visible: {
                                transition: {
                                    staggerChildren: 0.08,
                                    delayChildren: 0.35
                                }
                            }
                        }}
                        className="grid gap-5"
                    >
                        <motion.div variants={itemVariants} className="grid gap-2">
                            <Label htmlFor="email">E-mail</Label>
                            <Input
                                id="email"
                                type="email"
                                name="email"
                                autoComplete="email"
                                value={email}
                                className="bg-muted border-none shadow-none rounded-xl py-6 px-5 focus-visible:ring-2 focus-visible:ring-primary/50 text-base transition-all duration-300 hover:bg-muted/80 focus-visible:scale-[1.01] focus-visible:-translate-y-0.5 focus-visible:shadow-md mt-1 block w-full"
                                readOnly
                            />
                            <InputError
                                message={errors.email}
                            />
                        </motion.div>

                        <motion.div variants={itemVariants} className="grid gap-2">
                            <Label htmlFor="password">Mot de passe</Label>
                            <PasswordInput
                                id="password"
                                name="password"
                                autoComplete="new-password"
                                className="bg-muted border-none shadow-none rounded-xl py-6 px-5 focus-within:ring-2 focus-within:ring-primary/50 text-base transition-all duration-300 hover:bg-muted/80 focus-within:scale-[1.01] focus-within:-translate-y-0.5 focus-within:shadow-md mt-1 block w-full"
                                autoFocus
                                placeholder="Mot de passe"
                                passwordrules={passwordRules}
                            />
                            <InputError message={errors.password} />
                        </motion.div>

                        <motion.div variants={itemVariants} className="grid gap-2">
                            <Label htmlFor="password_confirmation">
                                Confirmer le mot de passe
                            </Label>
                            <PasswordInput
                                id="password_confirmation"
                                name="password_confirmation"
                                autoComplete="new-password"
                                className="bg-muted border-none shadow-none rounded-xl py-6 px-5 focus-within:ring-2 focus-within:ring-primary/50 text-base transition-all duration-300 hover:bg-muted/80 focus-within:scale-[1.01] focus-within:-translate-y-0.5 focus-within:shadow-md mt-1 block w-full"
                                placeholder="Confirmer le mot de passe"
                                passwordrules={passwordRules}
                            />
                            <InputError
                                message={errors.password_confirmation}
                            />
                        </motion.div>

                        <motion.div variants={itemVariants}>
                            <Button
                                type="submit"
                                className="mt-4 w-full rounded-full py-6 text-base bg-primary hover:bg-primary/90 text-primary-foreground font-medium transition-all duration-300 hover:scale-[1.02] hover:-translate-y-0.5 hover:shadow-lg active:scale-[0.98]"
                                disabled={processing}
                                data-test="reset-password-button"
                            >
                                {processing && <Spinner className="mr-2" />}
                                Réinitialiser le mot de passe
                            </Button>
                        </motion.div>
                    </motion.div>
                )}
            </Form>
        </>
    );
}

ResetPassword.layout = {
    title: 'Réinitialiser le mot de passe',
    description: 'Veuillez saisir votre nouveau mot de passe ci-dessous',
};
