import {useConfig} from "../../ConfigProvider";
import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query";
import {fetch_} from "../request-utils";
import {template} from "@babel/core";
import {data} from "react-router-dom";


export const useMarkdownTemplate = (template_id) => {
    const config = useConfig();
    return useQuery({
        queryKey: ["markdown_template", template_id],
        queryFn: () => fetch_(`${config.BACKEND_HOST}/api/template/markdown/${template_id}`),
        enabled: !!template_id,
    });
};

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

export const useUpdateMarkdownTemplate = () => {
    const config = useConfig();
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: ({id, data}) => fetch_(`${config.BACKEND_HOST}/api/template/markdown/${id}`, {
            method: "PUT",
            body: JSON.stringify(data),
        }),
        onSuccess: (data) => {
            queryClient.invalidateQueries({queryKey: ["markdown_template", data.id]});
            queryClient.invalidateQueries({queryKey: ["markdown_templates"]});
        }
    });
}

export const useCreateMarkdownTemplate = () => {
    const config = useConfig();
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: (data) => fetch_(`${config.BACKEND_HOST}/api/template/markdown/`, {
            method: "POST",
            body: JSON.stringify(data),
        }),
        onSuccess: (data) => {
            queryClient.invalidateQueries({queryKey: ["markdown_template", data.id]});
            queryClient.invalidateQueries({queryKey: ["markdown_templates"]});
        }
    });
}

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

export const useSaveMarkdownTemplate = () => {
    const config = useConfig();
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: ({id, data}) => {
            const url = id
                ? `${config.BACKEND_HOST}/api/template/markdown/${id}`
                : `${config.BACKEND_HOST}/api/template/markdown/`;
            const method = id? "PUT": "POST";
            return fetch_(url, {
                method,
                body: JSON.stringify(data),
            });
        },
        onSuccess: (data) => {
            queryClient.invalidateQueries({queryKey: ["markdown_template", data.id]});
            queryClient.invalidateQueries({queryKey: ["markdown_templates"]});
        }
    });
}
