/**
 * 文章分类管理服务层
 * 提供分类列表、保存、删除等接口
 */

// 分类数据结构
export interface CategoryItem {
  id: number
  name: string
  description: string
  image: string
  article_count: number
  sort: number
  status: number // 0: 禁用, 1: 启用
  created_at: string
  updated_at: string
}

// 分类创建/编辑参数
export interface CategoryPayload {
  name: string
  description: string
  image: string
  sort: number
  status: number
}

/**
 * 获取分类列表
 * @param params 查询参数 { page, limit, keyword, status }
 * @returns Promise<{ items: CategoryItem[], total: number }>
 */
export function getCategoryList(params: any = {}): Promise<any> {
  // TODO: 替换为实际 API 调用
  // return uni.$http.get('/article/category/list', { params })
  
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        items: [
          {
            id: 1,
            name: '运营指南',
            description: '关于商城运营的各类指南和教程',
            image: '/static/category-1.png',
            article_count: 12,
            sort: 1,
            status: 1,
            created_at: '2026-01-15 10:00:00',
            updated_at: '2026-01-20 15:30:00'
          },
          {
            id: 2,
            name: '营销技巧',
            description: '营销活动策略和最佳实践',
            image: '/static/category-2.png',
            article_count: 8,
            sort: 2,
            status: 1,
            created_at: '2026-01-15 11:00:00',
            updated_at: '2026-01-19 14:20:00'
          },
          {
            id: 3,
            name: '常见问题',
            description: '常见问题解答和故障排除',
            image: '/static/category-3.png',
            article_count: 5,
            sort: 3,
            status: 1,
            created_at: '2026-01-15 12:00:00',
            updated_at: '2026-01-18 09:45:00'
          },
          {
            id: 4,
            name: '产品更新',
            description: '产品更新日志和新功能介绍',
            image: '/static/category-4.png',
            article_count: 3,
            sort: 4,
            status: 0,
            created_at: '2026-01-16 10:00:00',
            updated_at: '2026-01-17 16:00:00'
          }
        ],
        total: 4
      })
    }, 300)
  })
}

/**
 * 获取分类详情
 * @param id 分类ID
 * @returns Promise<CategoryItem>
 */
export function getCategoryDetail(id: number): Promise<CategoryItem> {
  // TODO: 替换为实际 API 调用
  // return uni.$http.get(`/article/category/${id}`)
  
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        id,
        name: '运营指南',
        description: '关于商城运营的各类指南和教程',
        image: '/static/category-1.png',
        article_count: 12,
        sort: 1,
        status: 1,
        created_at: '2026-01-15 10:00:00',
        updated_at: '2026-01-20 15:30:00'
      })
    }, 300)
  })
}

/**
 * 保存分类（新建或编辑）
 * @param data 分类数据
 * @param id 分类ID（编辑时传入）
 * @returns Promise<{ success: boolean, message: string, id?: number }>
 */
export function saveCategory(data: CategoryPayload, id?: number): Promise<any> {
  // TODO: 替换为实际 API 调用
  // const method = id ? 'PUT' : 'POST'
  // const url = id ? `/article/category/${id}` : '/article/category'
  // return uni.$http[method === 'PUT' ? 'put' : 'post'](url, data)
  
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        success: true,
        message: id ? '编辑成功' : '新建成功',
        id: id || Math.floor(Math.random() * 10000)
      })
    }, 500)
  })
}

/**
 * 删除分类
 * @param id 分类ID
 * @returns Promise<{ success: boolean, message: string }>
 */
export function deleteCategory(id: number): Promise<any> {
  // TODO: 替换为实际 API 调用
  // return uni.$http.delete(`/article/category/${id}`)
  
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        success: true,
        message: '删除成功'
      })
    }, 300)
  })
}

/**
 * 启用/禁用分类
 * @param id 分类ID
 * @param status 状态 (0: 禁用, 1: 启用)
 * @returns Promise<{ success: boolean, message: string }>
 */
export function toggleCategoryStatus(id: number, status: number): Promise<any> {
  // TODO: 替换为实际 API 调用
  // return uni.$http.put(`/article/category/${id}/status`, { status })
  
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        success: true,
        message: status === 1 ? '启用成功' : '禁用成功'
      })
    }, 300)
  })
}
