/**
 * @description 资源分类API接口
 * @filename resourceCategory.ts
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-15
 */

import { api } from '@/apis';
import type { ResultDomain, ResourceCategory } from '@/types';

/**
 * 资源分类API服务
 */
export const resourceCategoryApi = {
  /**
   * 获取分类列表
   * @returns Promise<ResultDomain<ResourceCategory>>
   */
  async getCategoryList(): Promise<ResultDomain<ResourceCategory>> {
    const response = await api.get<ResourceCategory>('/news/categorys/list');
    return response.data;
  },

  /**
   * 根据ID获取分类详情
   * @param categoryID 分类ID
   * @returns Promise<ResultDomain<ResourceCategory>>
   */
  async getCategoryById(categoryID: string): Promise<ResultDomain<ResourceCategory>> {
    const response = await api.get<ResourceCategory>(`/news/categorys/category/${categoryID}`);
    return response.data;
  },

  /**
   * 创建分类
   * @param category 分类信息
   * @returns Promise<ResultDomain<ResourceCategory>>
   */
  async createCategory(category: ResourceCategory): Promise<ResultDomain<ResourceCategory>> {
    const response = await api.post<ResourceCategory>('/news/categorys/category', category);
    return response.data;
  },

  /**
   * 更新分类
   * @param category 分类信息
   * @returns Promise<ResultDomain<ResourceCategory>>
   */
  async updateCategory(category: ResourceCategory): Promise<ResultDomain<ResourceCategory>> {
    const response = await api.put<ResourceCategory>('/news/categorys/category', category);
    return response.data;
  },

  /**
   * 删除分类
   * @param categoryID 分类ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteCategory(categoryID: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`/news/categorys/category/${categoryID}`);
    return response.data;
  },

  /**
   * 更新分类状态
   * @param categoryID 分类ID
   * @param status 状态值
   * @returns Promise<ResultDomain<ResourceCategory>>
   */
  async updateCategoryStatus(categoryID: string, status: number): Promise<ResultDomain<ResourceCategory>> {
    const response = await api.put<ResourceCategory>(`/news/categorys/category/${categoryID}/status`, null, {
      params: { status }
    });
    return response.data;
  },

  /**
   * 获取分类树
   * @returns Promise<ResultDomain<ResourceCategory>>
   */
  async getCategoryTree(): Promise<ResultDomain<ResourceCategory>> {
    const response = await api.get<ResourceCategory>('/news/categorys/tree');
    return response.data;
  },

  /**
   * 获取子分类
   * @param parentID 父分类ID
   * @returns Promise<ResultDomain<ResourceCategory>>
   */
  async getChildCategories(parentID: string): Promise<ResultDomain<ResourceCategory>> {
    const response = await api.get<ResourceCategory>(`/news/categorys/category/${parentID}/children`);
    return response.data;
  }
};

export default resourceCategoryApi;
