import { Form, Head } from '@inertiajs/react';
import InputError from '@/components/input-error';
import PasswordInput from '@/components/password-input';
import TextLink from '@/components/text-link';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Spinner } from '@/components/ui/spinner';
import { login } from '@/routes';
import { store } from '@/routes/register';

type Props = {
    passwordRules?: string;
};

export default function Register({ passwordRules }: Props) {
    return (
        <Card className="w-full max-w-md border-slate-100 shadow-sm">
            <CardHeader className="pb-4 pt-6 px-8">
                <CardTitle className="text-xl font-medium text-slate-800">Create your account</CardTitle>
                <CardDescription className="text-slate-500 text-sm">
                    Fill in your details to join your firm's workspace.
                </CardDescription>
            </CardHeader>
            <CardContent className="px-8">
                <Head title="Create account" />

                <Form
                    {...store.form()}
                    resetOnSuccess={['password', 'password_confirmation']}
                    disableWhileProcessing
                    className="flex flex-col gap-4"
                >
                    {({ processing, errors }) => (
                        <>
                            <div className="grid gap-1.5">
                                <Label htmlFor="name" className="text-slate-600 text-sm">Full name</Label>
                                <Input
                                    id="name"
                                    name="name"
                                    required
                                    autoFocus
                                    tabIndex={1}
                                    autoComplete="name"
                                    placeholder="Ada Okafor"
                                    className="border-slate-200 focus:border-slate-400 focus:ring-slate-300"
                                />
                                <InputError message={errors.name} />
                            </div>

                            <div className="grid gap-1.5">
                                <Label htmlFor="email" className="text-slate-600 text-sm">Email address</Label>
                                <Input
                                    id="email"
                                    type="email"
                                    name="email"
                                    required
                                    tabIndex={2}
                                    autoComplete="email"
                                    placeholder="you@lawfirm.com"
                                    className="border-slate-200 focus:border-slate-400 focus:ring-slate-300"
                                />
                                <InputError message={errors.email} />
                            </div>

                            <div className="grid gap-1.5">
                                <Label htmlFor="password" className="text-slate-600 text-sm">Password</Label>
                                <PasswordInput
                                    id="password"
                                    name="password"
                                    required
                                    tabIndex={3}
                                    autoComplete="new-password"
                                    placeholder="At least 12 characters"
                                    passwordrules={passwordRules}
                                    className="border-slate-200 focus:border-slate-400 focus:ring-slate-300"
                                />
                                <InputError message={errors.password} />
                            </div>

                            <div className="grid gap-1.5">
                                <Label htmlFor="password_confirmation" className="text-slate-600 text-sm">
                                    Confirm password
                                </Label>
                                <PasswordInput
                                    id="password_confirmation"
                                    name="password_confirmation"
                                    required
                                    tabIndex={4}
                                    autoComplete="new-password"
                                    placeholder="Repeat your password"
                                    className="border-slate-200 focus:border-slate-400 focus:ring-slate-300"
                                />
                                <InputError
                                    message={errors.password_confirmation}
                                />
                            </div>

                            <Button
                                type="submit"
                                className="w-full bg-slate-800 hover:bg-slate-700 text-white"
                                size="lg"
                                tabIndex={5}
                                disabled={processing}
                                data-test="register-button"
                            >
                                {processing && <Spinner />}
                                Create account
                            </Button>
                        </>
                    )}
                </Form>
            </CardContent>
            <CardFooter className="flex flex-col gap-5 border-t border-slate-100 bg-slate-50 py-5 rounded-b-xl">
                <p className="text-sm text-slate-600">
                    Already have an account?{' '}
                    <TextLink href={login()} tabIndex={6} className="font-semibold text-slate-800 hover:text-slate-600">
                        Log in
                    </TextLink>
                </p>

                <p className="rounded-lg bg-slate-100 px-4 py-3 text-center text-xs text-slate-600">
                    New accounts join your firm with no access until an
                    administrator grants a role.
                </p>
            </CardFooter>
        </Card>
    );
}

Register.layout = {
    title: 'Create your account',
    description: 'Join your firm’s workspace in a few seconds',
};
