import { Head } from '@inertiajs/react';
import type { FormEvent } from 'react';
import InputError from '@/components/InputError';
import PasswordInput from '@/components/PasswordInput';
import TextLink from '@/components/TextLink';
import { Button } from '@/components/ui/button';
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 { useYupForm } from '@/hooks/use-yup-form';
import { loginSchema } from '@/lib/validation/auth';
import { store } from '@/routes/login';
import { request } from '@/routes/password';

export default function Login() {
    const { data, setData, processing, errors, validateField, submit } =
        useYupForm(loginSchema, {
            nik: '',
            password: '',
            remember: false,
        });

    const handleSubmit = (e: FormEvent) => {
        e.preventDefault();

        submit((form) =>
            form.post(store().url, {
                onFinish: () => form.reset('password'),
            }),
        );
    };

    return (
        <>
            <Head title="Log in" />

            <form
                onSubmit={handleSubmit}
                className="flex flex-col gap-6"
                noValidate
            >
                <div className="grid gap-6">
                    <div className="grid gap-2">
                        <Label htmlFor="nik">NIK</Label>
                        <Input
                            id="nik"
                            type="text"
                            name="nik"
                            value={data.nik}
                            onChange={(e) => setData('nik', e.target.value)}
                            onBlur={() => validateField('nik')}
                            required
                            autoFocus
                            tabIndex={1}
                            autoComplete="username"
                            placeholder="Masukkan NIK Anda"
                        />
                        <InputError message={errors.nik} />
                    </div>

                    <div className="grid gap-2">
                        <div className="flex items-center justify-between">
                            <Label htmlFor="current-password">Password</Label>
                            {/* NIK untuk masuk, email untuk pemulihan (K5) —
                                karena itu tautannya menuju form yang meminta
                                email, bukan NIK. */}
                            <TextLink
                                href={request()}
                                className="text-sm"
                                tabIndex={5}
                            >
                                Forgot password?
                            </TextLink>
                        </div>
                        <PasswordInput
                            id="current-password"
                            name="password"
                            value={data.password}
                            onChange={(e) =>
                                setData('password', e.target.value)
                            }
                            onBlur={() => validateField('password')}
                            required
                            tabIndex={2}
                            autoComplete="current-password"
                            placeholder="Password"
                        />
                        <InputError message={errors.password} />
                    </div>

                    <div className="flex items-center space-x-3">
                        <Checkbox
                            id="remember"
                            name="remember"
                            checked={data.remember}
                            onCheckedChange={(checked) =>
                                setData('remember', checked === true)
                            }
                            tabIndex={3}
                        />
                        <Label htmlFor="remember">Remember me</Label>
                    </div>

                    <Button
                        type="submit"
                        className="mt-4 w-full"
                        tabIndex={4}
                        disabled={processing}
                        data-test="login-button"
                    >
                        {processing && <Spinner />}
                        Log in
                    </Button>
                </div>
            </form>
        </>
    );
}

Login.layout = {
    title: 'Log in to your account',
    description: 'Enter your NIK and password below to log in',
};
