import { Form } from '@inertiajs/react';
import { useRef } from 'react';
import ProfileController from '@/actions/App/Http/Controllers/Settings/ProfileController';
import Heading from '@/components/heading';
import InputError from '@/components/input-error';
import PasswordInput from '@/components/password-input';
import { Button } from '@/components/ui/button';
import {
    Dialog,
    DialogBody,
    DialogClose,
    DialogContent,
    DialogDescription,
    DialogIconClose,
    DialogFooter,
    DialogHeader,
    DialogTitle,
    DialogTrigger,
} from '@/components/ui/dialog';
import { Label } from '@/components/ui/label';
import { useI18n } from '@/hooks/use-i18n';

export default function DeleteUser() {
    const { t } = useI18n();
    const passwordInput = useRef<HTMLInputElement>(null);

    return (
        <div className="space-y-6">
            <Heading
                variant="small"
                title={t('settings.profile.delete.heading_title')}
                description={t('settings.profile.delete.heading_description')}
            />
            <div className="space-y-4 rounded-lg border border-red-100 bg-red-50 p-4 dark:border-red-200/10 dark:bg-red-700/10">
                <div className="relative space-y-0.5 text-red-600 dark:text-red-100">
                    <p className="font-medium">{t('settings.profile.delete.warning_title')}</p>
                    <p className="text-sm">
                        {t('settings.profile.delete.warning_body')}
                    </p>
                </div>

                <Dialog>
                    <DialogTrigger asChild>
                        <Button
                            variant="destructive"
                            data-test="delete-user-button"
                        >
                            {t('settings.profile.delete.open_button')}
                        </Button>
                    </DialogTrigger>
                    <DialogContent className="gap-0">
                        <DialogHeader>
                            <div className="flex items-start justify-between gap-3">
                                <div className="min-w-0">
                                    <DialogTitle>
                                        {t('settings.profile.delete.dialog_title')}
                                    </DialogTitle>
                                    <DialogDescription>
                                        {t('settings.profile.delete.dialog_description')}
                                    </DialogDescription>
                                </div>
                                <DialogIconClose />
                            </div>
                        </DialogHeader>

                        <Form
                            {...ProfileController.destroy.form()}
                            options={{
                                preserveScroll: true,
                            }}
                            onError={() => passwordInput.current?.focus()}
                            resetOnSuccess
                            className="contents"
                        >
                            {({ resetAndClearErrors, processing, errors }) => (
                                <>
                                    <DialogBody className="space-y-2">
                                        <Label
                                            htmlFor="password"
                                            className="sr-only"
                                        >
                                            {t('settings.profile.delete.password_label')}
                                        </Label>

                                        <PasswordInput
                                            id="password"
                                            name="password"
                                            ref={passwordInput}
                                            placeholder={t('settings.profile.delete.password_placeholder')}
                                            autoComplete="current-password"
                                        />

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

                                    <DialogFooter className="gap-2">
                                        <DialogClose asChild>
                                            <Button
                                                variant="secondary"
                                                onClick={() =>
                                                    resetAndClearErrors()
                                                }
                                            >
                                                {t('settings.profile.delete.cancel')}
                                            </Button>
                                        </DialogClose>

                                        <Button
                                            variant="destructive"
                                            disabled={processing}
                                            asChild
                                        >
                                            <button
                                                type="submit"
                                                data-test="confirm-delete-user-button"
                                            >
                                                {t('settings.profile.delete.confirm')}
                                            </button>
                                        </Button>
                                    </DialogFooter>
                                </>
                            )}
                        </Form>
                    </DialogContent>
                </Dialog>
            </div>
        </div>
    );
}
