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

export default function Login({
    status,
    canResetPassword,
}: {
    status?: string;
    canResetPassword: boolean;
}) {
    const { data, setData, post, processing, errors, reset } = useForm({
        email: '',
        password: '',
        remember: false as boolean,
    });

    const submit: FormEventHandler = (e) => {
        e.preventDefault();
        post(route('login'), {
            onFinish: () => reset('password'),
        });
    };

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

            {/* Left Side: Visual */}
            <div className="relative hidden w-1/2 lg:block">
                <img 
                    src="/storage/login_visual.png" 
                    alt="Login Visual" 
                    className="absolute inset-0 h-full w-full object-cover"
                    onError={(e) => {
                        // Fallback if image not yet in storage
                        e.currentTarget.src = 'https://images.unsplash.com/photo-1557683316-973673baf926?auto=format&fit=crop&q=80';
                    }}
                />
                <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">
                            Elevate your <br />
                            <span className="italic text-accent">perspective.</span>
                        </h2>
                        <p className="mt-6 max-w-md text-lg text-cream/80">
                            Access the Altivate Solutions command center. Manage your store, bookings, and growth strategies in one place.
                        </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">Welcome back</h1>
                            <p className="mt-2 text-muted-foreground">Please enter your credentials to access your account.</p>
                        </div>

                        {status && (
                            <div className="mb-6 rounded-2xl bg-emerald-50 p-4 text-sm font-medium text-emerald-600">
                                {status}
                            </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"
                                        isFocused={true}
                                        placeholder="admin@altivate.com"
                                        onChange={(e) => setData('email', e.target.value)}
                                    />
                                </div>
                                <InputError message={errors.email} className="mt-1 ml-1" />
                            </div>

                            <div className="space-y-2">
                                <div className="flex items-center justify-between">
                                    <label className="eyebrow ml-1" htmlFor="password">Password</label>
                                    {canResetPassword && (
                                        <Link
                                            href={route('password.request')}
                                            className="text-xs text-muted-foreground hover:text-accent transition-colors"
                                        >
                                            Forgot password?
                                        </Link>
                                    )}
                                </div>
                                <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="current-password"
                                        placeholder="••••••••"
                                        onChange={(e) => setData('password', e.target.value)}
                                    />
                                </div>
                                <InputError message={errors.password} className="mt-1 ml-1" />
                            </div>

                            <div className="flex items-center">
                                <label className="flex items-center group cursor-pointer">
                                    <input
                                        type="checkbox"
                                        name="remember"
                                        checked={data.remember}
                                        onChange={(e) => setData('remember', e.target.checked)}
                                        className="rounded border-border text-primary focus:ring-primary h-4 w-4 transition-colors"
                                    />
                                    <span className="ms-3 text-sm text-muted-foreground group-hover:text-foreground transition-colors">
                                        Remember me for 30 days
                                    </span>
                                </label>
                            </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 ? 'Signing in...' : (
                                    <>
                                        Sign in <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>
    );
}

