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 { 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;
    canRegister: boolean;
};

export default function Login({
    status,
    canResetPassword,
    canRegister,
}: Props) {
    return (
        <>
            <Head title="Log in" />
            <Head>
                <link rel="preconnect" href="https://fonts.googleapis.com" />
                <link rel="preconnect" href="https://fonts.gstatic.com" crossOrigin="" />
                <link
                    href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;500;600&display=swap"
                    rel="stylesheet"
                />
            </Head>

            <Form
                {...store.form()}
                resetOnSuccess={['password']}
                className="flex flex-col gap-6"
            >
                {({ processing, errors }) => (
                    <>
                        {(status || Object.keys(errors).length > 0) && (
                            <div
                                className={`rounded-lg border px-4 py-3 text-sm leading-5 ${
                                    Object.keys(errors).length > 0
                                        ? 'border-[#7F1D1D] bg-[#450A0A] text-[#FECACA]'
                                        : 'border-[#166534] bg-[#052E16] text-[#BBF7D0]'
                                }`}
                                role="status"
                                aria-live="polite"
                            >
                                {Object.keys(errors).length > 0
                                    ? 'Sign-in failed. Check your work email and password, then try again.'
                                    : status}
                            </div>
                        )}

                        <div className="grid gap-5">
                            <div className="grid gap-2">
                                <Label
                                    htmlFor="email"
                                    className="text-[13px] leading-4.5 font-medium text-[#E2E8F0]"
                                >
                                    Work email
                                </Label>
                                <Input
                                    id="email"
                                    type="email"
                                    name="email"
                                    required
                                    autoFocus
                                    tabIndex={1}
                                    autoComplete="email"
                                    placeholder="email@example.com"
                                    className="h-11 rounded-lg border-[#334155] bg-[#0B1220] text-[#F8FAFC] placeholder:text-[#94A3B8] transition-[color,box-shadow,border-color] duration-150 ease-out hover:border-[#475569] focus-visible:border-[#60A5FA] focus-visible:ring-2 focus-visible:ring-[#38BDF8]/40"
                                    aria-invalid={Boolean(errors.email)}
                                />
                                <InputError message={errors.email} />
                            </div>

                            <div className="grid gap-2">
                                <div className="flex items-center">
                                    <Label
                                        htmlFor="password"
                                        className="text-[13px] leading-4.5 font-medium text-[#E2E8F0]"
                                    >
                                        Password
                                    </Label>
                                    {canResetPassword && (
                                        <TextLink
                                            href={request()}
                                            className="ml-auto text-[13px] leading-4.5 text-[#60A5FA] hover:text-[#93C5FD]"
                                            tabIndex={5}
                                        >
                                            Forgot your password?
                                        </TextLink>
                                    )}
                                </div>
                                <PasswordInput
                                    id="password"
                                    name="password"
                                    required
                                    tabIndex={2}
                                    autoComplete="current-password"
                                    placeholder="Password"
                                    className="h-11 rounded-lg border-[#334155] bg-[#0B1220] text-[#F8FAFC] placeholder:text-[#94A3B8] transition-[color,box-shadow,border-color] duration-150 ease-out hover:border-[#475569] focus-visible:border-[#60A5FA] focus-visible:ring-2 focus-visible:ring-[#38BDF8]/40"
                                    aria-invalid={Boolean(errors.password)}
                                />
                                <InputError message={errors.password} />
                            </div>

                            <div className="flex items-center space-x-3">
                                <Checkbox
                                    id="remember"
                                    name="remember"
                                    tabIndex={3}
                                    className="border-[#334155] data-[state=checked]:border-[#2563EB] data-[state=checked]:bg-[#2563EB]"
                                />
                                <Label
                                    htmlFor="remember"
                                    className="text-[13px] leading-4.5 text-[#94A3B8]"
                                >
                                    Keep me signed in on this secure device
                                </Label>
                            </div>

                            <Button
                                type="submit"
                                className="mt-1 h-11 w-full rounded-lg bg-[#2563EB] text-sm font-semibold text-white shadow-sm transition-colors duration-150 ease-out hover:bg-[#1D4ED8] focus-visible:ring-2 focus-visible:ring-[#38BDF8]/40"
                                tabIndex={4}
                                disabled={processing}
                                data-test="login-button"
                            >
                                {processing && <Spinner />}
                                Sign in
                            </Button>
                        </div>

                        {canRegister && (
                            <div className="text-center text-[13px] leading-4.5 text-[#94A3B8]">
                                Need access?{' '}
                                <TextLink
                                    href={register()}
                                    tabIndex={5}
                                    className="text-[#60A5FA] hover:text-[#93C5FD]"
                                >
                                    Request account access
                                </TextLink>
                            </div>
                        )}
                    </>
                )}
            </Form>
        </>
    );
}

Login.layout = {
    title: 'Sign in to Support Desk',
    description: 'Access tickets, SLAs, and customer updates in one place.',
};
