import type { FormDataConvertible } from '@inertiajs/core';
import { Head, useForm } from '@inertiajs/react';
import { FormEventHandler } from 'react';

import { Button } from '@admin/components/ui/button';
import { FormError } from '@admin/components/ui/form-error';
import { Input } from '@admin/components/ui/input';
import { Label } from '@admin/components/ui/label';
import { TextLink } from '@admin/components/ui/text-link';
import AuthLayout from '@admin/layouts/auth-layout';

interface RegisterForm extends Record<string, FormDataConvertible> {
    name: string;
    email: string;
    password: string;
    password_confirmation: string;
}

export default function Register() {
    const { data, setData, post, processing, errors, reset } = useForm<RegisterForm>({
        name: '',
        email: '',
        password: '',
        password_confirmation: '',
    });

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

    return (
        <AuthLayout
            title="Sign up"
            description={
                <>
                    Already have an account? <TextLink href={route('login')}>Sign in here</TextLink>
                </>
            }
        >
            <Head title="Register" />

            <form onSubmit={submit}>
                <div className="grid gap-y-4">
                    <div>
                        <Label htmlFor="name">Full Name</Label>
                        <Input
                            id="name"
                            type="text"
                            required
                            autoFocus
                            autoComplete="name"
                            value={data.name}
                            onChange={(e) => setData('name', e.target.value)}
                        />
                        <FormError message={errors.name} />
                    </div>

                    <div>
                        <Label htmlFor="email">Email address</Label>
                        <Input
                            id="email"
                            type="email"
                            required
                            autoComplete="email"
                            value={data.email}
                            onChange={(e) => setData('email', e.target.value)}
                        />
                        <FormError message={errors.email} />
                    </div>

                    <div>
                        <Label htmlFor="password">Password</Label>
                        <Input
                            id="password"
                            type="password"
                            required
                            autoComplete="new-password"
                            value={data.password}
                            onChange={(e) => setData('password', e.target.value)}
                        />
                        <FormError message={errors.password} />
                    </div>

                    <div>
                        <Label htmlFor="password_confirmation">Confirm Password</Label>
                        <Input
                            id="password_confirmation"
                            type="password"
                            required
                            autoComplete="new-password"
                            value={data.password_confirmation}
                            onChange={(e) => setData('password_confirmation', e.target.value)}
                        />
                        <FormError message={errors.password_confirmation} />
                    </div>

                    <Button type="submit" className="w-full" disabled={processing}>
                        {processing ? 'Creating account…' : 'Sign up'}
                    </Button>
                </div>
            </form>
        </AuthLayout>
    );
}
