import React, { useState } from 'react';
import { Form, Head, Link, usePage } from '@inertiajs/react';
import InputError from '@/components/input-error';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Spinner } from '@/components/ui/spinner';
import { store } from '@/routes/register';
import { User, Mail, Lock, UserPlus, AlertCircle, Eye, EyeOff } from 'lucide-react';

type Props = {
    passwordRules: string;
};

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


    if (!allowRegistration) {
        return (
            <>
                <Head title="Registration Disabled" />
                <div className="text-center space-y-6">
                    <div className="w-12 h-12 rounded-2xl bg-amber-500/10 border border-amber-500/30 flex items-center justify-center mx-auto text-amber-400">
                        <AlertCircle className="w-6 h-6" />
                    </div>
                    <div className="space-y-2">
                        <h2 className="text-lg font-bold text-white">Registration Disabled</h2>
                        <p className="text-xs text-slate-400">
                            Public user registration is currently turned off by the system administrator.
                        </p>
                    </div>
                    <Link
                        href="/login"
                        className="btn-aqua inline-flex items-center justify-center gap-2 px-6 py-2.5 rounded-xl text-xs font-bold w-full"
                    >
                        Return to Login
                    </Link>
                </div>
            </>
        );
    }

    return (
        <>
            <Head title="Create Account" />
            <Form
                {...store.form()}
                resetOnSuccess={['password', 'password_confirmation']}
                disableWhileProcessing
                className="space-y-4"
            >
                {({ processing, errors }) => (
                    <>
                        <div className="space-y-4">
                            <div>
                                <Label htmlFor="name" className="text-xs font-bold text-slate-300 uppercase tracking-wider block mb-1">
                                    Full Name *
                                </Label>
                                <div className="relative">
                                    <Input
                                        id="name"
                                        type="text"
                                        required
                                        autoFocus
                                        tabIndex={1}
                                        autoComplete="name"
                                        name="name"
                                        placeholder="Khalequzzaman"
                                        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]"
                                    />
                                    <User className="w-4 h-4 text-slate-500 absolute left-3.5 top-3" />
                                </div>
                                <InputError message={errors.name} className="mt-1" />
                            </div>

                            <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"
                                        required
                                        tabIndex={2}
                                        autoComplete="email"
                                        name="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>
                                <Label htmlFor="password" className="text-xs font-bold text-slate-300 uppercase tracking-wider block mb-1">
                                    Password *
                                </Label>
                                <div className="relative">
                                    <Input
                                        id="password"
                                        type={showPassword ? 'text' : 'password'}
                                        required
                                        tabIndex={3}
                                        autoComplete="new-password"
                                        name="password"
                                        placeholder="Minimum 8 characters"
                                        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>
                                <Label htmlFor="password_confirmation" className="text-xs font-bold text-slate-300 uppercase tracking-wider block mb-1">
                                    Confirm Password *
                                </Label>
                                <div className="relative">
                                    <Input
                                        id="password_confirmation"
                                        type={showConfirmPassword ? 'text' : 'password'}
                                        required
                                        tabIndex={4}
                                        autoComplete="new-password"
                                        name="password_confirmation"
                                        placeholder="Re-enter password"
                                        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={() => setShowConfirmPassword(!showConfirmPassword)}
                                        className="absolute right-3.5 top-3 text-slate-400 hover:text-white transition-colors focus:outline-none"
                                        tabIndex={-1}
                                        aria-label={showConfirmPassword ? 'Hide password' : 'Show password'}
                                    >
                                        {showConfirmPassword ? <EyeOff className="w-4 h-4 text-[#00F2FE]" /> : <Eye className="w-4 h-4" />}
                                    </button>
                                </div>
                                <InputError message={errors.password_confirmation} className="mt-1" />
                            </div>


                            <button
                                type="submit"
                                tabIndex={5}
                                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 />}
                                <UserPlus className="w-4 h-4" />
                                Create Admin Account
                            </button>
                        </div>

                        <div className="text-center text-xs text-slate-400 pt-2 border-t border-slate-800/60">
                            Already registered?{' '}
                            <Link href="/login" tabIndex={6} className="text-[#00F2FE] font-semibold hover:underline">
                                Log in
                            </Link>
                        </div>
                    </>
                )}
            </Form>
        </>
    );
}

Register.layout = {
    title: 'Create Account',
    description: 'Register a new administrative user account for Tech in Soul',
};
