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

import { api } from '@/apis';
import type { ResultDomain, Resource, ResourceSearchParams, PageParam, ResourceVO, UserCollection } from '@/types';

/**
 * 资源API服务
 */
export const resourceApi = {
  // ==================== 资源基础操作 ====================
  
  /**
   * 获取资源列表
   * @param filter 筛选条件
   * @returns Promise<ResultDomain<Resource>>
   */
  async getResourceList(filter?: ResourceSearchParams): Promise<ResultDomain<Resource>> {
    const response = await api.get<Resource>('/news/resources/list', filter);
    return response.data;
  },

  /**
   * 获取资源分页列表
   * @param filter 筛选条件
   * @param pageParam 分页参数
   * @returns Promise<ResultDomain<Resource>>
   */
  async getResourcePage(pageParam: PageParam, filter?: ResourceSearchParams): Promise<ResultDomain<Resource>> {
    const response = await api.post<Resource>('/news/resources/page', {
      pageParam,
      filter,
    });
    return response.data;
  },

  /**
   * 根据ID获取资源详情
   * @param resourceID 资源ID
   * @returns Promise<ResultDomain<Resource>>
   */
  async getResourceById(resourceID: string): Promise<ResultDomain<ResourceVO>> {
    const response = await api.get<ResourceVO>(`/news/resources/resource/${resourceID}`);
    return response.data;
  },

  /**
   * 创建资源
   * @param resource 资源信息
   * @returns Promise<ResultDomain<Resource>>
   */
  async createResource(resource: ResourceVO): Promise<ResultDomain<ResourceVO>> {
    const response = await api.post<ResourceVO>('/news/resources/resource', resource);
    return response.data;
  },

  /**
   * 更新资源
   * @param resource 资源信息
   * @returns Promise<ResultDomain<ResourceVO>>
   */
  async updateResource(resource: ResourceVO): Promise<ResultDomain<ResourceVO>> {
    const response = await api.put<ResourceVO>('/news/resources/resource', resource);
    return response.data;
  },

  /**
   * 删除资源
   * @param resourceID 资源ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteResource(resourceID: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`/news/resources/resource/${resourceID}`);
    return response.data;
  },

  // ==================== 资源状态操作 ====================

  /**
   * 更新资源状态
   * @param resourceID 资源ID
   * @param status 状态值（0草稿 1已发布 2下架）
   * @returns Promise<ResultDomain<Resource>>
   */
  async updateResourceStatus(resourceID: string, status: number): Promise<ResultDomain<Resource>> {
    const response = await api.put<Resource>(`/news/resources/resource/${resourceID}/status`, null, {
      params: { status }
    });
    return response.data;
  },

  /**
   * 发布资源
   * @param resourceID 资源ID
   * @returns Promise<ResultDomain<Resource>>
   */
  async publishResource(resourceID: string): Promise<ResultDomain<Resource>> {
    const response = await api.post<Resource>(`/news/resources/resource/${resourceID}/publish`);
    return response.data;
  },

  /**
   * 下架资源
   * @param resourceID 资源ID
   * @returns Promise<ResultDomain<Resource>>
   */
  async unpublishResource(resourceID: string): Promise<ResultDomain<Resource>> {
    const response = await api.post<Resource>(`/news/resources/resource/${resourceID}/unpublish`);
    return response.data;
  },

  // ==================== 资源统计操作 ====================

  /**
   * 增加浏览次数
   * @param resourceID 资源ID
   * @returns Promise<ResultDomain<Resource>>
   */
  async incrementViewCount(resourceID: string): Promise<ResultDomain<Resource>> {
    const response = await api.post<Resource>(`/news/resources/resource/${resourceID}/view`);
    return response.data;
  },

  /**
   * 增加点赞次数
   * @param resourceID 资源ID
   * @returns Promise<ResultDomain<Resource>>
   */
  async incrementLikeCount(resourceID: string): Promise<ResultDomain<Resource>> {
    const response = await api.post<Resource>(`/news/resources/resource/${resourceID}/like`);
    return response.data;
  },

  /**
   * 收藏次数增减
   * @param resourceID 资源ID
   * @returns Promise<ResultDomain<Resource>>
   */
  async resourceCollect(collect: UserCollection): Promise<ResultDomain<Resource>> {
    const response = await api.post<Resource>(`/news/resources/resource/collect`, collect);
    return response.data;
  },

  // ==================== 资源推荐和轮播操作 ====================

  /**
   * 设置资源推荐
   * @param resourceID 资源ID
   * @param isRecommend 是否推荐
   * @returns Promise<ResultDomain<Resource>>
   */
  async setResourceRecommend(resourceID: string, isRecommend: boolean): Promise<ResultDomain<Resource>> {
    const response = await api.put<Resource>(`/news/resources/resource/${resourceID}/recommend`, null, {
      params: { isRecommend }
    });
    return response.data;
  },

  /**
   * 设置资源轮播
   * @param resourceID 资源ID
   * @param isBanner 是否轮播
   * @returns Promise<ResultDomain<Resource>>
   */
  async setResourceBanner(resourceID: string, isBanner: boolean): Promise<ResultDomain<Resource>> {
    const response = await api.put<Resource>(`/news/resources/resource/${resourceID}/banner`, null, {
      params: { isBanner }
    });
    return response.data;
  },

  // ==================== 资源查询操作 ====================

  /**
   * 获取推荐资源列表
   * @param limit 限制数量
   * @returns Promise<ResultDomain<Resource>>
   */
  async getRecommendResources(limit?: number): Promise<ResultDomain<Resource>> {
    const response = await api.get<Resource>('/news/resources/recommend', { limit });
    return response.data;
  },

  /**
   * 获取轮播资源列表
   * @param limit 限制数量
   * @returns Promise<ResultDomain<Resource>>
   */
  async getBannerResources(limit?: number): Promise<ResultDomain<Resource>> {
    const response = await api.get<Resource>('/news/resources/banner', { limit });
    return response.data;
  },

  /**
   * 搜索资源
   * @param keyword 搜索关键词
   * @param categoryID 分类ID（可选）
   * @param status 状态（可选）
   * @returns Promise<ResultDomain<Resource>>
   */
  async searchResources(
    keyword: string, 
    categoryID?: string, 
    status?: number
  ): Promise<ResultDomain<Resource>> {
    const params: any = { keyword };
    if (categoryID) params.categoryID = categoryID;
    if (status !== undefined) params.status = status;
    
    const response = await api.get<Resource>('/news/resources/search', params);
    return response.data;
  }
};

export default resourceApi;
