package org.xyzh.news.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xyzh.api.news.tag.TagService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.dto.resource.TbTag;
import org.xyzh.common.dto.resource.TbResourceTag;

import java.util.List;

/**
 * @description 标签控制器
 * @filename TagController.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-15
 */
@RestController
@RequestMapping("/news/tags")
public class TagController {
    private static final Logger logger = LoggerFactory.getLogger(TagController.class);

    @Autowired
    private TagService tagService;

    /**
     * 获取标签列表
     */
    @GetMapping("/list")
    public ResultDomain<TbTag> getTagList() {
        return tagService.getAllTags();
    }

    /**
     * 根据ID获取标签详情
     */
    @GetMapping("/tag/{tagID}")
    public ResultDomain<TbTag> getTagById(@PathVariable String tagID) {
        return tagService.getTagById(tagID);
    }

    /**
     * 创建标签
     */
    @PostMapping("/tag")
    public ResultDomain<TbTag> createTag(@RequestBody TbTag tag) {
        return tagService.createTag(tag);
    }

    /**
     * 更新标签
     */
    @PutMapping("/tag")
    public ResultDomain<TbTag> updateTag(@RequestBody TbTag tag) {
        return tagService.updateTag(tag);
    }

    /**
     * 删除标签
     */
    @DeleteMapping("/tag/{tagID}")
    public ResultDomain<Boolean> deleteTag(@PathVariable String tagID) {
        return tagService.deleteTag(tagID);
    }

    /**
     * 搜索标签
     */
    @GetMapping("/search")
    public ResultDomain<TbTag> searchTags(@RequestParam String keyword) {
        return tagService.searchTagsByName(keyword);
    }

    // ----------------资源标签关联相关--------------------------------

    /**
     * 获取资源的标签列表
     */
    @GetMapping("/resource/{resourceID}")
    public ResultDomain<TbTag> getResourceTags(@PathVariable String resourceID) {
        return tagService.getResourceTags(resourceID);
    }

    /**
     * 为资源添加单个标签
     */
    @PostMapping("/resource/{resourceID}/tag/{tagID}")
    public ResultDomain<TbResourceTag> addResourceTag(
            @PathVariable String resourceID,
            @PathVariable String tagID) {
        return tagService.addResourceTag(resourceID, tagID);
    }

    /**
     * 批量为资源添加标签
     */
    @PostMapping("/resource/{resourceID}/tags")
    public ResultDomain<TbResourceTag> batchAddResourceTags(
            @PathVariable String resourceID,
            @RequestBody List<String> tagIDs) {
        return tagService.batchAddResourceTags(resourceID, tagIDs);
    }

    /**
     * 移除资源的标签
     */
    @DeleteMapping("/resource/{resourceID}/tag/{tagID}")
    public ResultDomain<Boolean> removeResourceTag(
            @PathVariable String resourceID,
            @PathVariable String tagID) {
        return tagService.removeResourceTag(resourceID, tagID);
    }

    /**
     * 清空资源的所有标签
     */
    @DeleteMapping("/resource/{resourceID}/tags")
    public ResultDomain<Boolean> clearResourceTags(@PathVariable String resourceID) {
        return tagService.clearResourceTags(resourceID);
    }

    /**
     * 根据标签获取资源列表
     */
    @GetMapping("/tag/{tagID}/resources")
    public ResultDomain<String> getResourcesByTag(@PathVariable String tagID) {
        return tagService.getResourcesByTag(tagID);
    }
}
