import {fetch_ } from "../request-utils"
import {useConfig} from "../../ConfigProvider";
import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query";

export const useWebhooks = () =>{
    const queryClient = useQueryClient();
    const config = useConfig();
    return useQuery({
        queryKey: ["webhooks"],
        queryFn: () => fetch_(`${config.BACKEND_HOST}/api/webhook/`),
        onSuccess: (data) => {
            if(data){
                for(const webhook of data){
                    queryClient.setQueryData({queryKey: ["webhook", data.id]}, data);
                }
            }
        }
    });
};

export const useCreateWebhook = () => {
    const config = useConfig();
    const queryClient = useQueryClient();

    return useMutation({
        mutationFn: (data) => fetch_(`${config.BACKEND_HOST}/api/webhook/`, {
            method: "POST",
            body: JSON.stringify({
                "hook_url": data
            }),
        }),
        onSuccess: () => {
            queryClient.invalidateQueries({queryKey: ["webhooks"]});
        }
    });
};

export const useUpdateWebhook = () =>{
    const config = useConfig();
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: ({id, data}) => fetch_(`${config.BACKEND_HOST}/api/webhook/${id}`, {
            method: "PATCH",
            body: JSON.stringify(data)
        }),
        onSuccess: (res) => {
            queryClient.invalidateQueries({queryKey: ["webhook", res.id]});
            queryClient.invalidateQueries({queryKey: ["webhooks"]});
        }
    });
};

export const useDeleteWebhook = () => {
    const config = useConfig();
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: (id) => fetch_(`${config.BACKEND_HOST}/api/webhook/${id}`, {
            method: "DELETE",
        }),
        onSuccess: (res, variables) => {
            queryClient.invalidateQueries({queryKey: ["webhook", variables.id]});
            queryClient.invalidateQueries({queryKey: ["webhooks"]});
        }
    });
}

export const useWebhookSettings = () => {
    const config = useConfig();
    const queryClient = useQueryClient();
    return useQuery({
        queryKey: ["webhook_setting"],
        queryFn: () => fetch_(`${config.BACKEND_HOST}/api/setting/path/webhook/`),
        onSuccess: (data) => {
            if(data){
                for(const setting of data){
                    queryClient.setQueryData({queryKey: ["webhook_setting", setting.id]}, setting);
                }
            }
        }
    });
};

export const useWebhookSetting = (setting_id) => {
    const config = useConfig();
    return useQuery({
        queryKey: ["webhook_setting", setting_id],
        queryFn: () => fetch_(`${config.BACKEND_HOST}/api/setting/path/webhook/${setting_id}`),
        enabled: !!setting_id,
    });
};


export const useCreateWebhookSetting = () => {
    const config = useConfig();
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: (data) => fetch_(`${config.BACKEND_HOST}/api/setting/path/webhook/`, {
            method: "POST",
            body: JSON.stringify(data)
        }),
        onSuccess: (res) => {
            queryClient.invalidateQueries({queryKey: ["webhook_setting", res.id]});
            queryClient.invalidateQueries({queryKey: ["webhook_setting"]});
        }
    });
};

export const useUpdateWebhookSetting = () => {
    const config = useConfig();
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: ({id, data}) => fetch_(`${config.BACKEND_HOST}/api/setting/path/webhook/${id}`, {
            method: "PATCH",
            body: JSON.stringify(data)
        }),
        onSuccess: (res, variables) => {
            queryClient.invalidateQueries({queryKey: ["webhook_setting", variables.id]});
            queryClient.invalidateQueries({queryKey: ["webhook_setting"]});
        }
    });
};


export const useDeleteWebhookSetting = () => {
    const config = useConfig();
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: (id) => fetch_(`${config.BACKEND_HOST}/api/setting/path/webhook/${id}`, {
            method: "DELETE",
        }),
        onSuccess: (res, variables) => {
            queryClient.invalidateQueries({queryKey: ["webhook_setting", variables.id]});
            queryClient.invalidateQueries({queryKey: ["webhook_setting"]});
        }
    });
};
