import { useContext } from "react";
import { AppContext } from "@/src/pages/_app";
import Button from "@/twui/components/layout/Button";
import LucideIcon from "@/twui/components/elements/lucide-icon";
import useStatus from "@/twui/components/hooks/useStatus";
import downloadFile from "@/src/utils/download-file";

export default function DownloadPrivateSSHKeyButton() {
    const { pageProps } = useContext(AppContext);
    const { deployment_user, deployment } = pageProps;

    const { loading, setLoading } = useStatus();

    if (!deployment_user?.id) {
        return null;
    }

    const is_super_admin = Boolean(deployment_user.is_super_admin);

    return (
        <Button
            title="Download Private Key"
            variant="outlined"
            beforeIcon={<LucideIcon name="Download" size={17} />}
            loading={loading}
            onClick={() => {
                setLoading(true);

                downloadFile({
                    file_name: `${deployment_user.username}-private-key`,
                    url: `/api/admin/download-private-ssh-key?user_id=${deployment_user.id}`,
                })
                    .then((res) => {})
                    .finally(() => {
                        setTimeout(() => {
                            setLoading(false);
                        }, 2000);
                    });
            }}
        >
            Download Private Key
        </Button>
    );
}
