/**
 * @description 标签API接口
 * @filename resourceTag.ts
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-15
 */

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

/**
 * 标签API服务
 */
export const resourceTagApi = {
  // ==================== 标签基础操作 ====================
  
  /**
   * 获取标签列表
   * @returns Promise<ResultDomain<Tag>>
   */
  async getTagList(): Promise<ResultDomain<Tag>> {
    const response = await api.get<Tag>('/news/tags/list');
    return response.data;
  },

  /**
   * 根据ID获取标签详情
   * @param tagID 标签ID
   * @returns Promise<ResultDomain<Tag>>
   */
  async getTagById(tagID: string): Promise<ResultDomain<Tag>> {
    const response = await api.get<Tag>(`/news/tags/tag/${tagID}`);
    return response.data;
  },

  /**
   * 创建标签
   * @param tag 标签信息
   * @returns Promise<ResultDomain<Tag>>
   */
  async createTag(tag: Tag): Promise<ResultDomain<Tag>> {
    const response = await api.post<Tag>('/news/tags/tag', tag);
    return response.data;
  },

  /**
   * 更新标签
   * @param tag 标签信息
   * @returns Promise<ResultDomain<Tag>>
   */
  async updateTag(tag: Tag): Promise<ResultDomain<Tag>> {
    const response = await api.put<Tag>('/news/tags/tag', tag);
    return response.data;
  },

  /**
   * 删除标签
   * @param tagID 标签ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteTag(tagID: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`/news/tags/tag/${tagID}`);
    return response.data;
  },

  /**
   * 搜索标签
   * @param keyword 搜索关键词
   * @returns Promise<ResultDomain<Tag>>
   */
  async searchTags(keyword: string): Promise<ResultDomain<Tag>> {
    const response = await api.get<Tag>('/news/tags/search', { keyword });
    return response.data;
  },

  // ==================== 资源标签关联操作 ====================

  /**
   * 获取资源的标签列表
   * @param resourceID 资源ID
   * @returns Promise<ResultDomain<Tag>>
   */
  async getResourceTags(resourceID: string): Promise<ResultDomain<Tag>> {
    const response = await api.get<Tag>(`/news/tags/resource/${resourceID}`);
    return response.data;
  },

  /**
   * 为资源添加单个标签
   * @param resourceID 资源ID
   * @param tagID 标签ID
   * @returns Promise<ResultDomain<ResourceTag>>
   */
  async addResourceTag(resourceID: string, tagID: string): Promise<ResultDomain<ResourceTag>> {
    const response = await api.post<ResourceTag>(`/news/tags/resource/${resourceID}/tag/${tagID}`);
    return response.data;
  },

  /**
   * 批量为资源添加标签
   * @param resourceID 资源ID
   * @param tagIDs 标签ID列表
   * @returns Promise<ResultDomain<ResourceTag>>
   */
  async batchAddResourceTags(resourceID: string, tagIDs: string): Promise<ResultDomain<ResourceTag>> {
    const response = await api.post<ResourceTag>(`/news/tags/resource/${resourceID}/tags`, tagIDs);
    return response.data;
  },

  /**
   * 移除资源的标签
   * @param resourceID 资源ID
   * @param tagID 标签ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async removeResourceTag(resourceID: string, tagID: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`/news/tags/resource/${resourceID}/tag/${tagID}`);
    return response.data;
  },

  /**
   * 清空资源的所有标签
   * @param resourceID 资源ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async clearResourceTags(resourceID: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`/news/tags/resource/${resourceID}/tags`);
    return response.data;
  },

  /**
   * 根据标签获取资源列表
   * @param tagID 标签ID
   * @returns Promise<ResultDomain<string>>
   */
  async getResourcesByTag(tagID: string): Promise<ResultDomain<string>> {
    const response = await api.get<string>(`/news/tags/tag/${tagID}/resources`);
    return response.data;
  }
};

export default resourceTagApi;
