import { Form, Head } from '@inertiajs/react';
import InputError from '@/components/input-error';
import PasswordInput from '@/components/password-input';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Spinner } from '@/components/ui/spinner';

type Props = {
    email: string;
    submitUrl: string;
};

export default function SetPassword({ email, submitUrl }: Props) {
    return (
        <>
            <Head title="Set your password" />

            <Form
                action={submitUrl}
                method="post"
                resetOnSuccess={['password', 'password_confirmation']}
                className="flex flex-col gap-6"
            >
                {({ processing, errors }) => (
                    <>
                        <div className="grid gap-2">
                            <Label htmlFor="email">Email address</Label>
                            <Input
                                id="email"
                                type="email"
                                value={email}
                                readOnly
                                disabled
                            />
                        </div>

                        <div className="grid gap-2">
                            <Label htmlFor="password">
                                Password (min 12 characters)
                            </Label>
                            <PasswordInput
                                id="password"
                                name="password"
                                required
                                autoFocus
                                autoComplete="new-password"
                                placeholder="Choose a strong password"
                            />
                            <InputError message={errors.password} />
                        </div>

                        <div className="grid gap-2">
                            <Label htmlFor="password_confirmation">
                                Confirm password
                            </Label>
                            <PasswordInput
                                id="password_confirmation"
                                name="password_confirmation"
                                required
                                autoComplete="new-password"
                                placeholder="Repeat your password"
                            />
                        </div>

                        <Button type="submit" disabled={processing}>
                            {processing && <Spinner />}
                            Set password &amp; continue
                        </Button>
                    </>
                )}
            </Form>
        </>
    );
}

SetPassword.layout = {
    title: 'Welcome to BAMS Legal',
    description: 'Set a password to activate your account',
};
