import { Form } from '@inertiajs/react';
import AccountController from '@/actions/App/Http/Controllers/AccountController';
import { Button } from '@/components/ui/button';
import {
    Dialog,
    DialogClose,
    DialogContent,
    DialogDescription,
    DialogFooter,
    DialogTitle,
} from '@/components/ui/dialog';
import type { AccountRow } from '@/types/account';

type DeleteAccountDialogProps = {
    account: AccountRow;
    open: boolean;
    onOpenChange: (open: boolean) => void;
};

export function DeleteAccountDialog({
    account,
    open,
    onOpenChange,
}: DeleteAccountDialogProps) {
    return (
        <Dialog open={open} onOpenChange={onOpenChange}>
            <DialogContent>
                <DialogTitle>Delete account</DialogTitle>
                <DialogDescription>
                    Are you sure you want to delete &quot;{account.account_name}
                    &quot;? This action cannot be undone.
                </DialogDescription>

                <Form
                    {...AccountController.destroy.form(account.id)}
                    options={{
                        preserveScroll: true,
                    }}
                    onSuccess={() => onOpenChange(false)}
                    className="space-y-4"
                >
                    {({ processing }) => (
                        <DialogFooter>
                            <DialogClose asChild>
                                <Button type="button" variant="outline">
                                    Cancel
                                </Button>
                            </DialogClose>
                            <Button
                                type="submit"
                                variant="destructive"
                                disabled={processing}
                            >
                                Delete
                            </Button>
                        </DialogFooter>
                    )}
                </Form>
            </DialogContent>
        </Dialog>
    );
}
