import React, { useState } from 'react';
import { Form, Head, Link, usePage } from '@inertiajs/react';
import InputError from '@/components/input-error';
import PasskeyVerify from '@/components/passkey-verify';
import { Button } from '@/components/ui/button';
import { Checkbox } from '@/components/ui/checkbox';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Spinner } from '@/components/ui/spinner';
import { store } from '@/routes/login';
import { request } from '@/routes/password';
import { ArrowRight, Lock, Mail, Eye, EyeOff } from 'lucide-react';

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

export default function Login({ status, canResetPassword }: Props) {
    const { globalSettings } = usePage().props as any;
    const allowRegistration = globalSettings?.allow_registration !== 'false';
    const [showPassword, setShowPassword] = useState(false);


    return (
        <>
            <Head title="Log in" />

            <PasskeyVerify />

            {status && (
                <div className="p-3 rounded-xl bg-emerald-500/10 border border-emerald-500/30 text-emerald-400 text-xs font-semibold text-center">
                    {status}
                </div>
            )}

            <Form
                {...store.form()}
                resetOnSuccess={['password']}
                className="space-y-4"
            >
                {({ processing, errors }) => (
                    <>
                        <div className="space-y-4">
                            <div>
                                <Label htmlFor="email" className="text-xs font-bold text-slate-300 uppercase tracking-wider block mb-1">
                                    Email Address
                                </Label>
                                <div className="relative">
                                    <Input
                                        id="email"
                                        type="email"
                                        name="email"
                                        required
                                        autoFocus
                                        tabIndex={1}
                                        autoComplete="email"
                                        placeholder="admin@techinsoul.com"
                                        className="w-full bg-[#060B18] border border-slate-800 rounded-xl px-4 py-2.5 pl-10 text-sm text-white placeholder-slate-500 focus:outline-none focus:border-[#00F2FE]"
                                    />
                                    <Mail className="w-4 h-4 text-slate-500 absolute left-3.5 top-3" />
                                </div>
                                <InputError message={errors.email} className="mt-1" />
                            </div>

                            <div>
                                <div className="flex items-center justify-between mb-1">
                                    <Label htmlFor="password" className="text-xs font-bold text-slate-300 uppercase tracking-wider">
                                        Password
                                    </Label>
                                    {canResetPassword && (
                                        <Link
                                            href={request()}
                                            className="text-xs text-[#00F2FE] hover:underline"
                                            tabIndex={5}
                                        >
                                            Forgot password?
                                        </Link>
                                    )}
                                </div>
                                <div className="relative">
                                    <Input
                                        id="password"
                                        type={showPassword ? 'text' : 'password'}
                                        name="password"
                                        required
                                        tabIndex={2}
                                        autoComplete="current-password"
                                        placeholder="••••••••"
                                        className="w-full bg-[#060B18] border border-slate-800 rounded-xl px-4 py-2.5 pl-10 pr-10 text-sm text-white placeholder-slate-500 focus:outline-none focus:border-[#00F2FE]"
                                    />
                                    <Lock className="w-4 h-4 text-slate-500 absolute left-3.5 top-3" />
                                    <button
                                        type="button"
                                        onClick={() => setShowPassword(!showPassword)}
                                        className="absolute right-3.5 top-3 text-slate-400 hover:text-white transition-colors focus:outline-none"
                                        tabIndex={-1}
                                        aria-label={showPassword ? 'Hide password' : 'Show password'}
                                    >
                                        {showPassword ? <EyeOff className="w-4 h-4 text-[#00F2FE]" /> : <Eye className="w-4 h-4" />}
                                    </button>
                                </div>

                                <InputError message={errors.password} className="mt-1" />
                            </div>

                            <div className="flex items-center space-x-2 pt-1">
                                <Checkbox
                                    id="remember"
                                    name="remember"
                                    tabIndex={3}
                                    className="border-slate-700 bg-[#060B18] data-[state=checked]:bg-[#00F2FE]"
                                />
                                <Label htmlFor="remember" className="text-xs text-slate-300 cursor-pointer">
                                    Remember me for 30 days
                                </Label>
                            </div>

                            <button
                                type="submit"
                                tabIndex={4}
                                disabled={processing}
                                className="btn-aqua w-full py-3 rounded-xl text-sm font-bold flex items-center justify-center gap-2 mt-2"
                            >
                                {processing && <Spinner />}
                                Log In to Admin Panel
                                <ArrowRight className="w-4 h-4" />
                            </button>
                        </div>

                        {allowRegistration ? (
                            <div className="text-center text-xs text-slate-400 pt-2 border-t border-slate-800/60">
                                Don't have an admin account?{' '}
                                <Link href="/register" className="text-[#00F2FE] font-semibold hover:underline">
                                    Create account
                                </Link>
                            </div>
                        ) : (
                            <div className="text-center text-[11px] text-slate-500 pt-2 border-t border-slate-800/60">
                                Registration is restricted to system administrators.
                            </div>
                        )}
                    </>
                )}
            </Form>
        </>
    );
}

Login.layout = {
    title: 'Welcome Back',
    description: 'Log in to access your Tech in Soul Control Panel',
};
