import { NSQLITE_TURBOCI_ADMIN_USERS } from "@/src/db/types";
import { APIReqObject } from "@/src/types";
import Loading from "@/twui/components/elements/Loading";
import useStatus from "@/twui/components/hooks/useStatus";
import Button from "@/twui/components/layout/Button";
import fetchApi from "@moduletrace/datasquirel/dist/client/fetch";
import { APIResponseObject } from "@moduletrace/nsqlite/dist/types";
import { ComponentProps, ReactNode } from "react";

type Props = {
    dep_user: NSQLITE_TURBOCI_ADMIN_USERS;
    target?: ReactNode;
    button_props?: Omit<ComponentProps<typeof Button>, "title">;
};

export default function DeleteDeplotmentUserButton({
    dep_user,
    target: passed_target,
    button_props,
}: Props) {
    const { loading, setLoading } = useStatus();

    const target = (
        <Button title="Delete Deployment User" {...button_props}>
            Delete User
        </Button>
    );

    if (dep_user.is_super_admin) {
        return null;
    }

    return (
        <div
            onClick={() => {
                if (!window.confirm(`Delete User?`)) {
                    return;
                }

                setLoading(true);

                fetchApi<APIReqObject, APIResponseObject>(
                    `/api/admin/delete-user`,
                    {
                        method: "POST",
                        body: {
                            user_id: dep_user.id,
                        },
                    },
                ).then((res) => {
                    console.log(res);
                });
            }}
        >
            {loading ? <Loading /> : passed_target || target}
        </div>
    );
}
