import { Form, Head } from '@inertiajs/react';
import { KeyRound } from 'lucide-react';
import { useRef } from 'react';
import SecurityController from '@/actions/App/Http/Controllers/Settings/SecurityController';
import { Field, FormActions } from '@/components/crm/form-field';
import { SectionCard } from '@/components/crm/page-header';
import PasswordInput from '@/components/password-input';
import { Button } from '@/components/ui/button';
import { edit } from '@/routes/security';
import type { Props as ManagePasskeysProps } from '@/components/manage-passkeys';
import ManagePasskeys from '@/components/manage-passkeys';
import type { Props as ManageTwoFactorProps } from '@/components/manage-two-factor';
import ManageTwoFactor from '@/components/manage-two-factor';

// oxfmt-ignore
type Props = {
    passwordRules: string;
} & ManagePasskeysProps &
    ManageTwoFactorProps;

export default function Security(props: Props) {
    const passwordInput = useRef<HTMLInputElement>(null);
    const currentPasswordInput = useRef<HTMLInputElement>(null);

    return (
        <div className="flex flex-col gap-5">
            <Head title="Security settings" />

            <h1 className="sr-only">Security settings</h1>

            <SectionCard
                icon={KeyRound}
                tone="slate"
                title="Update password"
                description="Ensure your account is using a long, random password to stay secure."
            >
                <Form
                    {...SecurityController.update.form()}
                    options={{
                        preserveScroll: true,
                    }}
                    resetOnError={[
                        'password',
                        'password_confirmation',
                        'current_password',
                    ]}
                    resetOnSuccess
                    onError={(errors) => {
                        if (errors.password) {
                            passwordInput.current?.focus();
                        }

                        if (errors.current_password) {
                            currentPasswordInput.current?.focus();
                        }
                    }}
                    className="flex flex-col gap-5"
                >
                    {({ errors, processing }) => (
                        <>
                            <Field
                                label="Current password"
                                htmlFor="current_password"
                                hint="Confirm it is you"
                                error={errors.current_password ?? null}
                                required
                            >
                                <PasswordInput
                                    id="current_password"
                                    ref={currentPasswordInput}
                                    name="current_password"
                                    className="w-full"
                                    autoComplete="current-password"
                                    placeholder="Current password"
                                />
                            </Field>

                            <Field
                                label="New password"
                                htmlFor="password"
                                error={errors.password ?? null}
                                required
                            >
                                <PasswordInput
                                    id="password"
                                    ref={passwordInput}
                                    name="password"
                                    className="w-full"
                                    autoComplete="new-password"
                                    placeholder="New password"
                                    passwordrules={props.passwordRules}
                                />
                            </Field>

                            <Field
                                label="Confirm password"
                                htmlFor="password_confirmation"
                                error={errors.password_confirmation ?? null}
                                required
                            >
                                <PasswordInput
                                    id="password_confirmation"
                                    name="password_confirmation"
                                    className="w-full"
                                    autoComplete="new-password"
                                    placeholder="Confirm password"
                                    passwordrules={props.passwordRules}
                                />
                            </Field>

                            <FormActions note="You stay signed in on this device after changing your password.">
                                <Button
                                    disabled={processing}
                                    data-test="update-password-button"
                                >
                                    Save
                                </Button>
                            </FormActions>
                        </>
                    )}
                </Form>
            </SectionCard>

            <ManageTwoFactor
                canManageTwoFactor={props.canManageTwoFactor}
                requiresConfirmation={props.requiresConfirmation}
                twoFactorEnabled={props.twoFactorEnabled}
            />

            <ManagePasskeys
                canManagePasskeys={props.canManagePasskeys}
                passkeys={props.passkeys}
            />
        </div>
    );
}

Security.layout = {
    breadcrumbs: [
        {
            title: 'Security settings',
            href: edit(),
        },
    ],
};
