import InputError from '@/Components/InputError';
import InputLabel from '@/Components/InputLabel';
import FormInput from '@/Components/UI/FormInput';
import Button from '@/Components/UI/Button';
import AuthLayout from '@/Layouts/AuthLayout';
import { Head, Link, useForm } from '@inertiajs/react';
import { FormEventHandler } from 'react';

export default function Register({ referralCode }: { referralCode?: string | null }) {
    const { data, setData, post, processing, errors, reset } = useForm({
        name: '',
        email: '',
        password: '',
        password_confirmation: '',
        referral_code: referralCode ?? '',
    });

    const submit: FormEventHandler = (e) => {
        e.preventDefault();

        post(route('register'), {
            onFinish: () => reset('password', 'password_confirmation'),
        });
    };

    return (
        <AuthLayout title="Create Your Account" subtitle="Begin your executive certification journey.">
            <Head title="Register" />

            {data.referral_code && (
                <div className="mb-6 rounded bg-accent-gold/10 px-4 py-2 text-sm font-medium text-accent-gold">
                    You were referred by a JGCP member — enrol in a Programme after signing up to activate it.
                </div>
            )}

            <form onSubmit={submit} className="space-y-5">
                <div>
                    <InputLabel htmlFor="name" value="Full Name" />
                    <FormInput
                        id="name"
                        name="name"
                        value={data.name}
                        className="mt-1"
                        autoComplete="name"
                        isFocused={true}
                        onChange={(e) => setData('name', e.target.value)}
                        required
                    />
                    <InputError message={errors.name} className="mt-1" />
                </div>

                <div>
                    <InputLabel htmlFor="email" value="Email" />
                    <FormInput
                        id="email"
                        type="email"
                        name="email"
                        value={data.email}
                        className="mt-1"
                        autoComplete="username"
                        onChange={(e) => setData('email', e.target.value)}
                        required
                    />
                    <InputError message={errors.email} className="mt-1" />
                </div>

                <div>
                    <InputLabel htmlFor="password" value="Password" />
                    <FormInput
                        id="password"
                        type="password"
                        name="password"
                        value={data.password}
                        className="mt-1"
                        autoComplete="new-password"
                        onChange={(e) => setData('password', e.target.value)}
                        required
                    />
                    <InputError message={errors.password} className="mt-1" />
                </div>

                <div>
                    <InputLabel htmlFor="password_confirmation" value="Confirm Password" />
                    <FormInput
                        id="password_confirmation"
                        type="password"
                        name="password_confirmation"
                        value={data.password_confirmation}
                        className="mt-1"
                        autoComplete="new-password"
                        onChange={(e) => setData('password_confirmation', e.target.value)}
                        required
                    />
                    <InputError message={errors.password_confirmation} className="mt-1" />
                </div>

                <Button type="submit" variant="primary" className="w-full" disabled={processing}>
                    Create Account
                </Button>
            </form>

            <p className="mt-8 text-center text-sm text-on-surface-variant">
                Already registered?{' '}
                <Link href={route('login')} className="font-medium text-jgcp-navy hover:underline">
                    Sign in
                </Link>
            </p>
        </AuthLayout>
    );
}
