import InputError from '@/Components/InputError';
import PrimaryButton from '@/Components/PrimaryButton';
import TextInput from '@/Components/TextInput';
import { Head, Link, useForm } from '@inertiajs/react';
import { FormEventHandler, useEffect } from 'react';
import { motion } from 'framer-motion';
import { Lock, Mail, ShieldCheck, ArrowRight } from 'lucide-react';

export default function ResetPassword({
    token,
    email,
}: {
    token: string;
    email: string;
}) {
    const { data, setData, post, processing, errors, reset } = useForm({
        token: token,
        email: email,
        password: '',
        password_confirmation: '',
    });

    useEffect(() => {
        return () => {
            reset('password', 'password_confirmation');
        };
    }, []);

    const submit: FormEventHandler = (e) => {
        e.preventDefault();
        post(route('password.store'));
    };

    return (
        <div className="flex min-h-screen bg-background">
            <Head title="Reset Password" />

            {/* Left Side: Visual */}
            <div className="relative hidden w-1/2 lg:block">
                <img 
                    src="https://images.unsplash.com/photo-1451187530220-43a0f1e9c21d?auto=format&fit=crop&q=80" 
                    alt="Reset Visual" 
                    className="absolute inset-0 h-full w-full object-cover"
                />
                <div className="absolute inset-0 bg-primary/20 backdrop-blur-[2px]" />
                <div className="absolute inset-0 flex flex-col justify-end p-20 text-white">
                    <motion.div
                        initial={{ opacity: 0, y: 20 }}
                        animate={{ opacity: 1, y: 0 }}
                        transition={{ duration: 0.8, delay: 0.2 }}
                    >
                        <h2 className="font-display text-6xl font-bold leading-tight">
                            Identity <br />
                            <span className="italic text-accent">restored.</span>
                        </h2>
                        <p className="mt-6 max-w-md text-lg text-cream/80">
                            Create a strong, new password for your Altivate administrative account. Your security is our highest priority.
                        </p>
                    </motion.div>
                </div>
            </div>

            {/* Right Side: Form */}
            <div className="flex w-full flex-col justify-center px-8 lg:w-1/2 lg:px-24">
                <div className="mx-auto w-full max-w-md">
                    <motion.div
                        initial={{ opacity: 0, x: 20 }}
                        animate={{ opacity: 1, x: 0 }}
                        transition={{ duration: 0.6 }}
                    >
                        <Link href="/" className="mb-10 inline-flex items-center gap-2">
                            <span className="h-3 w-3 rounded-full bg-accent animate-pulse" />
                            <span className="font-display text-2xl font-bold">Altivate<span className="text-accent">.</span></span>
                        </Link>

                        <div className="mb-10">
                            <h1 className="font-display text-4xl font-bold tracking-tight">Set new password</h1>
                            <p className="mt-2 text-muted-foreground">Please choose a secure password you haven't used before.</p>
                        </div>

                        <form onSubmit={submit} className="space-y-6">
                            <div className="space-y-2">
                                <label className="eyebrow ml-1" htmlFor="email">Email Address</label>
                                <div className="relative">
                                    <div className="absolute inset-y-0 left-0 flex items-center pl-4 pointer-events-none text-muted-foreground">
                                        <Mail className="h-4 w-4" />
                                    </div>
                                    <TextInput
                                        id="email"
                                        type="email"
                                        name="email"
                                        value={data.email}
                                        className="block w-full pl-11 h-12 rounded-2xl border-border bg-secondary/30 focus:bg-background transition-all"
                                        autoComplete="username"
                                        onChange={(e) => setData('email', e.target.value)}
                                        required
                                    />
                                </div>
                                <InputError message={errors.email} className="mt-1 ml-1" />
                            </div>

                            <div className="space-y-2">
                                <label className="eyebrow ml-1" htmlFor="password">New Password</label>
                                <div className="relative">
                                    <div className="absolute inset-y-0 left-0 flex items-center pl-4 pointer-events-none text-muted-foreground">
                                        <Lock className="h-4 w-4" />
                                    </div>
                                    <TextInput
                                        id="password"
                                        type="password"
                                        name="password"
                                        value={data.password}
                                        className="block w-full pl-11 h-12 rounded-2xl border-border bg-secondary/30 focus:bg-background transition-all"
                                        autoComplete="new-password"
                                        isFocused={true}
                                        placeholder="••••••••"
                                        onChange={(e) => setData('password', e.target.value)}
                                        required
                                    />
                                </div>
                                <InputError message={errors.password} className="mt-1 ml-1" />
                            </div>

                            <div className="space-y-2">
                                <label className="eyebrow ml-1" htmlFor="password_confirmation">Confirm Password</label>
                                <div className="relative">
                                    <div className="absolute inset-y-0 left-0 flex items-center pl-4 pointer-events-none text-muted-foreground">
                                        <ShieldCheck className="h-4 w-4" />
                                    </div>
                                    <TextInput
                                        id="password_confirmation"
                                        type="password"
                                        name="password_confirmation"
                                        value={data.password_confirmation}
                                        className="block w-full pl-11 h-12 rounded-2xl border-border bg-secondary/30 focus:bg-background transition-all"
                                        autoComplete="new-password"
                                        placeholder="••••••••"
                                        onChange={(e) => setData('password_confirmation', e.target.value)}
                                        required
                                    />
                                </div>
                                <InputError message={errors.password_confirmation} className="mt-1 ml-1" />
                            </div>

                            <PrimaryButton 
                                className="w-full h-12 rounded-2xl bg-primary text-primary-foreground hover:bg-accent flex items-center justify-center gap-2 group transition-all" 
                                disabled={processing}
                            >
                                {processing ? 'Updating...' : (
                                    <>
                                        Reset Password <ArrowRight className="h-4 w-4 group-hover:translate-x-1 transition-transform" />
                                    </>
                                )}
                            </PrimaryButton>
                        </form>

                        <p className="mt-10 text-center text-xs text-muted-foreground">
                            © {new Date().getFullYear()} Altivate Solutions. All rights reserved.
                        </p>
                    </motion.div>
                </div>
            </div>
        </div>
    );
}
