import request from './request'

// 登录
export const login = (data) => request.post('/admin/login', data)

// 当前用户权限（用于菜单与路由）
export const getMyPermissions = () => request.get('/admin/my-permissions')

// 角色权限管理
export const getRolePermissionsList = () => request.get('/admin/role-permissions')
export const createRole = (data) => request.post('/admin/role-permissions', data)
export const updateRolePermissions = (roleId, data) => request.put(`/admin/role-permissions/${roleId}`, data)
export const deleteRole = (roleId) => request.delete(`/admin/role-permissions/${roleId}`)

// 后台注册（手机号+验证码）
export const sendCode = (mobile) => request.post('/admin/send-code', { mobile })
export const register = (data) => request.post('/admin/register', data)
// 密码找回
export const resetPassword = (data) => request.post('/admin/reset-password', data)

// 统计
export const getStats = () => request.get('/admin/stats')

// 用户
export const getUsers = (params) => request.get('/admin/users', { params })
export const getUserById = (id) => request.get(`/admin/users/${id}`)
export const createUser = (data) => request.post('/admin/users', data)
export const updateUser = (id, data) => request.put(`/admin/users/${id}`, data)
export const deleteUser = (id) => request.delete(`/admin/users/${id}`)

// 工作空间
export const getWorkspaces = (params) => request.get('/admin/workspaces', { params })

// 对话
export const getConversations = (params) => request.get('/admin/conversations', { params })

// 短信平台配置
export const getSMSConfig = () => request.get('/admin/sms-config')
export const updateSMSConfig = (data) => request.put('/admin/sms-config', data)

// 支付配置（微信、支付宝）
export const getPaymentConfig = () => request.get('/admin/payment-config')
export const updatePaymentConfig = (data) => request.put('/admin/payment-config', data)

// 官网站点
export const getOfficialSite = () => request.get('/admin/system/official-site')
export const setOfficialSite = (siteId) => request.put('/admin/system/official-site', { site_id: siteId })

// 站点管理
export const getSites = () => request.get('/admin/sites')
export const getSiteById = (id) => request.get(`/admin/sites/${id}`)
export const createSite = (data) => request.post('/admin/sites', data)
export const updateSite = (id, data) => request.put(`/admin/sites/${id}`, data)
export const deleteSite = (id) => request.delete(`/admin/sites/${id}`)

// 网页管理
export const getPages = (params) => request.get('/admin/pages', { params })
export const getPageById = (id) => request.get(`/admin/pages/${id}`)
export const createPage = (data) => request.post('/admin/pages', data)
export const updatePage = (id, data) => request.put(`/admin/pages/${id}`, data)
export const deletePage = (id) => request.delete(`/admin/pages/${id}`)

// 首页编辑与下载
export const getHomepage = (siteId) => request.get(`/admin/sites/${siteId}/homepage`)
export const updateHomepage = (siteId, data) => request.put(`/admin/sites/${siteId}/homepage`, data)
export const downloadHomepage = (siteId) => request.get(`/admin/sites/${siteId}/homepage/download`, { responseType: 'blob' })
export const getDownloadableAssets = (siteId) => request.get(`/admin/sites/${siteId}/assets/downloadable`)

// 文件管理（功能模块：多级目录、可下载）
export const getSiteAssets = (siteId, path, opts = {}) => {
  const params = {}
  if (path) params.path = path
  if (opts.downloadable) params.downloadable = '1'
  return request.get(`/admin/sites/${siteId}/assets`, { params })
}
export const uploadSiteAsset = (siteId, file, opts = {}) => {
  const form = new FormData()
  form.append('file', file)
  if (opts.folder != null) form.append('folder', opts.folder)
  form.append('downloadable', opts.downloadable ? 'true' : 'false')
  return request.post(`/admin/sites/${siteId}/assets`, form, { headers: { 'Content-Type': 'multipart/form-data' } })
}
export const createSiteFolder = (siteId, path) => request.post(`/admin/sites/${siteId}/folders`, { path })
export const deleteSiteAsset = (siteId, id) => request.delete(`/admin/sites/${siteId}/assets/${id}`)
