/**
 * @description 资源推荐管理API接口
 * @filename resourceRecommend.ts
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-31
 */

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

/**
 * 推荐类型枚举
 */
export enum RecommendType {
  /** 热门资源推荐 */
  HOT = 1,
  /** 思政资源推荐 */
  IDEOLOGICAL = 2
}

/**
 * 资源推荐API服务
 */
export const resourceRecommendApi = {
  /**
   * 获取推荐列表
   * @returns Promise<ResultDomain<ResourceRecommendVO>>
   */
  async getRecommendList(): Promise<ResultDomain<ResourceRecommendVO>> {
    const response = await api.get<ResourceRecommendVO>('/news/recommends/list');
    return response.data;
  },

  /**
   * 根据推荐类型获取推荐资源列表
   * @param recommendType 推荐类型（1-热门资源，2-思政资源）
   * @param limit 限制数量
   * @returns Promise<ResultDomain<ResourceRecommendVO>>
   */
  async getRecommendsByType(recommendType: number, limit?: number): Promise<ResultDomain<ResourceRecommendVO>> {
    const params = limit ? { limit } : null;
    const response = await api.get<ResourceRecommendVO>(`/news/recommends/type/${recommendType}`, params);
    return response.data;
  },

  /**
   * 分页查询推荐资源列表
   * @param filter 筛选条件
   * @param pageParam 分页参数
   * @returns Promise<ResultDomain<ResourceRecommendVO>>
   */
  async getRecommendPage(pageParam: PageParam, filter?: any): Promise<ResultDomain<ResourceRecommendVO>> {
    const response = await api.post<ResourceRecommendVO>('/news/recommends/page', {
      pageParam,
      filter,
    });
    return response.data;
  },

  /**
   * 根据ID获取推荐详情
   * @param recommendID 推荐ID
   * @returns Promise<ResultDomain<ResourceRecommendVO>>
   */
  async getRecommendById(recommendID: string): Promise<ResultDomain<ResourceRecommendVO>> {
    const response = await api.get<ResourceRecommendVO>(`/news/recommends/recommend/${recommendID}`);
    return response.data;
  },

  /**
   * 创建推荐
   * @param recommend 推荐信息
   * @returns Promise<ResultDomain<any>>
   */
  async createRecommend(recommend: any): Promise<ResultDomain<any>> {
    const response = await api.post<any>('/news/recommends/recommend', recommend);
    return response.data;
  },

  /**
   * 批量添加推荐资源
   * @param resourceIDs 资源ID列表
   * @param recommendType 推荐类型
   * @param reason 推荐理由（可选）
   * @returns Promise<ResultDomain<any>>
   */
  async batchAddRecommends(resourceIDs: string[], recommendType: number, reason?: string): Promise<ResultDomain<any>> {
    const response = await api.post<any>('/news/recommends/recommend/batch', {
      resourceIDs,
      recommendType,
      reason
    });
    return response.data;
  },

  /**
   * 更新推荐
   * @param recommend 推荐信息
   * @returns Promise<ResultDomain<any>>
   */
  async updateRecommend(recommend: any): Promise<ResultDomain<any>> {
    const response = await api.put<any>('/news/recommends/recommend', recommend);
    return response.data;
  },

  /**
   * 删除推荐
   * @param recommendID 推荐ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteRecommend(recommendID: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`/news/recommends/recommend/${recommendID}`);
    return response.data;
  },

  /**
   * 更新推荐排序
   * @param recommendID 推荐ID
   * @param orderNum 排序号
   * @returns Promise<ResultDomain<any>>
   */
  async updateRecommendOrder(recommendID: string, orderNum: number): Promise<ResultDomain<any>> {
    const response = await api.put<any>(`/news/recommends/recommend/${recommendID}/order`, null, {
      params: { orderNum }
    });
    return response.data;
  },

  /**
   * 检查资源是否已推荐（按类型）
   * @param resourceID 资源ID
   * @param recommendType 推荐类型
   * @returns Promise<ResultDomain<boolean>>
   */
  async isResourceRecommendedByType(resourceID: string, recommendType: number): Promise<ResultDomain<boolean>> {
    const response = await api.get<boolean>(`/news/recommends/check/${resourceID}`, {
      recommendType
    });
    return response.data;
  },

  /**
   * 统计推荐资源总数
   * @param filter 筛选条件
   * @returns Promise<ResultDomain<number>>
   */
  async countRecommends(filter?: any): Promise<ResultDomain<number>> {
    const response = await api.post<number>('/news/recommends/count', filter);
    return response.data;
  }
};

export default resourceRecommendApi;

