import { ModalType, useModalStore } from "~/stores/modal-store";
import { Button } from "../ui/button";
import {
    Dialog,
    DialogContent,
    DialogDescription,
    DialogFooter,
    DialogHeader,
    DialogTitle,
} from "../ui/dialog";

export default function DeleteServerConfirmModal() {
    const { type, data, isOpen, onClose } = useModalStore();

    const isModalOpen = type === ModalType.DELETE_SERVER_CONFIRM && isOpen;

    const onOpenChange = () => {
        onClose();
    };

    const onConfirm = async () => {
        await import("~/lib/api/client/server").then((m) =>
            m.default.delet((data as { serverId: string }).serverId),
        );
        onClose();
    };

    return (
        <Dialog open={isModalOpen} onOpenChange={onOpenChange}>
            <DialogContent className="sm:max-w-md">
                <DialogHeader>
                    <DialogTitle>Are you sure?</DialogTitle>
                    <DialogDescription>
                        This action cannot be undone.
                    </DialogDescription>
                </DialogHeader>

                <DialogFooter className="justify-between">
                    <Button variant="default" onClick={onClose}>
                        No
                    </Button>
                    <Button variant="destructive" onClick={onConfirm}>
                        Yes
                    </Button>
                </DialogFooter>
            </DialogContent>
        </Dialog>
    );
}
