// Components
import { Head, router, useForm } from '@inertiajs/react';
import { LoaderCircle } from 'lucide-react';
import { FormEventHandler } from 'react';

import InputError from '@/components/input-error';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { useTranslation } from '@/hooks/use-translation';
import AuthLayout from '@/layouts/auth-layout';
import { type ConfirmWith } from '@/types';

interface ConfirmPasswordProps {
    /** False for an account that has no password here: it signs in through a provider. */
    has_password: boolean;
    /** The connected provider such an account confirms through, or null when it has none it can use. */
    confirm_with: ConfirmWith | null;
}

export default function ConfirmPassword({ has_password, confirm_with }: ConfirmPasswordProps) {
    const { t } = useTranslation();

    const { data, setData, post, processing, errors, reset } = useForm({
        password: '',
    });

    const submit: FormEventHandler = (e) => {
        e.preventDefault();

        post(route('password.confirm'), {
            onFinish: () => reset('password'),
        });
    };

    return (
        <AuthLayout
            title={t('Confirm your password')}
            description={t('This is a secure area of the application. Please confirm your password before continuing.')}
        >
            <Head title={t('Confirm password')} />

            {!has_password && confirm_with && (
                <div className="space-y-4">
                    <p className="text-muted-foreground text-sm">
                        {t('Sign in to :provider again to confirm it is you.', { provider: confirm_with.label })}
                    </p>
                    <Button className="w-full" type="button" onClick={() => router.post(route('password.confirm.provider', { provider: confirm_with.provider }))}>
                        {t('Confirm with :provider', { provider: confirm_with.label })}
                    </Button>
                </div>
            )}

            {!has_password && !confirm_with && (
                <div className="space-y-4">
                    <p className="text-muted-foreground text-sm">
                        {t('You sign in through a connected account, so there is no password here to confirm. Set one to continue.')}
                    </p>
                    <Button asChild className="w-full">
                        <a href={route('password.edit')}>{t('Set a password')}</a>
                    </Button>
                </div>
            )}

            <form onSubmit={submit} hidden={!has_password}>
                <div className="space-y-6">
                    <div className="grid gap-2">
                        <Label htmlFor="password">{t('Password')}</Label>
                        <Input
                            id="password"
                            type="password"
                            name="password"
                            placeholder={t('Password')}
                            autoComplete="current-password"
                            value={data.password}
                            autoFocus
                            onChange={(e) => setData('password', e.target.value)}
                        />

                        <InputError message={errors.password} />
                    </div>

                    <div className="flex items-center">
                        <Button className="w-full" disabled={processing}>
                            {processing && <LoaderCircle className="h-4 w-4 animate-spin" />}
                            {t('Confirm password')}
                        </Button>
                    </div>
                </div>
            </form>
        </AuthLayout>
    );
}
