import { Form, Head } from '@inertiajs/react';
import InputError from '@/components/input-error';
import PasskeyVerify from '@/components/passkey-verify';
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 { Checkbox } from '@/components/ui/checkbox';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Spinner } from '@/components/ui/spinner';
import { register } from '@/routes';
import { store } from '@/routes/login';
import { request } from '@/routes/password';

type Props = {
    status?: string;
    canResetPassword: boolean;
};

export default function Login({ status, canResetPassword }: 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">Welcome back</CardTitle>
                <CardDescription className="text-slate-500 text-sm">
                    Enter your credentials to continue.
                </CardDescription>
            </CardHeader>
            <CardContent className="px-8 space-y-5">
                <Head title="Log in" />

                {status && (
                    <div className="rounded-md bg-slate-50 px-4 py-3 text-center text-sm font-medium text-slate-700 border border-slate-200">
                        {status}
                    </div>
                )}

                <PasskeyVerify />

                <div className="relative">
                    <div className="absolute inset-0 flex items-center">
                        <span className="w-full border-t border-slate-200" />
                    </div>
                    <div className="relative flex justify-center text-xs uppercase">
                        <span className="bg-white px-3 text-slate-400 font-medium">Or</span>
                    </div>
                </div>

                <Form
                    {...store.form()}
                    resetOnSuccess={['password']}
                    className="flex flex-col gap-4"
                >
                    {({ processing, errors }) => (
                        <>
                            <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
                                    autoFocus
                                    tabIndex={1}
                                    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">
                                <div className="flex items-center justify-between">
                                    <Label htmlFor="password" className="text-slate-600 text-sm">Password</Label>
                                    {canResetPassword && (
                                        <TextLink
                                            href={request()}
                                            className="text-xs text-slate-500 hover:text-slate-800"
                                            tabIndex={5}
                                        >
                                            Forgot?
                                        </TextLink>
                                    )}
                                </div>
                                <PasswordInput
                                    id="password"
                                    name="password"
                                    required
                                    tabIndex={2}
                                    autoComplete="current-password"
                                    placeholder="••••••••••••"
                                    className="border-slate-200 focus:border-slate-400 focus:ring-slate-300"
                                />
                                <InputError message={errors.password} />
                            </div>

                            <label className="flex items-center gap-2 text-sm text-slate-600 select-none">
                                <Checkbox
                                    id="remember"
                                    name="remember"
                                    tabIndex={3}
                                />
                                Keep me signed in
                            </label>

                            <Button
                                type="submit"
                                className="w-full bg-slate-800 hover:bg-slate-700 text-white"
                                size="lg"
                                tabIndex={4}
                                disabled={processing}
                                data-test="login-button"
                            >
                                {processing && <Spinner />}
                                Sign in
                            </Button>
                        </>
                    )}
                </Form>
            </CardContent>
            <CardFooter className="justify-center border-t border-slate-100 bg-slate-50 py-5 rounded-b-xl">
                <p className="text-sm text-slate-600">
                    New to the firm?{' '}
                    <TextLink href={register()} tabIndex={6} className="font-semibold text-slate-800 hover:text-slate-600">
                        Create an account
                    </TextLink>
                </p>
            </CardFooter>
        </Card>
    );
}

Login.layout = {
    title: 'Welcome back',
    description: 'Sign in to continue to your workspace',
};
