import axiosApi from '@/utils/axios.js'



export const groupingId = (data) => axiosApi.get(`/api/markdown/grouping/${data}`)
// 获取所有分组
export const groupingAll = (data) => axiosApi.get(`/api/groupings?parentId=${data}`);
// 获取所有Markdown文件
export const markdownAll = () => axiosApi.get(`/api/markdown`);
// 预览markdown文件
export const Preview = (id) => axiosApi.get(`/api/markdown/${id}`);

// 创建分类分组
export const addGroupings = (group) => {
    return axiosApi.post('/api/groupings', group);
}
//更新Markdown文件
export const  updateMarkdown = (data) => {
    return axiosApi.post(`/api/markdown/updateMarkdown`, data)
}
// 批量删除图片
export const deleteImages = (list) => {
    const formData = new FormData()
    formData.append('urls', list)
    return axiosApi.post('/api/images/batch', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}
// 上传图片
export const uploadImage = (file, userId, markdownId) => {
    const formData = new FormData()
    if (file) formData.append('file', file)
    if (userId) formData.append('userId', userId)
    if (markdownId) formData.append('markdownId', markdownId)
    return axiosApi.post('/api/images', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 删除Markdown文件
export const deleteMarkdown = (id) => axiosApi.delete(`/api/markdown/${id}`);
// 根据分组ID获取Markdown文件列表
export const markdownList = (groupingId) => axiosApi.get(`/api/markdown/grouping/${groupingId}`);

// 登录
export const login = (data) => {
    const formData = new FormData()
    formData.append('username', data.username)
    formData.append('password', data.password)
    return axiosApi.post('/api/user/login', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 搜索
export const searchMarkdown = (keyword) => axiosApi.get(`/api/markdown/search?keyword=${keyword}`);

// 注册
export const register = (data) => {
    const formData = new FormData()
    formData.append('username', data.username)
    formData.append('password', data.password)
    formData.append('registrationCode', data.registrationCode)
    return axiosApi.post('/api/user/register', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
        
    })
}

// 更新分组名称
export const updateGroupingName = (id, newName) => {
    return axiosApi.put(`/api/groupings/${id}`, { grouping: newName });
}

// 删除分组
export const deleteGrouping = (id) => axiosApi.delete(`/api/groupings/${id}`);

// 更新Markdown文件标题
export const updateMarkdownTitle = (id, newName) => {
    const formData = new FormData()
    if (newName) formData.append('title', newName)
    return axiosApi.post(`/api/markdown/${id}/title`,formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    });
}

// 获取最近更新的笔记
export const getRecentFiles = () => axiosApi.get('/api/markdown/recent');





// MD5哈希
export const MD5 = (data, file) => {
    const formData = new FormData()
    if (data) formData.append('input', data)
    if (file) formData.append('file', file)
    return axiosApi.post('/api/common/md5', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}









// 获取回收站内容
export const getTrash = () => axiosApi.get('/api/trash');

// 恢复项目
export const restoreTrashItem = (id, type) => axiosApi.post(`/api/trash/restore/${type}/${id}`);

// 彻底删除
export const permanentlyDeleteItem = (id, type) => axiosApi.delete(`/api/trash/permanently/${type}/${id}`);

// 清空回收站
export const cleanTrash = () => axiosApi.delete('/api/trash/clean');

// 验证Token
export const validateToken = () => axiosApi.post('/api/user/validate-token');

// System APIs
export const getRegistrationStatus = () => {
    return axiosApi.get('/api/system/registration/status');
};

export const toggleRegistration = (enabled) => {
    return axiosApi.post('/api/system/registration/toggle', enabled, {
        headers: {
            'Content-Type': 'application/json'
        }
    });
};

export const generateRegistrationCode = () => {
    return axiosApi.post('/api/system/registration/generate-code');
};

// 更新密码
export const updatePassword = (data) => {
    return axiosApi.put('/api/user/password', data);
};
