"use client";

import {
    Dialog,
    DialogClose,
    DialogContent,
    DialogDescription,
    DialogFooter,
    DialogHeader,
    DialogTitle,
    DialogTrigger
} from "@/components/ui/dialog";
import {Button} from "@/components/ui/button";
import {signOut} from "@/lib/backend/authentication";
import {useRouter} from "next/navigation";
import {HOME_URL} from "@/lib/constants";

export default function LogoutDialog() {

    const router = useRouter()

    const logoutUser = async () => {
        const response = await signOut();
        if (response?.code === 200) {
            await router.push(HOME_URL)
        }
    }
    return (
        <Dialog>
            <DialogTrigger asChild>
                <Button variant={"destructive"}>Logout</Button>
            </DialogTrigger>
            <DialogContent className="sm:max-w-md">
                <DialogHeader>
                    <DialogTitle>Sign Out</DialogTitle>
                    <DialogDescription>
                        Are you sure you want to sign out and exit? You'll have to login again to access your
                        dashboard.
                    </DialogDescription>
                </DialogHeader>
                <DialogFooter className="sm:justify-end">
                    <Button type="button" variant={"destructive"} onClick={logoutUser}>
                        Sign out
                    </Button>

                    <DialogClose asChild>
                        <Button type="button" variant="secondary">
                            Close
                        </Button>
                    </DialogClose>
                </DialogFooter>
            </DialogContent>
        </Dialog>
    )
}