import request from './request'

/**
 * 获取分类列表（支持按类型筛选）
 * @param {string|null} type - 交易类型(Expense=0/Income=1)，null表示获取全部
 * @returns {Promise<{success: boolean, data: Array}>}
 */
export const getCategoryList = (type = null) => {
  return request({
    url: '/TransactionCategory/GetList',
    method: 'get',
    params: type !== null ? { type } : {}
  })
}

/**
 * 根据ID获取分类详情
 * @param {number} id - 分类ID
 * @returns {Promise<{success: boolean, data: object}>}
 */
export const getCategoryById = (id) => {
  return request({
    url: `/TransactionCategory/GetById/${id}`,
    method: 'get'
  })
}

/**
 * 创建分类
 * @param {object} data - 分类数据 { name, type }
 * @returns {Promise<{success: boolean, data: number}>} 返回新创建的分类ID
 */
export const createCategory = (data) => {
  return request({
    url: '/TransactionCategory/Create',
    method: 'post',
    data
  })
}

/**
 * 更新分类
 * @param {object} data - 分类数据 { id, name }
 * @returns {Promise<{success: boolean}>}
 */
export const updateCategory = (data) => {
  return request({
    url: '/TransactionCategory/Update',
    method: 'post',
    data
  })
}

/**
 * 删除分类
 * @param {number} id - 分类ID
 * @returns {Promise<{success: boolean}>}
 */
export const deleteCategory = (id) => {
  return request({
    url: '/TransactionCategory/Delete',
    method: 'post',
    params: { id }
  })
}

/**
 * 批量创建分类（用于初始化）
 * @param {Array} dataList - 分类数据数组 [{ name, type }, ...]
 * @returns {Promise<{success: boolean, data: number}>} 返回创建的数量
 */
export const batchCreateCategories = (dataList) => {
  return request({
    url: '/TransactionCategory/BatchCreate',
    method: 'post',
    data: dataList
  })
}
